I am looking for the syntax to add a column to a MySQL table with the current time as a default value.
Even so many persons have provided solution but this is just to add more information-
If you just want to insert current timestamp at the time of row insertion-
ALTER TABLE mytable ADD mytimestampcol TIMESTAMP DEFAULT CURRENT_TIMESTAMP;
If you also want that this column should update if any update this row then use this-
ALTER TABLE mytable ADD mytimestampcol TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP;
IMPORTANT EDIT: It is now possible to achieve this with DATETIME
fields since MySQL 5.6.5, take a look at the other post below...
It is now possible to achieve this with DATETIME
fields since MySQL 5.6.5
But you can do it with TIMESTAMP
:
create table test (str varchar(32), ts TIMESTAMP DEFAULT CURRENT_TIMESTAMP)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With