How do you set a default value for a MySQL Datetime column?

后端 未结 26 2213
野的像风
野的像风 2020-11-22 01:55

How do you set a default value for a MySQL Datetime column?

In SQL Server it\'s getdate(). What is the equivalant for MySQL? I\'m using MySQL 5.x if tha

26条回答
  •  遇见更好的自我
    2020-11-22 02:35

    I'm running MySql Server 5.7.11 and this sentence:

    ALTER TABLE table_name CHANGE date_column datetime NOT NULL DEFAULT '0000-00-00 00:00:00'
    

    is not working. But the following:

    ALTER TABLE table_name CHANGE date_column datetime NOT NULL DEFAULT '1000-01-01 00:00:00'
    

    just works.

    As a sidenote, it is mentioned in the mysql docs:

    The DATE type is used for values with a date part but no time part. MySQL retrieves and displays DATE values in 'YYYY-MM-DD' format. The supported range is '1000-01-01' to '9999-12-31'.

    even if they also say:

    Invalid DATE, DATETIME, or TIMESTAMP values are converted to the “zero” value of the appropriate type ('0000-00-00' or '0000-00-00 00:00:00').

提交回复
热议问题