简体   繁体   中英

Why does the TIMESTAMP column require “NULL” to accept null values?

I've googled this for a bit and can't find anything on it.

Why does the MySQL column type "TIMESTAMP" require the "NULL" parameter to accept null values, when other column types dont?

Also, is this the only column type that requires the "NULL" parameter to accept null values?

Thank you.

This is related to a system variable that was added in 5.6.6 and later, explicit_defaults_for_timestamp . The default behavior is:

  • TIMESTAMP columns not explicitly declared with the NULL attribute are assigned the NOT NULL attribute. (Columns of other data types, if not explicitly declared as NOT NULL, permit NULL values.) Setting such a column to NULL sets it to the current timestamp.

  • The first TIMESTAMP column in a table, if not declared with the NULL attribute or an explicit DEFAULT or ON UPDATE clause, is automatically assigned the DEFAULT CURRENT_TIMESTAMP and ON UPDATE CURRENT_TIMESTAMP attributes.

  • TIMESTAMP columns following the first one, if not declared with the NULL attribute or an explicit DEFAULT clause, are automatically assigned DEFAULT '0000-00-00 00:00:00' (the “zero” timestamp). For inserted rows that specify no explicit value for such a column, the column is assigned '0000-00-00 00:00:00' and no warning occurs.

Setting this variable makes TIMESTAMP columns behave like other columns. The plan is that in some future release the non-standard behavior will be removed entirely, and the variable will then be deprecated.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM