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.
Related
W3schools defines the DATETIME datatype thusly:
A date and time combination. Format: YYYY-MM-DD hh:mm:ss. The
supported range is from '1000-01-01 00:00:00' to '9999-12-31
23:59:59'. Adding DEFAULT and ON UPDATE in the column definition to
get automatic initialization and updating to the current date and time
So, I'm trying to create a table that will record the date of each record inserted. I'm trying to record down to the minute, but not the second. I've tried to create the table as such:
create table forum(
postID int(2) AUTO_INCREMENT,
primary key (postID),
title varchar(100),
content varchar(2000),
postTime DATETIME(YYYY-MM-DD hh:mm) DEFAULT ON UPDATE
)
But that produces a syntax error. I've tried modifying it in countless ways, but short of
create table forum(
postID int(2) AUTO_INCREMENT,
primary key (postID),
title varchar(100),
content varchar(2000),
postTime DATETIME
)
nothing seems to be palatable to MySQL workbench. How can I properly express what I'm trying to do?
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 ;
I Wanted to initialize default date field in my table with CURRENT_DATE(); well BENCH gives me errors that is not possible i heard about triggers but it seems to be a little over complicatet for that problem so is there any way to make this in such way
CREATE TABLE Sprzedaz (
Id int unsigned primary key auto_increment,
KlientId int not null,
ProduktNumer int not null,
Ilosc int not null,
Cena float not null,
Data date default CURRENT_DATE(),
check (Data >= now()),
....
);
You can initialize a TIMESTAMP column with this:
Data TIMESTAMP DEFAULT CURRENT_TIMESTAMP
or a DATETIME column (MySQL 5.6+):
Data DATETIME DEFAULT CURRENT_TIMESTAMP
but if you want to initialize a DATE column using MySQL 5.5 you need an INSERT TRIGGER:
CREATE TRIGGER setdate_before_insert BEFORE INSERT ON test
FOR EACH ROW
SET NEW.Data = CURDATE();
and maybe you need also an UPDATE trigger? Please see fiddle here.
Another way to go about this, if you do not mind changing the date type, would be to use a TIMESTAMP and initialize it with TIMESTAMP DEFAULT CURRENT_TIMESTAMP. Your definition would then become:
CREATE TABLE Sprzedaz (
Id int unsigned primary key auto_increment,
KlientId int not null,
ProduktNumer int not null,
Ilosc int not null,
Cena float not null,
Data TIMESTAMP DEFAULT CURRENT_TIMESTAMP, <<<==== change this
check (Data >= now()),
....
);
If you have MySQL version 5.6.5 and above, you can use CURRENT_TIMESTAMP instead of CURRENT_DATE
See http://dev.mysql.com/doc/refman/5.6/en/timestamp-initialization.html
So you can't do it with dates but you may be able to change your field to be a TIMESTAMP or DATETIME and Bobs your mothers brother!
CREATE TABLE `Schedule` (
`id` smallint(6) NOT NULL AUTO_INCREMENT,
`name` varchar(30) DEFAULT NULL,
`deptime` varchar(20) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=3221 DEFAULT CHARSET=latin1;
The field deptime contains entries in the following format: 2012-09-04 09:17.
Now I need to change the day of all entries in this field, i.e. 2013-07-01 09:17. The time, i.e. 09:17 should not be changed. How can I do this in a quick way using some UPDATE query?
Or try this...
UPDATE schedule SET deptime = CONCAT('2013-07-01 ',TIME(deptime));
Try this-
update table Schedule
set deptime = STR_TO_DATE(deptime,'%Y-%m-7 %h: %i');
OR
update table Schedule
set deptime = STR_TO_DATE(deptime,'%Y-%m-$PHPVar %h: %i');
1) Give the hardcode value in day field or provide PHP variable if you have.
2) use %H for 24 hrs format and %h for 12 hrs format in str_to_date function
This table I created in a SQLite database:
CREATE TABLE [tickets] (
[id] INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
[coupon_id] INTEGER NULL,
[size] FLOAT NULL,
[phone] VARCHAR(10) NULL,
[date] DATE DEFAULT CURRENT_DATE NULL,
[time] TIME DEFAULT CURRENT_TIME NULL,
[product] TEXT NULL
);
Now INSERT operation is:
INSERT INTO "tickets" VALUES(429,9,18.16,'949-893-5032','2010-11-30','17:46:39','Kids’ Kups Berry Interesting™');
INSERT INTO "tickets" VALUES(430,9,12.04,'847-188-1359','2010-11-25','10:54:00','Raspberry Collider™');
INSERT INTO "tickets" VALUES(431,9,14.1,'204-682-5560','2010-12-08','15:34:07','Celestial Cherry High™');
Now the same table I created in MySQL:
CREATE TABLE tickets (
id INTEGER PRIMARY KEY AUTO_INCREMENT NOT NULL,
coupon_id INTEGER NULL,
size FLOAT NULL,
phone VARCHAR(10) NULL,
date TIMESTAMP DEFAULT CURRENT_TIMESTAMP NULL,
time TIMESTAMP DEFAULT CURRENT_TIMESTAMP NULL,
product TEXT NULL
);
INSERT operation for MySQL is:
INSERT INTO tickets VALUES(429,9,18.16,'949-893-5032','2010-11-30','17:46:39','Kids’ Kups Berry Interesting™');
INSERT INTO tickets VALUES(430,9,12.04,'847-188-1359','2010-11-25','10:54:00','Raspberry Collider™');
INSERT INTO tickets VALUES(431,9,14.1,'204-682-5560','2010-12-08','15:34:07','Celestial Cherry High™');
When i am inserting those values I got an error :-there can be only one TIMESTAMP column with current_timestamp in default of on update clause
…but I am not able to insert all those values into MySQL. Help me?
In SQLite you have two columns
[date] DATE DEFAULT CURRENT_DATE NULL,
[time] TIME DEFAULT CURRENT_TIME NULL,
while on MySQL you have only one
date TIMESTAMP DEFAULT CURRENT_TIMESTAMP NULL,
and you're trying to insert two values on it...
You should try
INSERT INTO tickets VALUES(..., '2010-11-30 17:46:39', ...)
At first glace, your varchar column is size 10, but you are inserting greater than length 10 data into it. Make sure your varchar column is wide enough for your data.
Your MySQL Schema appears to be incorrect for what you're trying to insert.
Excerpt from this post: Should I use field 'datetime' or 'timestamp'?
...Timestamps in MySQL generally used to track changes to records, and are updated every time the record is changed. If you want to store a specific value you should use a datetime field.
Change your MySQL schema to something closer to:
...
phone VARCHAR(12) NULL,
date DATE DEFAULT CURRENT_DATE NULL,
time TIME DEFAULT CURRENT_TIME NULL,
...