一、介绍
一般在数据表中 会添加两个时间字段 一个是创建条数据的时间 一个是最近一次修改这条数据的时间
create_time datetime default current_timestamp comment'创建时间',
update_time datetime default current_timestamp on update current_timestamp comment '最后修改时间'
二、代码示例
-- 日期
use db02;
select database();
create table t_stu(
id int unsigned auto_increment primary key,
name varchar(30),
age tinyint unsigned default 18,
gender enum('男','女') default '男',
birth date default (curdate()),
create_time datetime default current_timestamp comment'创建时间',
update_time datetime default current_timestamp on update current_timestamp comment '最后修改时间'
) comment '学生表';
select *from t_stu;
-- 给表格插入数据
insert into t_stu (name,birth) VALUES ('张三', '2020-01-01');
insert into t_stu (name,birth) values ('李四','2029-07-01');
-- 修改id为1 的数据
update t_stu set name='王五' where id=1;
三、运行结果示例