Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL: Column automaticly current time of insert

Tags:

mysql

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

like image 345
Dan Ciborowski - MSFT Avatar asked Dec 26 '22 00:12

Dan Ciborowski - MSFT


2 Answers

This should work:

CREATE TABLE table1 (
  timeStampColumn TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);
like image 170
SOfanatic Avatar answered Feb 22 '23 23:02

SOfanatic


Something like this when creating the table.

CREATE TABLE t1 (
  insert_timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
like image 30
Gilbert Le Blanc Avatar answered Feb 23 '23 01:02

Gilbert Le Blanc