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

相关推荐
StarRocks_labs1 小时前
StarRocks Community Monthly Newsletter (Jun)
数据库·starrocks·数据湖·物化视图·存算分离
光电的一只菜鸡2 小时前
ubuntu之坑(十五)——设备树
linux·数据库·ubuntu
ob熔天使——武3 小时前
MySQL
数据库·mysql
小光学长3 小时前
基于vue框架的防疫物资仓库管理系统09y38(程序+源码+数据库+调试部署+开发环境)带论文文档1万字以上,文末可获取,系统界面在最后面。
数据库
野生技术架构师7 小时前
MySQL数据实时同步到Elasticsearch的高效解决方案
数据库·mysql·elasticsearch
白仑色7 小时前
Oracle 高可用性与安全性
数据库·oracle·数据安全·goldengate·高可用架构
紫无之紫8 小时前
SQL性能调优经验总结
数据库·sql·性能调优
CZZDg8 小时前
Redis Sentinel哨兵集群
java·网络·数据库
__风__8 小时前
PostgreSQL ExecInitIndexScan 函数解析
数据库·postgresql
小云数据库服务专线8 小时前
GaussDB in的用法
数据库·sql·gaussdb