zoukankan      html  css  js  c++  java
  • Oracle 实现数据表插入时主键列自增

    首先创建sequence:

    create sequence TEST_SEQ  //序列名(TEST_SEQ 为序列名,自定义命名)­
    increment by 1  //每次增加1­
    start with 1  //从1开始­
    minvalue 1  //最小值1­
    nomaxvalue  //没有最大值 或者­ maxvalue 99999999999999999
    nocache  //没有缓存序列­ 或者 cache 20 缓存20个

    select TEST_SEQ.currval from dual;   //查询当前的序列值
    select TEST_SEQ.nextval from dual;  //查询下一个序列值

    假设表名为orcl_test
    方法一:自动获取自增主键

    INSERT INTO orcl_test VALUES(TEST_SEQ.nextval , '小明','xiaoming',.....)  //直接在主键自增列上用TEST_SEQ.nextval 来获取值就行

    方法二:使用触发器

    create or replace trigger test_trigger //(test_trigger) 触发器名称
    before insert 
    on orcl_test  //(orcl_test)表名称
    for each row  // for each row : 对表的每一行触发器执行一次
    begin
    select TEST_SEQ.nextval into :new.id from dual;  // (new.id)new 等于新插入的一行, id 是orcl_test 表要实现自增的主键列
    end;

  • 相关阅读:
    Python作业本——第4章 列表
    Python作业本——第3章 函数
    Python作业本——前言
    Yandex企业邮箱申请教程
    如何看待HTTP/3
    图床合集
    Windows File Recovery
    在线检测你的密码是否被泄露
    mybatis的mapper文件内容回顾
    java中系统中的常量
  • 原文地址:https://www.cnblogs.com/ningcc/p/14848248.html
Copyright © 2011-2022 走看看