MYSQL设计表时,需要 两个TIMESTAMP 字段的情况 有时候,数据库表有这样的需求,要一个记录创建时间,一个记录修改时间。 理想中的设计是这样的,更新时间的初始值和创建时间一样: CREATE TABLE `test_table` (`id` INT( 10 ) NOT NULL,`create_time` TIMEST
MYSQL设计表时,需要 两个TIMESTAMP 字段的情况CREATE TABLE `test_table` ( `id` INT( 10 ) NOT NULL, `create_time` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, `update_time` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP ) ENGINE = INNODB;
CREATE TABLE `test_table` ( `id` INT( 10 ) NOT NULL, `create_time` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, `update_time` TIMESTAMP ON UPDATE CURRENT_TIMESTAMP ) ENGINE = INNODB;
Incorrect table definition; there can be only one TIMESTAMP column with CURRENT_TIMESTAMP in DEFAULT or ON UPDATE clause
CREATE TABLE `test_table` ( `id` INT( 10 ) NOT NULL, `create_time` TIMESTAMP NOT NULL DEFAULT 0, `update_time` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP ) ENGINE = INNODB;
INSERT INTO test_table (id, create_time, update_time) VALUES (1, NULL, NULL);
INSERT INTO test_table (id, update_time) VALUES (1, NULL);
UPDATE test_table (id) VALUES (2);