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 的时间类型字段自动更新

相关推荐
编程小Y14 分钟前
MySQL原理
数据库·mysql
小石头 1008641 分钟前
MySQL 视图:把复杂变简单的“虚拟化”艺术
数据库·mysql
安当加密1 小时前
PostgreSQL 透明数据加密(TDE)方案与应用场景详解
数据库·postgresql
怪我冷i1 小时前
dbeaver如何连接PostgreSQL数据库
数据库·ai编程·ai写作
QH_ShareHub1 小时前
如何使用 NHANES 数据库
数据库
wuhen_n1 小时前
系统架构设计师(三):数据库系统
数据库·系统架构
DB虚空行者2 小时前
MySQL误删/批量更新数据恢复实战:基于Flashback工具的完整方案
数据库·mysql
IvorySQL2 小时前
外键的本质竟然是触发器?深入解析 PostgreSQL 约束底层
数据库·postgresql·开源
九皇叔叔2 小时前
MySQL Next-Key Lock 锁表事故全拆解(从现象到根治)
数据库·mysql
短剑重铸之日2 小时前
7天读懂MySQL|Day 4:锁与并发控制
数据库·mysql·架构