進入產品迭代階段,修改數據庫字段是不免的。除了咱們可以想到的,新增字段,必定要設置爲「容許爲空」或者設置「默認值」,當涉及到mysql的時間字段時,也要格外當心。java
下面是報錯:mysql
java.sql.SQLException: Value '0000-00-00 00:00:00' can not be represented as java.sql.Timestamp Query
CREATE TABLE `a` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT, `name` varchar(100) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO a(id, name) VALUES(1, "張三"); INSERT INTO a(id, name) VALUES(2, "李四");
ALTER Table a ADD COLUMN create_time timestamp not null DEFAULT CURRENT_TIMESTAMP; ALTER Table a ADD COLUMN update_time timestamp not null ON UPDATE CURRENT_TIMESTAMP;
INSERT INTO a(id, name) VALUES(3, "王五");
UPDATE a SET name="馬六" WHERE id = 3;