Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mysql automatically store record creation timestamp

Tags:

sql

mysql

Is there some way mysql can store timestamp automatically in a record row whenever that it is created. I was trying to use timestamp(data type) with current_timestamp as default value but then realised this will get updated everytime the record is updated. I just need something that will store create timestamp.

Thanks

like image 224
Good Guy Avatar asked Feb 02 '11 03:02

Good Guy


People also ask

How does MySQL store TIMESTAMP?

MySQL converts TIMESTAMP values from the current time zone to UTC for storage, and back from UTC to the current time zone for retrieval. (This does not occur for other types such as DATETIME, which is stored “as is”.) By default, the current time zone for each connection is the server's time.

What is automatic initialization and updating for TIMESTAMP in a MySQL table?

An auto-initialized column is set to the current timestamp for inserted rows that specify no value for the column. An auto-updated column is automatically updated to the current timestamp when the value of any other column in the row is changed from its current value.

How do I automatically insert date in MySQL?

You can use now() with default auto fill and current date and time for this. Later, you can extract the date part using date() function. Let us set the default value with some date.

How do I add a TIMESTAMP to a MySQL table?

Here is the SQL you can use to add the column in: ALTER TABLE `table1` ADD `lastUpdated` TIMESTAMP ON UPDATE CURRENT_TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ; This adds a column called 'lastUpdated' with a default value of the current date/time.


2 Answers

Set the DEFAULT constraint to use CURRENT_TIMESTAMP:

CREATE TABLE ...   your_date_column DATETIME DEFAULT CURRENT_TIMESTAMP   ... 

For an existing table, use the ALTER TABLE statement:

ALTER TABLE your_table ALTER COLUMN date_column SET DEFAULT CURRENT_TIMESTAMP 

Unless you specify a value to for the date_column, the default will be the date & time the INSERT statement was run. NULL and DEFAULT or valid values to use the default constraint otherwise, assuming the column is nullable.

like image 182
OMG Ponies Avatar answered Sep 24 '22 05:09

OMG Ponies


You can get the full details on timestamps in MySQL at https://dev.mysql.com/doc/refman/5.0/en/timestamp-initialization.html.

The point that you care about is that if you define a timestamp column as DEFAULT CURRENT_TIMESTAMP clause and don't have an ON UPDATE clause, the column has the current timestamp for its default value but is not automatically updated.

But be warned. The obvious thing to want to do is to have two timestamp columns, one being the creation time and the other being the last update time. Unfortunately it is a documented MySQL limitation that MySQL does not support this. I have no idea why MySQL has such an odd limitation - no other major database has problems with this common use case.

like image 31
btilly Avatar answered Sep 24 '22 05:09

btilly