MySQL、Oracle的时间类型字段自动更新:insert插入、update更新时,自动更新时间戳。设置自增主键id,oracle创建自增id序列和触发器

1. MySQL

  • 支持设置自增id的字段类型:int、bigint、double等数值类型,一般用int、bigint
  • 支持设置自动更新时间的字段类型:datetimetimestamp
  • 下面sql中的now()函数可以用current_timestamp()替代

1.1. 不指定秒精度

sql 复制代码
drop table if exists demo;
create table demo
(
    id         bigint auto_increment primary key       comment '自增id',
    name       varchar(8)                              comment '姓名',
    datetime1  datetime  default now()                 comment 'insert        时,更新时间',
    datetime2  datetime                on update now() comment '       update 时,更新时间',
    datetime3  datetime  default now() on update now() comment 'insert/update 时,更新时间',
    timestamp1 timestamp default now()                 comment 'insert        时,更新时间',
    timestamp2 timestamp               on update now() comment '       update 时,更新时间',
    timestamp3 timestamp default now() on update now() comment 'insert/update 时,更新时间'
) comment = '测试自动更新时间';

1.2. 指定秒精度为3

sql 复制代码
drop table if exists demo;
create table demo
(
    id         bigint auto_increment primary key            comment '自增id',
    name       varchar(8)                                   comment '姓名',
    datetime1  datetime(3)  default now(3)                  comment 'insert        时,更新时间',
    datetime2  datetime(3)                 on update now(3) comment '       update 时,更新时间',
    datetime3  datetime(3)  default now(3) on update now(3) comment 'insert/update 时,更新时间',
    timestamp1 timestamp(3) default now(3)                  comment 'insert        时,更新时间',
    timestamp2 timestamp(3)                on update now(3) comment '       update 时,更新时间',
    timestamp3 timestamp(3) default now(3) on update now(3) comment 'insert/update 时,更新时间'
) comment = '测试自动更新时间';

1.3. 测试

1.4.1. 自动更新时间

1.4.2. 自增Id

9.参考

MYsql 和Oracle 的时间类型字段自动更新

相关推荐
柠檬汁Dev11 分钟前
还在等DBA给你库?我3分钟就拉起一个高可用集群
数据库·云计算·dba
安卓开发者18 分钟前
鸿蒙NEXT应用数据持久化全面解析:从用户首选项到分布式数据库
数据库·分布式·harmonyos
少男的脸红藏不住心事1 小时前
GD32入门到实战35--485实现OTA
数据库·mongodb·nosql
爬山算法2 小时前
Redis(50) Redis哨兵如何与客户端进行交互?
数据库·redis·交互
007php0072 小时前
某大厂MySQL面试之SQL注入触点发现与SQLMap测试
数据库·python·sql·mysql·面试·职场和发展·golang
Craze_rd2 小时前
腾讯云TDSQL-C 与传统MySQL对比
mysql·云计算·腾讯云
赛姐在努力.2 小时前
Spring DI详解--依赖注入的三种方式及优缺点分析
java·mysql·spring
YS_Geo2 小时前
Redis 深度解析:数据结构、持久化与集群
数据结构·数据库·redis
IvanCodes2 小时前
六、Docker 核心技术:Dockerfile 指令详解
java·数据库·docker
寻星探路2 小时前
数据结构造神计划第三天---数据类型
数据库