10

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

col1 TIMESTAMP DEFAULT NULL

(i.e. 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?

1
  • If you have just col1 timestamp null, the default null can be omitted (at least according to the documentation). A non-nullable timestamp column has predefined special properties, but they are suppressed when you explicitly make the column nullable. Commented Sep 10, 2012 at 7:19

1 Answer 1

12

The first NULL says that the column is nullable, i.e. 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).

Sign up to request clarification or add additional context in comments.

4 Comments

Can you explain If you only have the default, but make the column reject nulls, then that default cannot be used?
The default value must be a an acceptable value for the column. If you say that the column is not-nullable, you cannot put in NULL. So NULL cannot be used for the default value either.
Do you mean to say NULL can only be inserted if the column accepts NULL?
Yes, you can only insert NULL into a column that is nullable.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.