简体   繁体   中英

MYSQL query to alter timestamp column

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'

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; 

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