MySQL auto-store datetime for each row
Asked Answered
D

7

60

In MySQL, I'm sick of adding the columns dt_created and dt_modified (which are date time stamps for creation and last modified respectively) to all the tables I have in my database.

Every time I INSERT or UPDATE the database, I will have to use the NOW() keyword. This is going all over my persistence.

Is there any efficient alternative where MySQL can automatically store at least the datatime of the row that is inserted and let me retrieve it?

Digitiform answered 19/12, 2009 at 5:47 Comment(1)
My poor grammar lurks on the dark depths of Stack Overflow. Thank you @Eric Leschinski.Digitiform
S
112

You can use DEFAULT constraints to set the timestamp:

ALTER TABLE
 MODIFY dt_created datetime DEFAULT CURRENT_TIMESTAMP

ALTER TABLE
 MODIFY dt_modified datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP

Then you wouldn't have to specify NOW() in your INSERT/UPDATE statements.

Reference: TIMESTAMP properties

Swirl answered 19/12, 2009 at 5:58 Comment(3)
ahh I didn't know you can use something like ON UPDATE CURRENT_TIMESTAMPDigitiform
Aditional info: One TIMESTAMP column in a table can have the current timestamp as the default value for initializing the column, as the auto-update value, or both. It is not possible to have the current timestamp be the default value for one column and the auto-update value for another column. dev.mysql.com/doc/refman/5.0/en/timestamp-initialization.htmlElsyelton
DEFAULT ON UPDATE CURRENT_TIMESTAMP is not valid SQL. It must be either dt_modified datetime ON UPDATE CURRENT_TIMESTAMP or dt_modified datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP If you still claim it is valid, then perhaps MySQL Workbench has a problem with it that should be mentioned.Stenotypy
I
15

If you're using phpmyadmin you can do this by :

enter image description here

Imaret answered 4/4, 2014 at 13:31 Comment(0)
T
11
ALTER TABLE  `tablename` CHANGE  `dt`  `dt` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP

should be the correct code.

Trompe answered 20/2, 2010 at 10:13 Comment(0)
F
3

Well, you can't have both:

mysql doc:

It is not possible to have the current timestamp be the default value for one column and the auto-update value for another column.

Sad, isn't it?

You could however use null instead of now() following this tip

Fusilier answered 29/3, 2013 at 13:4 Comment(0)
O
1

Similar question was asked here "Timestamp for MySQL" the timestamp field will update every time it is accessed. You might also consider a Trigger placed on the table in question to automatically populate those fields for you. Depending on the environment some shops/businesses do not like the use of triggers and so you might have to find alternate work arounds.

Occasional answered 19/12, 2009 at 5:58 Comment(0)
B
0

In phpmyadmin you can set enter image description here

OR use this query

ALTER TABLE  `tablename`
    CHANGE  `dt_created`  `dt_created` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
Busk answered 28/4, 2017 at 13:21 Comment(0)
C
0

could be set as default an on update of rows

ALTER TABLE `tablename` CHANGE `dt` `dt` TIMESTAMP on update CURRENT_TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP;
Convulsion answered 8/11, 2019 at 1:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.