1.mysql
自动获取创建时间:
timestamp not null default current_timestamp
自动获取更新时间:
timestamp not null default current_timestamp on update current_timestamp
1.1.建表语句
create table test( id integer not null auto_increment primary key, name varchar(20) not null , create_time timestamp not null default current_timestamp comment '创建时间', update_time timestamp not null default current_timestamp on update current_timestamp comment '更新时间');
2. oracle
2.1.建表语句
create table userinfo( id varchar2(32) not null primary key, name varchar2(200) , age int , create_time date default sysdate, modify_time date default sysdate ) insert 时可以自动更新 create_time 字段,update时需要设置触发器,才能自动更新时间。
2.2.利用触发器设置更新时间
create or replace trigger userinfo_trigger before update on userinfo for each row begin :new.modify_time := sysdate; end;
userinfo:是表名modify_time:执行update需要自动更新的字段
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持代码网。
发表评论