Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does the Alter Table syntax look like for adding a DATETIME column?

Tags:

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

like image 937
H. Ferrence Avatar asked Jan 26 '11 21:01

H. Ferrence


People also ask

How do I add a column to a datetime table in SQL?

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.

How do you add a column to an ALTER TABLE?

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.

What is a datetime column?

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.


2 Answers

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)

like image 110
Mchl Avatar answered Oct 09 '22 16:10

Mchl


ALTER TABLE  `yourTable` ADD `new_date` DATETIME NOT NULL DEFAULT '2011-01-26 14:30:00' AFTER `preceding_col` 
like image 37
Rocket Hazmat Avatar answered Oct 09 '22 16:10

Rocket Hazmat