当前位置:Gxlcms > 数据库问题 > Oracle 创建自增列

Oracle 创建自增列

时间:2021-07-01 10:21:17 帮助过:2人阅读

--1.创建表 2 create table Student( 3 ID integer not null primary key, 4 Name varchar2(40) not null, 5 Sex integer, 6 Address varchar2(100) 7 ) 8 tablespace MyTest_Data;--指明表空间 9 10 --2.创建序列 11 create sequence SEQ_StuID 12 minvalue 1 13 start with 1 14 increment by 1 15 nomaxvalue 16 nocache; 17 18 --3.创建触发器 19 create OR REPLACE TRIGGER Trigger_StuID 20 BEFORE insert 21 ON Student 22 FOR EACH ROW 23 BEGIN 24 if inserting then 25 if :NEW."ID" is null then 26 select SEQ_StuID.nextval into :NEW."ID" from dual; 27 end if; 28 end if; 29 END; 30 31 --4.激活触发器 32 alter table "admin"."Student" --用户.表名 33 enable all triggers; 34 35 --5.插入数据 36 insert into Student(NAME,SEX,ADDRESS) values(张三,1,第七街区); 37 38 --可以使用【序列名.newxtval】查看下一个要使用的序列号 39 --注意:以下语句执行一次会按指定的增量增加 40 select SEQ_StuID.nextval from dual

 

Oracle 创建自增列

标签:

人气教程排行