Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL default datetime through phpmyadmin

In an existing database, I've age column (INT). Now I need to set it as dob (DATETIME).

I try doing so through PHPMyAdmin, giving CURRENT_TIMESTAMP as default value as defined by answer with 138 upvotes. However PHPMyAdmin is complaining #1067 - invalid default value for 'dob' as in attached screenshot:

Error screenshot

Can someone please suggest why I'm getting that error and how to fix that?

like image 229
Kapil Sharma Avatar asked Oct 25 '12 07:10

Kapil Sharma


People also ask

What is the default value for datetime in MySQL?

DATETIME has a default of NULL unless defined with the NOT NULL attribute, in which case the default is 0.

How do I set the current time in phpMyAdmin?

You can't set CURRENT_TIMESTAMP as default value with DATETIME. But you can do it with TIMESTAMP. See the difference here. The DEFAULT value clause in a data type specification indicates a default value for a column.

How do I add a default value to a TIMESTAMP column?

Use of DEFAULT CURRENT_TIMESTAMP and ON UPDATE CURRENT_TIMESTAMP is specific to TIMESTAMP. The DEFAULT clause also can be used to specify a constant (nonautomatic) default value; for example, DEFAULT 0 or DEFAULT '2000-01-01 00:00:00'.


2 Answers

You can't set CURRENT_TIMESTAMP as default value with DATETIME.

But you can do it with TIMESTAMP.

See the difference here.

Words from this blog

The DEFAULT value clause in a data type specification indicates a default value for a column. With one exception, the default value must be a constant; it cannot be a function or an expression.

This means, for example, that you cannot set the default for a date column to be the value of a function such as NOW() or CURRENT_DATE.

The exception is that you can specify CURRENT_TIMESTAMP as the default for a TIMESTAMP column.

like image 100
Mithun Sreedharan Avatar answered Oct 17 '22 23:10

Mithun Sreedharan


Set the type of the field as TIMESTAMP too.

enter image description here

like image 4
Ashutosh Bajpai Avatar answered Oct 17 '22 22:10

Ashutosh Bajpai