Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL - Remove default value for Datetime field

Tags:

datetime

mysql

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 ?

like image 539
NullReference Avatar asked Jan 23 '17 16:01

NullReference


People also ask

How do I change the default value in MySQL?

To change a default value, use ALTER col_name SET DEFAULT : ALTER TABLE mytbl ALTER j SET DEFAULT 1000; Default values must be constants. For example, you cannot set the default for a date-valued column to NOW( ) , although that would be very useful.

How do I change the default value to not null in MySQL?

To enforce NOT NULL for a column in MySQL, you use the ALTER TABLE .... MODIFY command and restate the column definition, adding the NOT NULL attribute.

How do I change my default value?

Set a default valueRight-click the control that you want to change, and then click Properties or press F4. Click the All tab in the property sheet, locate the Default Value property, and then enter your default value.

How do I drop a default constraint?

Syntax: ALTER TABLE <table_name> ALTER COLUMN <column_name> DROP DEFAULT; Example: Lets say we want to drop the constraint from STUDENTS table, which we have created in the above sections.


2 Answers

Yes, you can drop the default using an ALTER TABLE statement like this:

alter table your_table    alter column your_column drop default; 
like image 169
Ike Walker Avatar answered Oct 01 '22 02:10

Ike Walker


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,    .... 
like image 27
NullReference Avatar answered Oct 01 '22 02:10

NullReference