I can't find what the syntax looks like for adding a DATETIME column to a mysql table when I want to set the default to - example - 2011-01-26 14:30:00
Does anyone know what that syntax looks like?
Here's what I've got
ADD COLUMN new_date DATETIME AFTER preceding_col,
Thanks
One has to just create table with default value as a current datetime. In following example we will first create a sample table and later we will add a column which will be defaulted to the current date time when any new record is inserted.
Syntax. The basic syntax of an ALTER TABLE command to add a New Column in an existing table is as follows. ALTER TABLE table_name ADD column_name datatype; The basic syntax of an ALTER TABLE command to DROP COLUMN in an existing table is as follows.
The DATETIME type is used for values that contain both date and time parts. MySQL retrieves and displays DATETIME values in ' YYYY-MM-DD hh:mm:ss ' format. The supported range is '1000-01-01 00:00:00' to '9999-12-31 23:59:59' . The TIMESTAMP data type is used for values that contain both date and time parts.
If you ever have doubts, the syntax is explained here http://dev.mysql.com/doc/refman/5.5/en/alter-table.html
ALTER TABLE yourTable ADD COLUMN new_date DATETIME NOT NULL DEFAULT 20110126143000 AFTER preceding_col
or
ALTER TABLE yourTable ADD COLUMN new_date DATETIME NOT NULL DEFAULT '2011-01-26 14:30:00' AFTER preceding_col
(I just prefer the numeric DATETIME format)
ALTER TABLE `yourTable` ADD `new_date` DATETIME NOT NULL DEFAULT '2011-01-26 14:30:00' AFTER `preceding_col`
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