0

MYSQL VERSION - 5.7.14

In mysql version I am not able to insert timestamp value with null in insert query.

I tried to alter the timestamp coulmn in mysql with the below query:

ALTER TABLE `business` MODIFY `created` TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL;
ALTER TABLE `business` MODIFY `modified` TIMESTAMP on update CURRENT_TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL;

But I am getting the error #1067 - Invalid default value for 'modified'

Joshi
  • 2,730
  • 5
  • 36
  • 62

1 Answers1

0

This should work:

// TO ALLOW NULL AND SET CURRENT_TIMESTAMP AS DEFAULT:
ALTER TABLE `business` MODIFY `created` TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP;
// TO ALLOW NULL AND SET CURRENT_TIMESTAMP AS DEFAULT AND ON UPDATE:  
ALTER TABLE `business` MODIFY `modified` TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP;

If you do not want to allow null then just remove the NULL:

// DO NOT ALLOW NULL, SET CURRENT_TIMESTAMP AS DEFAULT:
ALTER TABLE `business` MODIFY `created` TIMESTAMP DEFAULT CURRENT_TIMESTAMP;
// DO NOT ALLOW NULL, SET CURRENT_TIMESTAMP AS DEFAULT AND ON UPDATE:  
ALTER TABLE `business` MODIFY `modified` TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP; 
Michael Krikorev
  • 2,126
  • 1
  • 18
  • 25