0

Mysql version is 5.1.73,there can be only one timestamp column with current_timestamp in deafault or on update clause,so I tried to create table:

create table `temp`( 
  `id` INTEGER NOT NULL auto_increment PRIMARY KEY, 
  `created_date` TIMESTAMP NOT NULL DEFAULT '0000-00-00 00:00:00',
  `last_updated_date` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);

And after I executed this sql:

insert into `temp`(id,created_date, last_updated_date) values(1,null, null); 

I got this document:

1   2017-02-10 18:17:16 2017-02-10 18:18:13

Here is my question:

  • I set created_date NOT NULL,why did the insert sql work well?Should it return Error: Column 'xx' cannot be null
  • Why did created_date column get a value and it is not the default value?

Thanks.

1
  • NULL !=0; NULL <> 0; NULL=??????????? Your default is a value NULL has no value. Commented Feb 10, 2017 at 11:35

1 Answer 1

1

That's how the MySql's TIMESTAMP work, if you want to get an error on NULL the way to go is to use DATETIME instead.

So if you create the following table

create table `temp`( 
  `id` INTEGER NOT NULL auto_increment PRIMARY KEY, 
  `created_date` DATETIME NOT NULL DEFAULT '0000-00-00 00:00:00',
  `last_updated_date` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);

And then execute the below query you'll get the error.

insert into `temp`(id,created_date, last_updated_date) values(1,null, null); 

Now you'll start getting this error #1048 - Column 'created_date' cannot be null.

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

Comments

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.