4

can somebody please explain why the following is happening? When I run this statement:

CREATE TABLE `MyTable2` (
   `id` int(10) unsigned NOT NULL,
   `start` timestamp NOT NULL,
   `cTime` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
   PRIMARY KEY (`id`)
);

I get my table created. But when I run this one:

CREATE TABLE `MyTable2` (
   `id` int(10) unsigned NOT NULL,
   `start` timestamp NOT NULL,
   `end` timestamp NOT NULL,
   `cTime` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
   PRIMARY KEY (`id`)
);

I get an error stating:

[42000][1067] Invalid default value for 'end'

What am I missing? Is this the expected behaviour?

[Update]: It seems that there's a filed bug here https://bugs.mysql.com/bug.php?id=80163 but I can not make anything out of the description. Silencing the warnings and/or changing the sql_mode is not a solution as it changes the semantics of the created table (two timestamp columns not null with no default).

[Update]: The sql_mode on my server is as follows:

ONLY_FULL_GROUP_BY,NO_AUTO_VALUE_ON_ZERO,STRICT_TRANS_TABLES,
NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,
NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION

[Solution/Explanation]: Turns out that here https://dev.mysql.com/doc/refman/5.7/en/server-system-variables.html#sysvar_explicit_defaults_for_timestamp this behaviour is explained in detail. In order to get the expected semantics, I had to enable sysvar_explicit_defaults_for_timestamp in my server's configuration.

7
  • 1
    @frlan it is escaped, so that's not an issue Commented Feb 3, 2017 at 9:11
  • @OP: It is due to Automatic Initialization and Updating for TIMESTAMP and DATETIME. Commented Feb 3, 2017 at 9:18
  • @RavinderReddy So what is the solution? Cause reading that leads me to think that it is a bug. Commented Feb 3, 2017 at 9:27
  • I just visited link you provided, and observe that this may be cause by SQL Mode, try to Set SQL mode as set @@sql_mode = '' and then execute your CREATE TABLE command, this may resolve your issue Commented Feb 3, 2017 at 9:34
  • @JaydipJ In that link the sql statement is CREATE TABLE annotation (`created` TIMESTAMP NOT NULL, `ts` TIMESTAMP NOT NULL); and the resulting table has defaults. Commented Feb 3, 2017 at 9:35

1 Answer 1

6

I agree that this is a bit hidden and mysql could be more explicit about this. Mysql manual on timestamp initialisation says:

With an ON UPDATE CURRENT_TIMESTAMP clause but no DEFAULT clause, the column is automatically updated to the current timestamp but does not have the current timestamp for its default value.

The default in this case is type dependent. TIMESTAMP has a default of 0 unless defined with the NULL attribute, in which case the default is NULL.

The highlighted section applies to you: the implicit default is 0 from the 2nd timestamp field on, which is the same as '0000-00-00 00:00:00'. You have strict and no zero date sql modes enabled that forbid this default value.

However, the 1st timestamp field is assigned current_timestamp() as default (it is treated specially), this is why the 1st query passes with your current sql mode settings.

Either make the 2nd timestamp field nullable or change your sql mode settings. The former is the preferred approach.

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

7 Comments

I am obviously missing something fundamental here. When I create a table like this CREATE TABLE ``MyTable2`` (``id`` int(10) unsigned NOT NULL, ``start`` timestamp NOT NULL, PRIMARY KEY (``id``)); and then I issue an insert statement like this insert into MyTable2 (id) VALUES (2); shouldn't I get an error? As the link you provided implies, there's always a default for the timestamp field.
I added a few more sentences a couple of minutes ago to my answer that explain your question in the comment.
I am upvoting your answer because it made me dig a lot and gain an understanding on what's going on. Finally I found this dev.mysql.com/doc/refman/5.7/en/… which explains and solves my problem.
Pls read the linked documentation very carefully: the referenced setting has already been deprecated. It is not recommended to rely on such feature.
I'm sorry, but this was not your question! You asked for an explanation to the behaviour and that's what I provided. You treat your own solution as an answer to a question you did not even ask.
|

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.