MySQL: Column automatically current insert time

I am creating a new table, and I would like each row to have a column equal to the time the row is inserted into the table. Is there a way to do this in the create table statement, or do I need to do this in my insert statement?

+4
source share
5 answers

This should work:

CREATE TABLE table1 ( timeStampColumn TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP ); 
+5
source

Something like this when creating a table.

 CREATE TABLE t1 ( insert_timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP ); 
+2
source

The TIMESTAMP data type offers automatic initialization and updating to the current date and time (that is, the current timestamp). MORE

 TIMESTAMP 

Example

 CREATE TABLE t1 ( ts TIMESTAMP ); 
+1
source

you can set the default value as NOW as

  CREATE TABLE example ( id INT NOT NULL PRIMARY KEY AUTO_INCREMENT, data VARCHAR(140), created TIMESTAMP DEFAULT NOW() ); 
+1
source

SQLFiddle demo

Automatic initialization and update for TIMESTAMP and DATETIME

 CREATE TABLE t1 ( id int, update_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP ); 
+1
source

Source: https://habr.com/ru/post/1491877/


All Articles