zoukankan      html  css  js  c++  java
  • oracle表自增序列

    1.给表自增序列

    1--创建测试表
    
    create table student_test (
    
           student_id number(8) not null primary key, --主键列
    
           first_name varchar2(50), -- 名字
    
           last_name varchar2(50)   --
    
    );
    2--创建序列
    create sequence seq_student_test
           minvalue 1  --最小值
           nomaxvalue --最大值
           start with 1 --起始值
           increment by 1  --增长基数
           nocycle  --不循环,一直增加
           nocache ;
    3   --删除触发器   
           DROP TRIGGER tri_student_ins
    4  --创建序列后插入
    insert into student_test(student_id,first_name,last_name) values(seq_student_test.nextval,'','');
    5--创建触发器
    create trigger tri_student_ins 
           before insert on student_test for each row  when (new.student_id is null)
        begin 
          select seq_student_test.nextval into:new.student_id from dual;
        end;
    
    6--序列插入
    insert into student_test(first_name,last_name) values('','');
    
    7--循环插入代码 
    declare 
           i number := 1;
           v_first_name varchar2(50);
           v_last_name varchar2(50);
    begin 
      for i in 1 .. 200 
        loop 
          v_first_name := '' || to_char(i);
          v_last_name := '' || to_char(i);
          insert into student(first_name,last_name) values(v_first_name,v_last_name);
          commit;
        end loop;
    end;

    2.创建触发器,相关测试数据

    CREATE  TABLE  SEOrder(
              FInterId  number(8) not null primary key ,
              FBillNo  VARCHAR(50)  NOT  NULL,
              FBillerID  INT  DEFAULT(0),
              FDepart  INT  NOT  NULL
     ) 
    
    
    --alter table SEOrder add constraint SEOrder_PK primary key (FInterId);
    ALTER  TABLE  SEOrder ADD  FDate  VARCHAR(50)
    ALTER  TABLE  SEOrder    DROP  COLUMN  KKK;
    ALTER  TABLE  SEOrder  MODIFY    (FDate  DATE );
    
    alter table SEOrder drop column KKK;
    drop sequence SEOrder_Id_sequence;
    drop trigger SEOrder_Id_autoincrement;
    --DROP sequence SEOrder_FInterId_sequence
    create sequence seq_student_test
           minvalue 1  --最小值
           nomaxvalue --最大值
           start with 1 --起始值
           increment by 1  --增长基数
           nocycle  --不循环,一直增加
           nocache ;
    DROP sequence SEOrder_FInterId_sequence
     create sequence SEOrder_FInterId_sequence
     minvalue 1  --最小值
           nomaxvalue --最大值
           start with 1 --起始值
           increment by 1  --增长基数
           nocycle  --不循环,一直增加
           nocache ;
    create trigger SEOrder_FInterId_autoincrement 
    before 
     insert ON SEOrder for each row 
     when (new.FInterId is null)
    begin
     select SEOrder_FInterId_sequence.nextval into:new.FInterId from dual;
     end;
    DROP  trigger SEOrder_FInterId_autoincrement 
    grant select, insert, update, delete, references, alter, index on SEOrder to PUBLIC;
     ALTER TRIGGER  SEOrder_FInterId_autoincrement COMPILE ;
    DROP TABLE  SEOrder;
    -- Create table
    
    DROP table  SEORDER
    create table SEORDER
    (
      finterid  NUMBER(8) not null,
      fbillno   VARCHAR2(50) not null,
      fbillerid NUMBER(5) not null,
      fdepart   NUMBER(5) not null,
      fdate     DATE
    )
    tablespace DBS1
      pctfree 10
      initrans 1
      maxtrans 255
      storage
      (
        initial 64K
        next 1M
        minextents 1
        maxextents unlimited
      );
    -- Create/Recreate primary, unique and foreign key constraints 
    alter table SEORDER
      add primary key (FINTERID)
    
    ALTER TRIGGER SEOrder_FInterId_autoincrement DISABLE;
    CREATE  TABLE  SEOrder
          (
              FInterId  INT ,
              FBillNo  VARCHAR(50)  NOT  NULL,
              FBillerID  INT  DEFAULT(0),
              FDate  DATE,
              FDepart  INT  NOT  NULL
    )
    INSERT  INTO  SEOrder  VALUES(SEOrder_FInterId_sequence.nextval,'XM201501' , '1' , '2015-1-22' , '1' )
    INSERT  INTO  SEOrder  VALUES(1,'XM201502' , '1' , '2015-1-22' , '1' )
    INSERT  INTO SEOrder (FBillNo , FBillerID ,FDate , FDepart)
    VALUES('XM201503' ,'2' ,to_date('2015-1-22','yyyy-mm-dd') , '2' )
    alter table SEOrder add FDate DATE;
    SELECT * FROM
    SEOrder
    INSERT  INTO  SEOrder(FInterId,FBillNo , FBillerID,FDate , FDepart)    
    VALUES(SEOrder_FInterId_sequence.nextval,'1' , '1' ,sysdate, '2' )
  • 相关阅读:
    TensorFlow 1.2.0新版本完美支持Python3.6,windows在cmd中输入pip install tensorflow就能下载应用最新tensorflow
    两眼论&矩阵变现理论结合打造赚钱大模式
    《影响力》6个使人顺从的武器之一互惠原理深入剖析
    电子书如何定位、如何取名呢?电子书文案模型的6个要点
    《影响力》顺从心理学6个使人顺从的策略
    SEO三种职位类型:编辑型SEO、技术型SEO、营销型SEO详解
    json-lib-2.4-jdk15.jar所需全部JAR包.rar java jsoup解析开彩网api接口json数据实例
    防DNS劫持教程,手动修复本地DNS教程
    拆分字符串
    计算目录下文件数
  • 原文地址:https://www.cnblogs.com/chenzhelove/p/13479729.html
Copyright © 2011-2022 走看看