1
2
3
4
5
6
7
8
9
10

drop table if exists test;
create table test (
id int auto_increment primary key
,info varchar(1000)
,created_time timestamp default CURRENT_TIMESTAMP comment '创建时间'
,updated_time timestamp default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP comment '更新时间'
);

insert into test (info) value ('test');

如果更新的数据相同,那么updated_time不会更新

1
2
3
4
5
6
7
-- updated_time没有更新
update test set info = 'test'
where id = 1;

-- updated_time更新了
update test set info = 'test1'
where id = 1;

如果修改表结构,增加字段/删除字段不会更新

1
alter table test add (name varchar(100) default 'test');

如果不想更新更新字段,请使用 updated_time = updated_time

1
2
update test set info = 'test',updated_time = updated_time
where id = 1;

如果有事务,那么更新时间是该记录的更新时间,不是该事务的提交时间

  1. 如果事务在14:00:00 开启
  2. 记录在14:00:01更新
  3. 事务在14:01:00 提交

那么数据的的更新时间是14:00:01,而不是14:01:00