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

相关推荐
暴躁小师兄数据学院33 分钟前
【AI大数据工程师特训笔记】第04讲:PostgreSQL 数据库内置函数详解
大数据·数据库·笔记·ai·语言模型
苏渡苇1 小时前
Spring Cloud Alibaba:将 Sentinel 熔断限流规则持久化到 Nacos 配置中心
数据库·spring boot·mysql·spring cloud·nacos·sentinel·持久化
杨云龙UP1 小时前
Oracle Recycle Bin 回收站详解:DROP TABLE 后还能找回吗?
linux·运维·数据库·sql·mysql·oracle
未来之窗软件服务2 小时前
酒店门锁V10SDK接口VB-幽冥大陆(一百26)—东方仙盟
数据库·酒店门锁·仙盟创梦ide·东方仙盟·东方仙盟sdk·东方仙盟幽冥大陆
墨_风2 小时前
MyBatis时间区间查询异常排查(达梦数据库)
数据库·mybatis·达梦
njsgcs3 小时前
用clip把设计经验变成向量数据库,然后每秒检索可以检查3维模型设计的错误吗
数据库
WiChP3 小时前
【V0.1B10】从零开始的2D游戏引擎开发之路
java·数据库·游戏引擎
Fanta丶3 小时前
7.MySql函数使用 字符串、数值、日期、流程函数
mysql
小当家.1053 小时前
PostgreSQL 做向量数据库:pgvector 在 RAG 中的实战与多场景适配
数据库·人工智能·postgresql·rag
Access开发易登软件3 小时前
Access 和 SQLite,根本不在一个赛道上
java·jvm·数据库·sqlite·excel·vba·access开发