mysql自动管理数据的创建时间和更新时间

本文详细介绍如何在MySQL中创建包含自动更新时间戳字段的表,通过实例展示在插入和更新数据时,如何利用默认current_timestamp和on update current_timestamp属性自动记录创建时间和更新时间,免去手动维护时间戳的烦恼。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

首先在创建表的时候

create table testmysqltime(

    -> id int primary key auto_increment comment '主键id',

    -> username varchar(10) not null comment '用户名',

    -> password varchar(10) not null comment '用户密码',

    -> createtime timestamp not null default current_timestamp comment '创建时间',

    -> updatetime timestamp not null default current_timestamp on update current_timestamp comment '更新时间') comment '测试时间表';

在插入数据的时候 :

insert into testmysqltime (username, password) values ("admin", "123456");

这样我们在操作新增数据的时候 就不用考虑关于数据创建的时间的问题了。

在更新数据的时候

update testmysqltime set password="456789" where id=1;

这样我们在操作更新数据的时候 就不用考虑关于数据创建的时间的问题了。

注意的是:同一数据多次更改 更改的数据是相同的  那 更新的时间是不变化的

例如:update testmysqltime set password="456789" where id=1;  执行多次  更新时间还是第一次执行的时候的更新时间,除非你更改数据  把password="4567890"进行更改  在执行 更新时间就是当前时间了

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值