简体   繁体   中英

Difference between NULL DEFAULT NULL and DEFAULT NULL in MySQL?

Recently I faced a new problem in MySQL. I was about to create a new table with

col1 TIMESTAMP DEFAULT NULL

(ie the column having the default NULL value), but on creation that gave me an error:

Invalid default value for column

But when I tried col1 TIMESTAMP NULL DEFAULT NULL , that table got created.

I want to know what is the difference between the above two syntaxes. I also faced this issue earlier too in some insert NULL values in column.

Can any one explain the cause of this problem, like is it a version specific issue or something else with MySQL?

The first NULL says that the column is nullable , ie accepts NULL . The second NULL (after DEFAULT ) is the default value.

If you only have the default, but make the column reject nulls, then that default cannot be used.

(I was under the impression, though, that if you specify neither NULL nor NOT NULL that the column was nullable, so what you see is a bit confusing).

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