An existing MySQL table has a DateTime field which is not null and having a Default Value set as '0001-00-00 00:00:00'. Is it possible to Alter this table to remove the default value for the DateTime field ?
Asked
Active
Viewed 1.9k times
30
-
I found this for you : http://stackoverflow.com/questions/11312433/how-to-alter-a-column-and-change-the-default-value – AchrafGounane Jan 23 '17 at 16:18
3 Answers
48
Yes, you can drop the default using an ALTER TABLE
statement like this:
alter table your_table
alter column your_column drop default;

Ike Walker
- 64,401
- 14
- 110
- 109
8
To drop the default from multiple datetime columns in a table:
ALTER TABLE your_table
ALTER COLUMN columnname1 DROP DEFAULT,
ALTER COLUMN columnname2 DROP DEFAULT,
ALTER COLUMN columnname3 DROP DEFAULT,
....

NullReference
- 2,828
- 2
- 30
- 33
0
You can simply change the column and exclude default clause using following query. Here T1
is table containing default value for column updated and subject. You can simply remove default value as given below:
ALTER TABLE `T1`
CHANGE `updated` `updated` DATETIME NOT NULL,
CHANGE `subject` `subject` VARCHAR(100)

Gass
- 7,536
- 3
- 37
- 41

Sumit Jangir
- 51
- 4