John
John

Reputation: 952

Add default value for date column

I am trying to add an default value for my date column. The default value should be GETDATE(). I want to do this so I can see the date of the created row. When I try to save it I get the warning: Incorrect default value for date.

Does someone know why I am getting the warning and how I can fix it?

Upvotes: 0

Views: 1250

Answers (1)

Tik
Tik

Reputation: 882

Run this query

ALTER TABLE `YourDb.yourtable`
    CHANGE COLUMN `date` `date` DATETIME DEFAULT CURRENT_TIMESTAMP;

The Change column is oldval newval and the type make sure if your column name isnt date to change it appropriately

additionally if you want that timestamp to update when the row is modified use

CHANGE COLUMN `date` `date` DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP 

Upvotes: 1

Related Questions