Method of setting default value for datetime type in mysql

  • 2021-09-25 00:00:32
  • OfStack

A problem was encountered while modifying the datetime default value through the navicat client.

The database table field type datetime was originally defaulted to NULL. When the default value is set to the current time through the interface, it will prompt "1067-Invalid default value for 'CREATE_TM'", but this problem will not occur when building a table, such as the table building statement:


CREATE TABLE `app_info1` ( 
 `id` bigint(21) unsigned NOT NULL AUTO_INCREMENT COMMENT ' Primary key ID', 
 `app_id` varchar(30) DEFAULT NULL COMMENT ' Applied coding ', 
 `app_name` varchar(30) DEFAULT NULL COMMENT ' Application name ', 
 `package_name` varchar(100) DEFAULT NULL COMMENT ' Package name ', 
 `version` int(11) DEFAULT NULL COMMENT ' Version number ', 
 `version_name` varchar(20) DEFAULT NULL COMMENT ' Version name ', 
 `icon_url` varchar(512) DEFAULT NULL COMMENT 'Icon Address ', 
 `download_url` varchar(512) DEFAULT NULL COMMENT ' Download address ', 
 `summary` varchar(512) DEFAULT NULL COMMENT ' Summary ', 
 `desc` varchar(512) DEFAULT NULL COMMENT ' Descriptive information ', 
 `app_status` int(4) DEFAULT '0' COMMENT ' Status  0 : Available, 1 : Delete ', 
 `create_time` datetime DEFAULT CURRENT_TIMESTAMP COMMENT ' Creation time ', 
 `create_user` varchar(30) DEFAULT NULL COMMENT ' Founder ', 
 `modify_time` datetime DEFAULT CURRENT_TIMESTAMP COMMENT ' Modification time ', 
 `modify_user` varchar(30) DEFAULT NULL COMMENT ' Modifier ', 
 PRIMARY KEY (`id`) 
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8; 

Then I tried 1 to modify the field directly through script, which is also OK, as follows:


alter table ts_role 
MODIFY column create_tm datetime default CURRENT_TIMESTAMP COMMENT ' Creation time ' 

Of course, the Mysql I installed is 5.6 or above, and other versions have not been tried.


Related articles: