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

相关推荐
字节跳动开源2 小时前
火山引擎开源 Agent 驱动的搜索自迭代技术
数据库·开源·agent
Oo大司命oO4 小时前
藏在正则表达式里的陷阱
数据库·mysql·正则表达式
_oP_i5 小时前
mysql统计数据库使用存储大小
数据库
会编程的土豆5 小时前
MySQL 入门:库、表、行、主键是什么
linux·数据库·网络协议·http
jjjava2.05 小时前
系统日志:从入门到精通的完整指南
网络·数据库
写代码的强哥5 小时前
云原生数据库与传统数据库相比“新”在哪里
数据库·云原生·架构
许彰午6 小时前
05-十分钟读懂AWR报告
oracle
吴声子夜歌7 小时前
MongoDB 4.2——查询
数据库·mongodb
三十岁老牛再出发7 小时前
07.26每日总结
linux·c语言·mysql