mysql怎么设置自动更新时间

mysql只有timestamp可以使用时间函数,但是在5.5版本中不支持一张表中有两个timestamp。我们可以使用触发器来解决这个问题!

#创建表,设置默认值

create table tie(
ts_time timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
ts_timeq timestamp NOT NULL DEFAULT  CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
ss int 
);

#创建触发器

CREATE  TRIGGER update_tim BEFORE insert  ON tie
 FOR EACH ROW SET NEW.ts_time = NOW();

#增加一条数据

insert into tie(ss)
value (2);

#修改数据
update  tie set ss=6
select * from tie;

#注意:当添加数据是,两个时间列的时间一致,当数据发生改变时,第二个列的数据仍然取当前系统时间,而第一个列是刚插入数据时的时间

猜你喜欢

转载自blog.csdn.net/QQ17680473835/article/details/81986294