昨天我要创建一个表,里面包含两个日期时间字段,一个用来存放记录创建时间,一个用来存放记录最后被访问时间,所以我用了timestamp类型,其实用datetime也是可以的,只是没有timestamp方便,用datetime不能函数为属性设置默认值,需要写两个触发器才行,而timestamp只要写一个触发器就行了。如下:
create table post(
pid int not null auto_increment,
ctime timestamp default 0,//记录插入时间
atime timestamp default current_timestamp on update current_timestamp//记录最后访问时间
primary key(pit)
)
触发器:
create trigger p_time
before insert on post
set ctime=current_timestamp
在这里不能用after,只能用before,不太清楚为什么。