Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Timestamp for row creation and last modification

I need to keep track of the time a row was inserted into the database, and the time it was last modified.

I tried to create two separate columns, and use CURRENT_TIMESTAMP:

create table def (
  id int, 
  creation timestamp 
    default CURRENT_TIMESTAMP, 
  modification timestamp 
    on update CURRENT_TIMESTAMP
);

However, this produced an error:

ERROR 1293 (HY000): Incorrect table definition; there can be only one TIMESTAMP column with CURRENT_TIMESTAMP in DEFAULT or ON UPDATE clause

What is the best way to do this?

I'm thinking stored procedure, but looking for a standard solution. I'm also concerned with access privileges -- as few programs/things should be able to touch the timestamps as possible.


Although I would prefer MySQL answers, solutions for other RDBMS's are also appreciated!

like image 759
Matt Fenwick Avatar asked Dec 03 '11 00:12

Matt Fenwick


People also ask

How can I tell when a mysql table was last updated?

SELECT UPDATE_TIME FROM information_schema. tables WHERE TABLE_SCHEMA = 'yourDatabaseName' AND TABLE_NAME = 'yourTableName'; Let us implement the following query to get the last updated time. mysql> SELECT UPDATE_TIME -> FROM information_schema.

What creates timestamp?

A timestamp is a sequence of characters or encoded information identifying when a certain event occurred, usually giving date and time of day, sometimes accurate to a small fraction of a second. Timestamps do not have to be based on some absolute notion of time, however.

How do I change a timestamp in SQL query?

Syntax – Update value to Current TimestampALTER TABLE table_name updates table schema. CHANGE column_name updates the column to. column_name TIMESTAMP NOT NULL defines the column as of datatype TIMESTAMP. DEFAULT CURRENT_TIMESTAMP sets the default value of the column to CURRENT_TIMESTAMP.


2 Answers

Ya this is a lame limitation on MySQL. If you are going through an application you can add a time() call for the created_at column, and let the updated_at column use the CURRENT_TIMESTAMP.

$sql = "INSERT INTO my_table SET name = 'Mike', created_at = " . time();

I'd opt to do this on the created_at column as it probably won't be touched as often as the updated_at column.

-- Edit --

Better yet, use MySQL's built in now() function. This way you only need to be concerned with the timezone of the mysql server, and not the timezones of the app server AND the mysql server.

$sql = "INSERT INTO my_table SET name = 'Mike', created_at = NOW()";
like image 88
Mike Purcell Avatar answered Oct 14 '22 07:10

Mike Purcell


You can use a trigger. The application can also set the value, but if do, it will be overwritten by the database.

delimiter //
CREATE TRIGGER def_bef_update BEFORE UPDATE ON def FOR EACH ROW BEGIN
    SET NEW.modification = CURRENT_TIMESTAMP;
END//
delimiter ;

You can also use it to check the data and update your modification date only if has important changes.

like image 44
Paulo H. Avatar answered Oct 14 '22 08:10

Paulo H.