Automatic insert and update timestamp on Mysql table's
7331 Views
Example with Create Table Query
CREATE TABLE table_name
(id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
username VARCHAR(20) NOT NULL,
modified_on TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP);
Example with Alter Table Query
ALTER TABLE table_name
MODIFY COLUMN
`modified_on` TIMESTAMP NOT NULL
DEFAULT CURRENT_TIMESTAMP
ON UPDATE CURRENT_TIMESTAMP;
Notes
Default CURRENT_TIMESTAMP is putting current time when record insert on table. ON UPDATE CURRENT_TIMESTAMP is putting current time when record update on table.