zoukankan      html  css  js  c++  java
  • oracle 实现主键id自增

    公司现在项目数据库使用oracle,oracle实现表主键自增比mysql麻烦

    mysql 在表主键auto_increment 打钩即可。oracle没有改属性,就相对麻烦。特此记录一下自增方法

    测试案例如下

    第一步创建一张测试表table1

    sql语句

    create table table1
    (
    id int not null,
    name varchar2(20),
    sex varchar2(4)
    )

    添加表注释、字段注释

    comment on table table1 is '测试表 稍后会删除'
    comment on column table1.name is '姓名'
    comment on column table1.sex is '性别'

    第二步:创建序列

    create sequence table1_id
    minvalue 1             //自增字段最小值
    nomaxvalue           //最大值 没有就算nomaxvalue
    increment by 1      //每次增值1
    start with 1           //起始值
    nocache;             //不缓存

    第三步:创建触发器

    create or replace trigger table1_tg_insertId
    before insert on table1 for each row
    begin
    select table1_id.nextval into:new.id from dual;
    end;

    第四步:测试开始  插入两条数据

    insert into table1(name,sex) values ('zhangsan','nan');
    insert into table1(name,sex) values ('lisi','nan');

    查询数据

  • 相关阅读:
    Java SE——线程介绍
    Vue
    Spring asm
    spring 自定义标签的实现
    Spring InitializingBean 接口以及Aware接口实现的原理
    值传递与引用传递
    redis学习
    ssm多数据源配置
    redis 启动
    json的设置
  • 原文地址:https://www.cnblogs.com/prettrywork/p/11528526.html
Copyright © 2011-2022 走看看