mysql sets the default value default for the datetime type field

 

The operating server environment is Linux centos5.7

The installed mysql version of MariaDB, the figure is simple and straightforward to use the command install.

 

There is no problem in modifying the Alter statement in the test database on your own mac.

 

ALTER TABLE `XXX`.`XXX` ADD COLUMN `createtime` datetime NOT NULL DEFAULT NOW() AFTER `userinfo`;

 

But modifying the operation on the server prompts an error:

 

ERROR 1067 (42000): Invalid default value for 'createtime'

 

I checked a lot and said that the sql_mode setting is wrong. I checked that there is no setting and it is empty.

But I found a problem, that is, the mysql version is

MariaDB [XXX]> show variables like 'version';

+---------------+----------------+

| Variable_name | Value          |

+---------------+----------------+

| version       | 5.5.52-MariaDB |

+---------------+----------------+

The test machine environment is version 5.7.17.

 

embarrassment """ After checking the information, I learned that the datetime type only supports MySQL 5.6.5 and above .

 

Modify the test statement --- replace  datetime  with  TIMESTAMP  :

 

ALTER TABLE `XXX`.`XXX` MODIFY COLUMN `createtime` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP;

 +------------+--------------+------+-----+-------------------+----------------+

| Field      | Type         | Null | Key | Default           | Extra          |

+------------+--------------+------+-----+-------------------+----------------+

| id         | int(11)      | NO   | PRI | NULL              | auto_increment |

| createtime | timestamp    | NO   |     | CURRENT_TIMESTAMP |   |

+------------+--------------+------+-----+-------------------+----------------+

Also, the "DEFAULT NOW()" command line is not very easy to use, but the client side executes fine

 

 

refer to: 

http://stackoverflow.com/questions/168736/how-do-you-set-a-default-value-for-a-mysql-datetime-column/10603198

 

Original link: http://zl378837964.iteye.com/blog/2368532

 

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326337675&siteId=291194637