I'm looking for the most efficient solution to the problem I'm running into. I'm designing a shift calendar for our employees. This is the table I'm working with so far:
CREATE TABLE IF NOT EXISTS `Shift` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`accountId` smallint(6) unsigned NOT NULL,
`grpId` smallint(6) unsigned NOT NULL,
`locationId` smallint(6) unsigned NOT NULL,
`unitId` smallint(6) unsigned NOT NULL,
`shiftTypeId` smallint(6) unsigned NOT NULL,
`startDate` date NOT NULL,
`endDate` date NOT NULL,
`needFlt` bit(1) NOT NULL DEFAULT b'1',
`needBillet` bit(1) NOT NULL DEFAULT b'1',
`fltArr` varchar(10) NOT NULL,
`fltDep` varchar(10) NOT NULL,
`fltArrMade` bit(1) NOT NULL DEFAULT b'0',
`fltDepMade` bit(1) NOT NULL DEFAULT b'0',
`billetArrMade` bit(1) NOT NULL DEFAULT b'0',
`billetDepMade` bit(1) NOT NULL DEFAULT b'0',
`FacilityId` smallint(6) unsigned NOT NULL,
`FacilityWingId` mediumint(9) unsigned NOT NULL,
`FacilityRoomId` int(11) unsigned NOT NULL,
`comment` varchar(255) NOT NULL,
`creation` datetime NOT NULL,
`lastUpdate` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`lastUpdateBy` mediumint(9) unsigned NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
Now here's the hitch - I'd like to be able to display on the calendar (in a different color) whether or not a timesheet has been received for a certain day.
My first thought was to create a separate table and list separate entries by day for each employee, T/F. But the amount of data returned from a separate query, for each employee, for the whole month would surely be huge and inefficient.
Second thought was to somehow put the information in this Shift table, with delimiters - then exploding it with PHP. Silly idea... but I guess that's why im here. Any thoughts?
Thanks for your help!
As hinted previously and I think you realized yourself, serializing the data into a single column or using some other form of delimited string is a path to computational inefficiencies in the packing and unpacking and serious maintenance grief for the future.
Heaps better is to get the data structure right, i.e. a properly normalized table. After all, MySQL is rather good at dealing with this some of structure.
You don't need to pull back every line for every staff member. If you're pull them out together, you could "group" your resultset by employee and date, and even make that a potentially useful result by (say) pulling the summary of hours. A zero result or null result would show no timesheet, and the total hours may be helpful in some other way.
If you were pulling them out an employee and a date at a time then your application structure probably needs looking at, but you could use the SQL LIMIT keyword to pull at most one record and then test to see if any came back.
Related
I know there are many examples of how to use this as well as links to the MySQL documentation. Unfortunately, I am still a in need of clarification on how it actually works.
For instance, The following table structure (SQL code) is one example of what I need to use the INSERT ... OR UPDATE:
CREATE TABLE IF NOT EXISTS `occt_category` (
`category_id` int(11) NOT NULL,
`image` varchar(255) DEFAULT NULL,
`parent_id` int(11) NOT NULL DEFAULT '0',
`top` tinyint(1) NOT NULL,
`column` int(3) NOT NULL,
`sort_order` int(3) NOT NULL DEFAULT '0',
`status` tinyint(1) NOT NULL,
`date_added` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
`date_modified` datetime NOT NULL DEFAULT '0000-00-00 00:00:00'
) ENGINE=MyISAM AUTO_INCREMENT=0 DEFAULT CHARSET=utf8;
ALTER TABLE `occt_category` ADD PRIMARY KEY (`category_id`);
ALTER TABLE `occt_category` MODIFY `category_id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=0;
What I am attempting to insert into this mess are new categories from an API source so there are definitely duplicates.
What I am getting from the API is the following:
[
{
"categoryID": 81,
"name": "3/4 Sleeve",
"url": "3-4sleeve",
"image": "Images/Categories/81_fm.jpg"
}
]
So given the above information; Do I need to change my table structure to check for duplicates coming in from the API?
In MSSQL I would just simply do an IF EXISTS .... statement to check for duplicates. Unfortunately, this is MySQL :(.
If you intend to make use of the INSERT ... ON DUPLICATE KEY UPDATE MySQL Syntax (which is what I understand from your question, as INSERT ... OR UPDATE is not a real MySQL command), then your current table structure is fine and you will NOT have to check for duplicate records.
The way this works is that before writing any new records into your table, the MySQL DB will first check to see if there are any records that have a value in a PRIMARY or UNIQUE key-field (in your case category_id) that is the same value for the corresponding field in the incoming record, if it finds one, it will simply update that record as opposed to writing a new one.
You can read more about this syntax here.
I have a mysql table whose create code is as follows :
CREATE TABLE image_ref (
region VARCHAR(50) NULL DEFAULT NULL,
district VARCHAR(50) NULL DEFAULT NULL,
district_name VARCHAR(100) NULL DEFAULT NULL,
lot_no VARCHAR(10) NULL DEFAULT NULL,
sp_no VARCHAR(10) NULL DEFAULT NULL,
name VARCHAR(200) NULL DEFAULT NULL,
form_no VARCHAR(50) NOT NULL DEFAULT '',
imagename VARCHAR(50) NULL DEFAULT NULL,
updated_by VARCHAR(50) NULL DEFAULT NULL,
update_log DATETIME NULL DEFAULT NULL,
ip VARCHAR(50) NULL DEFAULT NULL,
imgfetchstat VARCHAR(1) NULL DEFAULT NULL,
PRIMARY KEY (form_no)
)
COLLATE='latin1_swedish_ci'
ENGINE=MyISAM;
This table contains approximately 7,00,000 number of rows. I have an application developed using PHP. Somewhere I need to run the following query :
SELECT
min(imagename) imagename
FROM
image_ref
WHERE
district_name = '$sess_district'
AND
lot_no = '$sess_lotno'
AND
imgfetchstat = '0';
which is taking on average 1.560 sec. The form_no field only has unique values. After some job is done with the result set fetched, the imgfetchstat is required to be updated with a value 1. Now my requirement is that, whether I should use InnoDB or MyISAM? Also, the application is accessed by around 50 numbers of users in LAN. Is there any way out to run the above query little bit faster? because the imagename fetched is being used to load an image of resolution 500 x 498 into the browser and the it is taking enough time to load the image. Thanks in advance.
You can add indexes to your table (be aware that this will make the storage larger - but given your query, you should be able to use the following:
ALTER TABLE `table` ADD INDEX `product_id` (`product_id`)
For more information see http://dev.mysql.com/doc/refman/5.0/en/create-index.html
You can add an index on a single column (which makes things nice for the DB, even it it uses a few of them on a query) but if you have a secific query that needs to REALLY run fast, you can add a multi-column index which is specific to your query:
ALTER TABLE image_ref ADD INDEX `someName`
(`district_name`, `lot_no`, `imgfetchstat`)
I'm upgrading a system from CakePHP 1.1 to 1.3. When using create() (followed by save()) in 1.1 the system would happily add an insert an entry to the database with the created and modified datetime fields set to that of the time it was created. However, in 1.3 this is no longer correctly happening. Here the modified is still set to the current time upon the creation and save, but the created datetime is not being set. Any suggestions as to why this might be occurring? Thanks!
Create Table Code (as requested in comment):
CREATE TABLE `units` (
`id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`id_b36` VARCHAR(4) NULL DEFAULT NULL,
`subject_id` INT(10) UNSIGNED NOT NULL,
`gradelevel_id` INT(10) UNSIGNED NOT NULL,
`user_id` INT(10) UNSIGNED NOT NULL,
`school_id` INT(10) NULL DEFAULT NULL,
`district_id` INT(10) NULL DEFAULT NULL,
`description` VARCHAR(255) NOT NULL,
`sort` FLOAT(5,1) NOT NULL DEFAULT '0.0',
`created` DATETIME NOT NULL DEFAULT '0000-00-00 00:00:00',
`modified` DATETIME NOT NULL DEFAULT '0000-00-00 00:00:00',
PRIMARY KEY (`id`),
INDEX `subject_id` (`subject_id`),
INDEX `gradelevel_id` (`gradelevel_id`),
INDEX `sort` (`sort`),
INDEX `school_id` (`school_id`, `district_id`)
)
COLLATE='latin1_swedish_ci'
ENGINE=InnoDB
ROW_FORMAT=DEFAULT
AUTO_INCREMENT=483
I should note that this is just one of the tables, the same thing occurs on all other models. Thanks!
created DEFAULT NULL
CakePHP will only populate created if it is defined as NULL:
By defining a created and/or modified field in your database table as datetime fields (default null), CakePHP will recognize those fields and populate them automatically whenever a record is created ...
I have database structure like this.
CREATE TABLE IF NOT EXISTS `addreminde` (
`SMSId` int(11) NOT NULL AUTO_INCREMENT,
`UserId` int(11) NOT NULL,
`SendFrom` varchar(20) NOT NULL,
`SendTo` varchar(400) NOT NULL,
`Message` varchar(400) NOT NULL,
`ReminderTime` datetime NOT NULL,
`Status` varchar(400) DEFAULT NULL,
`datetime` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`SMSId`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=15;
So i am storing the ReminderTime in database.
Now want to know how i can send a email or (let say execute a php script ) to "SendTo" at the "ReminderTime"
Any help will be appreciated.
This cannot be done in PHP. PHP only comes to 'live' when a user makes a request to the webserver.
As mentioned, a cron job is the way to go. And a query ones a minute and sending some email will not be a big load for your server.
currently I am in the process of structuring a database for a site I am creating. However, I have come across a problem. I want to log the amount of times a user has logged in each day, and then be able to keep track of that info over large periods of time such as a 8 months, a year, 2 years, etc.
The only way I can think of right now, is to just have a column for each day of the year/automatically create a column each day. This idea however, just seems plain stupid to me. I'm sure there has to be a better way to do this, I just can't think of one.
Any suggestions?
Thanks,
Rob
Create a separate table where you store user_id, and datetime the user logs in.
Averytime the user logs in, you insert a new record on this table.
Example:
CREATE TABLE user_activity (
userid varchar(50),
log_in_datetime datetime
);
Here is a login table I use for one of my sites. The datetime can either be logged as a datetime or as a timestamp. If you use datetime make sure to consider the timezone of your mysql server.
There is plenty of stuff to track. Then you can just query it later. Each of these column names should be self explanatory with a google search.
CREATE TABLE `t_login` (
`id_login` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`id_user` INT(10) UNSIGNED NOT NULL DEFAULT '0',
`id_visit` INT(10) UNSIGNED NOT NULL DEFAULT '0' COMMENT 'fk to t_visit',
`id_org` INT(10) UNSIGNED NOT NULL DEFAULT '0',
`when_attempt` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
`uname_attempt` VARCHAR(100) NOT NULL DEFAULT '' COMMENT 'attempted username' COLLATE 'latin1_swedish_ci',
`valid_uname` TINYINT(1) UNSIGNED NOT NULL DEFAULT '0' COMMENT 'valid username',
`valid_uname_pword` TINYINT(1) UNSIGNED NOT NULL DEFAULT '0' COMMENT 'valid username and valid password together',
`pw_hash_attempt` BINARY(32) NOT NULL DEFAULT '\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0',
`remote_ip` CHAR(20) NOT NULL DEFAULT '' COLLATE 'latin1_swedish_ci',
`user_agent` VARCHAR(2000) NOT NULL DEFAULT '' COLLATE 'latin1_swedish_ci',
PRIMARY KEY (`id_login`),
INDEX `when_attempt` (`when_attempt`),
INDEX `rempte_ip` (`remote_ip`),
INDEX `valid_user` (`valid_uname`),
INDEX `valid_password` (`valid_uname_pword`),
INDEX `username` (`uname_attempt`),
INDEX `id_ten` (`id_org`),
INDEX `id_user` (`id_user`),
INDEX `id_visit` (`id_visit`)
)
COLLATE='utf8_general_ci'
ENGINE=InnoDB
AUTO_INCREMENT=429;