• MySQL 和 Oracle 主键自增长


    1、MySQL

    1)建表

     auto_increment:每插入一条数据,客户表(customers)的主键id就自动增1,如下所示

     1 create table customers    -- 创建客户表
     2 (
     3     id int auto_increment primary key not null,  -- auto_increment:自增长
     4     name varchar(15)
     5 );
     6 
    

    2)测试(实例)

    1 insert into customers(name) values("张三"),("李四");-- 向客户表中插入数据
    2 
    3 select * from customers; -- 查询客户表

    2、Oracle

    1)建表

    复制代码
     1 create table student
     2 (
     3   id       number not null,  -- 主键
     4   name     varchar2(20),
     5   birthday  date, 
    6 age     number(20),
    7 phone varchar2(60),
    8 email varchar2(10)
    9 )
    10 alter table student add constraint student_pk primary key (id); -- 主键
    复制代码

    2)创建序列

    复制代码
     1 /*
     2 --创建序列Sequence
     3 create sequence student_id
     4 minvalue 1  --最小值
     5 nomaxvalue  --不设置最大值(由机器决定),或 根据表字段的值范围设置 maxvalue
    6 maxvalue 99999999 -- 最大值 7 start with 1 --从1开始计数,数值可变 8 increment by 1 --每次加1,数值可变 9 nocycle --一直累加,不循环 10 nocache; --不建缓冲区。 如果建立cache那么系统将自动读取cache值个seq,这样会加快运行速度;如果在单机中使用cache,或者oracle死了,那么下次读取的seq值将不连贯,所以不建议使用cache。 11 */ 12
    13 14 -- 创建序列 15 create sequence student_id
    16 minvalue 1 17 nomaxvalue
    18 increment by 1
    19 start with 1
    20 nocache;
    复制代码

     Oracle sequence序列的创建、修改及删除 详解:http://www.cnblogs.com/dshore123/p/8269537.html

    3)创建触发器 (以下三种方式都行)

    格式:

      create or replace trigger 触发器名
      before insert on 表名 for each row when (new.表的自增长字段 is null)
      begin
        select 序列名.Nextval into:new.表的自增长字段 from dual;
      end;
    复制代码
     1 -- 方式一 
     2 create or replace trigger tg_insertId
     3 before insert on student for each row when (new.id is null)  -- 当id为NULL时触发 
     4 begin
     5   select student_id.Nextval into:new.id from dual;
     6 end;
    7
    8
    9 -- 方式二 10 create or replace trigger tg_insertId 11 before insert on student for each row 12 begin 13 select student_id.Nextval into:new.id from dual; 14 end;
    15
    16
    17 -- 方式三
    18 create or replace trigger tg_insertId
    19  before insert on student for each row
    20 declare -- 声明
    21  -- 局部变量(student表里的字段)
    22 begin
    23  if updating then
    24       insert into student
    25       values(student_id.nextval,
    26              :old.name,
    27          :old.sal, -- 假如student表 有该字段(举例、模板、样式)
    28          :old.sex -- 假如student表 有该字段
    29              );
    30  end if;
    31 end;

    复制代码

    4)测试(实例)

    复制代码
    1 INSERT INTO student(name,birthday,age,phone,email) 
    2     VALUES('zhangsan',to_date('2018-01-10 19:55:45','yyyy-MM-dd hh24:mi:ss'),18,'13510086110','123456789@qq.com');  -- 插入数据
    3
    4 INSERT INTO student(name,birthday,age,phone,email) 5 VALUES('zhangsan',to_date('2018-01-11 19:55:45','yyyy-MM-dd hh24:mi:ss'),20,'13510086110','123456789@qq.com');
    6
    7 8 select * from student; -- 查询学生表
    复制代码

    或者

    1  insert into student(seq,name,birthday,age,phone,email)     -- 这是带上“自增长主键(seq)”的写法
    2    values(student_id.Nextval,'zhangsan',to_date('2018-01-10 19:55:45','yyyy-MM-dd hh24:mi:ss'),18,'13510086110','123456789@qq.com');

    原创作者:DSHORE

    作者主页:http://www.cnblogs.com/dshore123/

    原文出自:http://www.cnblogs.com/dshore123/p/8267240.html

    -------------------------------------------------------------------------
    ## 极客时间全网最便宜最优惠购买方式,优惠券返现 百度网盘 微信关注公众号“选门好课”
    扫描下方二维码关注我的公众号"选门好课",与我一起交流知识
  • 相关阅读:
    Mac 下的 Homebrew 简介及安装
    配置Mac打开ntfs的外设磁盘硬盘的原生读写/Mac OS上使用不同格式的磁盘
    ztree使用 (一) 递归后台的数据
    springboot整合redis 配置文件及配置类(二)
    springboot整合redis 配置文件及配置类(一)
    java登录拦截器
    获取小程序二维码
    java合成图片
    微信 获取手机号
    js+html5点击赋值到剪贴板
  • 原文地址:https://www.cnblogs.com/singworld/p/9694064.html
Copyright © 2020-2023  润新知