zoukankan      html  css  js  c++  java
  • oracle设置主键自增

    oracle中没有自增字段,可通过序列+触发器间接实现,cmd中sqlplus登录,直接运行即可。一般要经过一下几步:

    1建立数据表

    create table Test_Increase(
               userid number(10) primary key,  /*主键,自动增加*/
               username varchar2(20)
               );

    2创建自动增长序列

     CREATE SEQUENCE TestIncrease_Sequence
     INCREMENT BY 1   -- 每次加几个  
         START WITH 1     -- 从1开始计数  
         NOMAXVALUE       -- 不设置最大值  
         NOCYCLE          -- 一直累加,不循环  
         CACHE 10; 

    3创建触发器

    CREATE TRIGGER Test_Increase BEFORE
    insert ON  Test_Increase FOR EACH ROW
    begin
    select TestIncrease_Sequence.nextval into:New.userid from dual;

    end;

    4 提交

    commit;

    5 测试

    反复执行如下语句:

    insert into Test_Increase(Username) values('test')

    6 查看插入结果:

    userid username

     1       test
     2       test
     3       test
     4       test
     5       test
     6       test
     7       test
     8       test
     9       test

    转:http://blog.163.com/xuxiaoqianhz@126/blog/static/165190577201082910371842/

  • 相关阅读:
    力扣(LeetCode) 14. 最长公共前缀
    力扣(LeetCode)965. 单值二叉树
    力扣(LeetCode)258. 各位相加
    力扣(LeetCode)389. 找不同
    PTA 阶乘之和取模
    A. Sea Battle
    PTA 二叉树路径
    PTA 重构二叉树
    PTA 笛卡尔树
    绿豆蛙的归宿
  • 原文地址:https://www.cnblogs.com/wang-jx/p/9308971.html
Copyright © 2011-2022 走看看