您好,欢迎访问一九零五行业门户网

MYSQL设计表时,亟需 两个TIMESTAMP 字段的情况

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;
这样的建表sql,是执行不了的,执行时报错,估计这个很多人遇到过吧:
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语句这样写:
insert into test_table (id, create_time, update_time) values (1, null, null);
或者这样写(注意,没有写create_time):
insert into test_table (id, update_time) values (1, null);
update语句正常写法(假设test_table.id可以修改):
update test_table (id) values (2);
其它类似信息

推荐信息