Dan Ciborowski - MSFT
Dan Ciborowski - MSFT

Reputation: 7247

MySQL: Column automaticly current time of insert

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?

Upvotes: 4

Views: 128

Answers (5)

bkilinc
bkilinc

Reputation: 989

you can set default value as NOW like

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

Upvotes: 1

valex
valex

Reputation: 24144

SQLFiddle demo

Automatic Initialization and Updating for TIMESTAMP and DATETIME

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

Upvotes: 1

Gilbert Le Blanc
Gilbert Le Blanc

Reputation: 51565

Something like this when creating the table.

CREATE TABLE t1 (
  insert_timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

Upvotes: 2

Ganesh Rengarajan
Ganesh Rengarajan

Reputation: 2006

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

TIMESTAMP

sample

CREATE TABLE t1 (
  ts TIMESTAMP
);

Upvotes: 1

SOfanatic
SOfanatic

Reputation: 5573

This should work:

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

Upvotes: 5

Related Questions