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

相关推荐
乡野码圣21 分钟前
【RK3588 Android12】RCU机制
java·jvm·数据库
亓才孓35 分钟前
[数据库]应该注意的细节
数据库·sql
m0_561359672 小时前
掌握Python魔法方法(Magic Methods)
jvm·数据库·python
xxxmine2 小时前
redis学习
数据库·redis·学习
qq_5470261792 小时前
Redis 常见问题
数据库·redis·mybatis
APIshop2 小时前
Java 实战:调用 item_search_tmall 按关键词搜索天猫商品
java·开发语言·数据库
小陈phd2 小时前
混合知识库搭建:本地Docker部署Neo4j图数据库与Milvus向量库
数据库·docker·neo4j
2401_838472513 小时前
使用Python进行图像识别:CNN卷积神经网络实战
jvm·数据库·python
知识即是力量ol3 小时前
基于 Redis 实现白名单,黑名单机制详解及应用场景
数据库·redis·缓存
zhihuaba3 小时前
使用PyTorch构建你的第一个神经网络
jvm·数据库·python