I have a database table. The creation query is listed as below.
CREATE TABLE `prime_clock` (
`stage` int(11) NOT NULL,
`name` varchar(50) DEFAULT NULL,
`start_time` time NOT NULL,
`end_time` time NOT NULL,
`difference` time NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
Consider I have a sample entry as
INSERT INTO `prime_clock` (`stage`, `name`, `start_time`, `end_time`, `difference`) VALUES
(1, 'fff', '12:33:00', '13:00:00', '00:00:00');
For primary key I used
ALTER TABLE `prime_clock`
ADD PRIMARY KEY (`stage`);
Now the question is How do I find duration between the start and end time; I.e. end_time - start_time and store in difference automatically as add a new row or make a modification in start time or end time.
Also, I can do it manually using
UPDATE `prime_clock` SET `difference`=TIMEDIFF(`end_time`,`start_time`) WHERE 1
You could go ahead with one of the following approaches:
Generated columns (explained here): You can define the table as follows:
CREATE TABLE prime_clock (
stage int(11) NOT NULL,
name varchar(50) DEFAULT NULL,
start_time time NOT NULL,
end_time time NOT NULL,
difference time AS TIMEDIFF(end_time, start_time) STORED
ENGINE=InnoDB DEFAULT CHARSET=latin1;
Trigger: You can write a BEFORE INSERT trigger that calculates difference for every inserted row, e.g.:
DELIMITER //
CREATE TRIGGER calculate_difference
BEFORE INSERT
ON table FOR EACH ROW
BEGIN
SET NEW.difference = TIMEDIFF(end_time, start_time);
END; //
DELIMITER ;
Related
I have searched quite a bit and can't seem to find the answer to this. I am fairly new to MySQL and this is something I had previously written as a php script to update the individual tables, but would rather it update automatically on the database when the table changes.
When I update a table via a google script I then require about 6 different tables to be updated from different rows in the main table, by date, and each table requires data from a different column.
I know this is not the correct way to do it but I need the trigger to effectively do the following;
CREATE TRIGGER dash_1
ON global_summary AFTER UPDATE
UPDATE widget_dash_2 (date, metric)
WHERE inc_id=100 SELECT date, volume
FROM global_summary
WHERE (`date` = SUBDATE(CURDATE(), 1))
UPDATE widget_dash_2 (date, metric)
WHERE inc_id=101 SELECT date, volume
FROM global_summary
WHERE (`date` = SUBDATE(CURDATE(), 8))
This would be for one of the columns "volume" from table "global_summary", I then need to add up to another 5 updates such as;
UPDATE widget_dash_3 (date, metric)
WHERE inc_id=100 SELECT date, score
FROM global_summary
WHERE (`date` = SUBDATE(CURDATE(), 1))
UPDATE widget_dash_3 (date, metric)
WHERE inc_id=101 SELECT date, score
FROM global_summary
WHERE (`date` = SUBDATE(CURDATE(), 8))
Where "score" is another column in the table "global_summary".
Any guidance would be much appreciated. I have been looking for some guidance on this for days now and it has just made me more confused.
I'm assuming I need to do something with arrays and initially getting all data from the main table, but how can I then carve that up?
#sticky bit,
Unfortunately I have to put this data into these small tables, believe me I would much rather just pull it from global_summary directly.
CREATE TABLE `global_summary` (
`id` int(11) DEFAULT NULL,
`date` date DEFAULT NULL,
`volume` int(10) DEFAULT NULL,
`volume_problem` int(10) DEFAULT NULL,
`removed` int(10) DEFAULT NULL,
`score` int(10) DEFAULT NULL,
`error` int(10) DEFAULT NULL,
`total` int(10) DEFAULT NULL,
`previous` int(10) DEFAULT NULL,
`entryID` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
All the widget_dash tables;
CREATE TABLE `widget_dash_2` (
`inc_id` int(11) NOT NULL,
`date` date DEFAULT NULL,
`metric` int(10) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
Thanks for your feedback so far, and yes I probably was confusing UPDATE with INSERT.
I need to create a database to store some logs which can occurs once per millisecond.
I've created the following table:
CREATE TABLE `log` (
`DataEvento` timestamp(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3) ON UPDATE CURRENT_TIMESTAMP(3),
`CodiceEvento` int(11) NOT NULL,
`IdApplicativo` int(11) NOT NULL,
PRIMARY KEY (`DataEvento`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci;
And a stored procedure:
CREATE DEFINER=`root`#`localhost` PROCEDURE `ScriviEvento`(IN evt_id INT, IN app_id INT, IN evt_descr TEXT)
BEGIN
DECLARE timestamp_now TIMESTAMP(3) DEFAULT NOW(3);
INSERT INTO log (DataEvento, CodiceEvento, IdApplicativo) VALUES (timestamp_now, evt_id, app_id);
IF (LENGTH(evt_descr) > 0) THEN
INSERT INTO descrizionelog (DataEvento, DescrizioneEvento) VALUES (timestamp_now, evt_descr);
END IF;
END
Inserting manually some entries I get the correct timestamp with milliseconds but if I create a thread
with a Sleep(1) I got duplicate key error, same happens if I press execute button fast in workbench with
CALL(1, 0, '');
Is there a workaround to this (excluding using an auto-increment id), or am I doing something wrong?
You are doing something wrong by assuming that the timestamp is going to be unique for log records. That really doesn't make sense.
I'm not sure why you are opposed to an auto-increment solution. This would be the right approach:
CREATE TABLE `log` (
LogId int auto_increment primary key,
`DataEvento` timestamp(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3) ON UPDATE CURRENT_TIMESTAMP(3),
`CodiceEvento` int NOT NULL,
`IdApplicativo` int NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci;
Hi I have a log database table in mysql which captures a start date and time and an end date and time.
The start and now the stop time is inserted to the record.
I have a third field which is duration which I would like displayed as hh:mm:ss
The schema looks like this at present
CREATE TABLE IF NOT EXISTS `log` (
`uid` int(20) NOT NULL AUTO_INCREMENT,
`room` int(11) NOT NULL,
`start` datetime NOT NULL,
`stop` datetime NOT NULL,
`duration` datetime DEFAULT NULL,
`participants` int(3) NOT NULL,
`recorded` varchar(3) NOT NULL,
`rec_file` varchar(35) NOT NULL,
PRIMARY KEY (`uid`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 COMMENT='Log File' AUTO_INCREMENT=106 ;
I'm trying to create a trigger which calculates the duration and writes it to the duration field as data is inserted
Currently the trigger I am trying to debug looks like this
BEGIN
SET NEW.duration = (TIMEDIFF (NEW.start,NEW.stop)) ;
END
The result is the duration field remains set to 0000-00-00 00:00:00.000000
Any suggestions on how to make this work are greatfully recived
Try to add the following :
Change duration type to TIME instead of DATETIME. According to documentation, the result returned by TIMEDIFF() is limited to the range allowed for TIME values.
This is the reason you are receiving all zeros currently.
I assume that stop time will always be later than start time, so I would write the trigger in the following way:
CREATE TRIGGER your_schema.insert_duration BEFORE INSERT ON your_schema.log
FOR EACH ROW SET NEW.duration = TIMEDIFF(NEW.stop, NEW.start);
The result will be in HH:MM:SS format for duration field.
this is my tables
CREATE TABLE IF NOT EXISTS `carslibrary` (
`CarID` int(10) unsigned NOT NULL AUTO_INCREMENT,
`CarName` varchar(255) NOT NULL,
PRIMARY KEY (`CarID`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=4 ;
CREATE TABLE IF NOT EXISTS `colorslibrary` (
`ColorID` int(11) unsigned NOT NULL AUTO_INCREMENT,
`ColorName` varchar(255) NOT NULL,
PRIMARY KEY (`ColorID`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=5 ;
CREATE TABLE IF NOT EXISTS `facerecord` (
`carslibrary_ID` int(10) unsigned NOT NULL,
`colorslibrary_ID` int(11) unsigned NOT NULL,
KEY `carslibrary_ID` (`carslibrary_ID`),
KEY `colorslibrary_ID` (`colorslibrary_ID`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
i noticed carslibrary_ID attribute inside facerecord table is not automatically updated when i add a car record inside carslibrary table, what should i do to be able to?
Firstly, you'll need to have a default value specified for the facerecord.colorslibrary_ID since you will not 'know' what it is when inserting into the carslibrary table. That said you could alter your DDL for the facerecord table to be:
CREATE TABLE `facerecord` (
`carslibrary_ID` int(10) unsigned NOT NULL,
`colorslibrary_ID` int(10) unsigned NOT NULL DEFAULT '0',
KEY `carslibrary_ID` (`carslibrary_ID`),
KEY `colorslibrary_ID` (`colorslibrary_ID`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
I've also changed the datatype of the colorslibrary_ID column to match that of the colorslibrary.ColorID column in case you ever feel like setting up a foreign key between facerecord.colorslibrary_ID and colorslibrary.ColorID ;). For the sake of completeness you should insert a row into the colorslibrary table with a ColorID = 0. Hence:
insert into `colorslibrary` (ColorName) values ('unknown color');
update `colorslibrary` set ColorID = 0 where ColorName = 'unknown color';
Then you can go ahead and define your trigger to insert into the facerecord table:
delimiter $$
CREATE TRIGGER carslibrary_trigger
AFTER insert ON carslibrary
FOR EACH ROW
BEGIN
insert into facerecord (carslibrary_ID) values (new.CarID);
END$$
delimiter;
All new rows inserted into the facerecord table will then be inserted with a colorslibrary_ID that relates to the 'unknown color' colorslibrary.ColorName.You can then manually update the facerecord.colorslibrary_ID as and when you know it.
Good luck!
PS If you need to remove any existing AFTER insert triggers from the carslibrary table you can do so by firstly finding the existing triggers:
select trigger_name
from information_schema.triggers
where event_object_table = 'carslibrary'
and action_timing = 'AFTER'
and event_manipulation= 'INSERT';
Then take the name of the trigger returned by the above statement (lets say the string 'carslibrary_trigger' is returned) and run:
drop trigger carslibrary_trigger;
Then re-run the CREATE TRIGGER script.
Once a trigger is set up it will automatically perform the action you have specified when the trigger action you have specified occurs. In this case we are telling the database "after an insert happens into the carslibrary table automatically insert a row into the facerecord table using the CarID of the new carslibrary row to populate the facerecord.carslibrary_ID column". As with most things the best way is to try it! Once you have created the trigger manually insert a new row into the 'carslibrarytable. Now look at the data in thefacerecord` table - you should see a new row that has been inserted by the trigger firing.
It sounds like you would benefit from learning about triggers. I recommend the docs on the MySQL site because this answer is way longer than I first intended it to be!
You will need to use triggers. See http://dev.mysql.com/doc/refman/5.0/en/triggers.html
I would like to create a table that has both a column for "created" and another for "updated". The column "created" will be set at insert and never change. The column "updated" will change every time a row is updated. I don't want to mess with either of these columns in the subsequent INSERT or UPDATE statements. So what should my CREATE TABLE statement look like if I start with something like this?
CREATE TABLE IF NOT EXISTS `mydb`.`mytable` (
`id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
`updated` TIMESTAMP,
`created` TIMESTAMP,
`deleted` TINYINT DEFAULT 0,
`notes` TEXT DEFAULT '',
`description` VARCHAR(100)
) TYPE=innodb;
I seem to be having trouble creating a table with two TIMESTAMP columns. I don't care if the columns are TIMESTAMP or DATETIME or whatever, I just want them to be populated by MySQL without explicit instructions from the insert or update statements.
I would like to be able to do inserts like this:
INSERT INTO `mydb`.`mytable` (notes,description) VALUES ('some note','some description');
and updates like this:
UPDATE `mydb`.`mytable` SET notes=CONCAT(notes,'some more notes') WHERE id=1;
both without having to explicitly set the "created" column or set (or reset) the "updated" column in the insert or update statement.
Try this one to create your table:
CREATE TABLE IF NOT EXISTS db.test_table
(
Id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
created DATETIME DEFAULT NULL,
updated TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
deleted TINYINT DEFAULT 0,
notes TEXT DEFAULT NULL,
description VARCHAR(100)
)
Note that
updated TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
will allow to update this field automatically.
And set this one for a trigger before inserting records:
DELIMITER $$
CREATE
/*[DEFINER = { user | CURRENT_USER }]*/
TRIGGER `db`.`on_before_insert` BEFORE INSERT
ON `db`.`test_table`
FOR EACH ROW BEGIN
SET new.created = NOW();
END$$
DELIMITER ;
Then you can use this to insert:
INSERT INTO db.test_table(description) VALUES ("Description")
and to update your record
UPDATE db.test_table SET description = "Description 2" where Id=1
And your created and updated fields will be set appropiately.
News flash: In mysql, TIMESTAMP columns are always updated with now() every time any other column in the row is updated - this is a deliberate feature of this datatype.
DATETIME on the other hand does not have this weird behaviour - it's completely normal.
The answer: created must be DATETIME, but due to this bug, you also need a trigger, like this:
CREATE TABLE IF NOT EXISTS mytable (
`id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
`updated` TIMESTAMP, -- This will be updated to now(), if you don't set it or set it to null
`created` DATETIME NOT NULL, -- This will never be magically updated once written
`deleted` TINYINT DEFAULT 0,
`notes` TEXT DEFAULT '',
`description` VARCHAR(100)
) TYPE=innodb;
DELIMITER ~
CREATE TRIGGER mytable_insert_trigger
BEFORE INSERT ON mytable
FOR EACH ROW BEGIN
SET NEW.created = CURRENT_TIMESTAMP;
END;~
DELIMITER ;
insert into mytable (notes) values ('test');
select * from mytable;
+----+---------------------+---------------------+---------+-------+-------------+
| id | updated | created | deleted | notes | description |
+----+---------------------+---------------------+---------+-------+-------------+
| 1 | 2011-07-05 11:48:02 | 2011-07-05 11:48:02 | 0 | test | NULL |
+----+---------------------+---------------------+---------+-------+-------------+
Try this:
CREATE TABLE IF NOT EXISTS mydb.mytable
(
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
updated DATETIME,
created TIMESTAMP,
deleted TINYINT DEFAULT 0,
notes TEXT DEFAULT '',
description VARCHAR(100)
) TYPE=innodb;
Edit: Use a trigger.
CREATE TRIGGER mytable_update
BEFORE UPDATE ON mydb.mytable
FOR EACH ROW SET NEW.updated = NOW();
alternative is to change the order of timestamp column
OR
set first column DEFAULT value like this
ALTER TABLE `tblname` CHANGE `first_timestamp_column`
`first_timestamp_column` TIMESTAMP NOT NULL DEFAULT 0;
Reference
Unfortunately MySQL doesn't let you have two TIMESTAMP columns in one table. I would use ON UPDATE CURRENT_TIMESTAMP for the updated column and set created manually using the NOW() function.