zoukankan      html  css  js  c++  java
  • Oracle通过序列实现主键自增长

    序列化+触发器

    一般选这种

    只要建立好序列化+触发器,触发器会在我们插入数据时自动触发,帮助我们进行+1操作。

    创建序列sequence,指定

    //准备工作创建一张表
    create table dept_p(
        dept_id   VARCHAR2(40) not null,
        dept_name VARCHAR2(40),
        parent_id VARCHAR2(40),
        state     NUMBER(11),
        dept_sort NUMBER(11)
    );
    alter table DEPT_P add [constraint dept_id] primary key(dept_id);

    创建序列

    create sequence seq_t_dept
    minvalue 1
    maxvalue 99999999
    start with 1
    increment by 1
    cache 50

    建立触发器

    create or replace trigger "dept_trig"
        before insert on dept_p
        referencing old as old new as new for each row
    declare
    begin
        select seq_t_dept.nextval into :new.dept_sort from dual;
    end dept_trig;

    此时报错:ORA-04089:无法对SYS拥有额对象创建触发器

    那么只能再建一个公用账户,并赋予connect和resource角色来创建触发器

    create user c##lyc identified by root;
    grant connect, resource to c##lyc;

    然后使用新建的用户登陆执行该指令

    序列化+显式调用序列化

    -- 创建sequence
    create sequence seq_on_dept
    increment by 1
    start with 1
    nomaxvalue
    nocycle
    nocache;

    显式调用序列

    insert into dept_p values('001', '安保部', '000', 1, seq_on_dept.nextval);
    论读书
    睁开眼,书在面前
    闭上眼,书在心里
  • 相关阅读:
    python--进程
    python---多线程
    python--上下文管理器
    python中的单例模式
    装饰器
    匿名函数
    python的内置方法
    命名元组
    如何管理我们的项目环境
    启动APP遇到“UiAutomator exited unexpectedly with code 0, signal null”解决
  • 原文地址:https://www.cnblogs.com/YC-L/p/14631595.html
Copyright © 2011-2022 走看看