首页 > 数据库 >MySQL中create_time 和 update_time实现自动更新时间

MySQL中create_time 和 update_time实现自动更新时间

时间:2024-01-17 23:11:10浏览次数:22  
标签:01 17 22 create update 自动更新 time

也是最近在捣鼓前后端分离项目, 在写后端接口的时候便设计到数据库表建设, 这里规范显得很重要.

通常的建表规范, 必备三字段:idcreate_timeupdate_time.

  • id 必为主键,类型为 bigint unsigned、单表时自增、步长为 1

  • create_time 类型为 datetime, 数据新增时自动创建

  • update_time 类型为 datetime, 数据更新时被动式更新

drop table if exists test;
create table test (
  id int unsigned primary key auto_increment comment 'id'
  , name varchar(50) not null comment '名称'
  , create_time datetime not null default current_timestamp comment '创建时间'
  , update_time datetime not null default current_timestamp on update current_timestamp comment '更新时间'
) charset=utf8 comment '测试表';

写入两条数据:

# 插入测试
insert into test(name) values ('张三'), ('李四');

然后查询该表, 这时候可以看到 id, create_time, update_time 都自动有值了

select * from test;


id	name	create_time	update_time
1	张三	2024-01-17 22:49:36.0	2024-01-17 22:49:36.0
2	李四	2024-01-17 22:49:36.0	2024-01-17 22:49:36.0

再来验证 update 修改其中的数据

# 更新第二条数据的值
update test set name = '杰哥' where id = 2;

然后再来查询即可看到自动更新:

select * from test;

id	name	create_time	update_time
1	张三	2024-01-17 22:49:36.0	2024-01-17 22:49:36.0
2	杰哥	2024-01-17 22:49:36.0	2024-01-17 22:55:07.0

nice !

标签:01,17,22,create,update,自动更新,time
From: https://www.cnblogs.com/chenjieyouge/p/17971440

相关文章

  • 【Azure Function】在Function执行中遇见Timeout错误
    问题描述在Function执行中遇见Timeout错误: Microsoft.Azure.WebJobs.Host.FunctionTimeoutException/Timeoutvalueof00:30:00wasexceededbyfunction/Functions.TimerTrigger_UdeskContact    asyncMicrosoft.Azure.WebJobs.Host.Executors.FunctionExecutor.TryHa......
  • Kubernetes集群中 Pod 中报pthread_create failed: Resource temporarily unavailable
    查看containerd日志显示如下信息OCIruntimeexecfailed:execfailed:unabletostartcontainerprocess:readinit-p:connectionresetbypeer:unknown"查看Pod日志中显示pthread_createfailed:Resourcetemporarilyunavailable通过监控系统查看节点的进程数t......
  • SQL中的unix_timestamp()函数
    unix_timestamp()是SQL中用于将日期和时间转换为UNIX时间戳的函数。UNIX时间戳是指从1970年1月1日(UTC)开始的秒数。使用场景:时间戳转换:当你有一个日期和时间,并希望将其转换为UNIX时间戳格式时。数据整合:在数据整合过程中,你可能需要将来自不同源的数据统一到相同的......
  • git pull 报错:ssh: connect to host github.com port 22: Connection timed out
    在执行gitpull时提示错误:ssh:connecttohostgithub.comport22:Connectiontimedout解决方案:添加config文件vim~/.ssh/config添加如下内容Hostgithub.comHostnamessh.github.comPort443ssh:connecttohostgithub.comport22:Connectionti......
  • C++:GDAL中CreateCopy()函数生成的栅格图像如何修改波段数?
      本文介绍基于C++语言GDAL库,为CreateCopy()函数创建的栅格图像添加更多波段的方法。  在C++语言的GDAL库中,我们可以基于CreateCopy()函数与Create()函数创建新的栅格图像文件。其中,CreateCopy()函数需要基于一个已有的栅格图像文件作为模板,将模板文件的各项属性信息(例如空......
  • 无涯教程-SQL - Create Table Using another Table.函数
    可以使用CREATETABLE语句和SELECT语句的组合来创建现有表的副本。新表具有相同的列定义。可以选择所有列或特定列。当您使用现有表创建新表时,将使用旧表中的现有值填充新表。语法从另一个表创建表的基本语法如下:CREATETABLENEW_TABLE_NAMEASSELECT[column1,colu......
  • Flink自定义Assigning Timestamps和Watermarks 使用Scal语言
    Flink自定义AssigningTimestamps和Watermarks使用Scal语言为了让eventtime工作,Flink需要知道事件的时间戳,这意味着流中的每个元素都需要分配其事件时间戳。这个通常是通过抽取或者访问事件中某些字段的时间戳来获取的。时间戳的分配伴随着水印的生成,告诉系统事件时间中的......
  • openGauss学习笔记-199 openGauss 数据库运维-常见故障定位案例-Lock wait timeout
    openGauss学习笔记-199openGauss数据库运维-常见故障定位案例-Lockwaittimeout199.1执行SQL语句时,提示Lockwaittimeout199.1.1问题现象执行SQL语句时,提示“Lockwaittimeout”。ERROR:Lockwaittimeout:thread140533638080272waitingforShareLockonrelat......
  • vscode报错Pylance client: couldn‘t create connection to server.解决
    问题描述:一打开vscode,右下角就弹报错,Pylanceclient:couldn’tcreateconnectiontoserver.,让我打开output,打开后似乎是在说连不上server因为连不上server,所以我的python代码没法解析,尝试重开vscode也没用问题解决:点开左侧的拓展,找到PythonExtensionPack,这就是解析python代......
  • 解决Github port443:Timed out
    Failedtoconnecttogithub.comport443:Timedout修改Git的网络设置注意修改成自己的代理的IP和端口号gitconfig--globalhttp.proxyhttp://127.0.0.1:7890gitconfig--globalhttps.proxyhttp://127.0.0.1:7890取消代理是因为,访问Gitee或其它是不需要梯子,所......