Reputation: 7247
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
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
Reputation: 24144
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
Reputation: 51565
Something like this when creating the table.
CREATE TABLE t1 (
insert_timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
Upvotes: 2
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
Reputation: 5573
This should work:
CREATE TABLE table1 (
timeStampColumn TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);
Upvotes: 5