標籤:
類似SqlServer中的識別欄位 identity,Oracle實現同樣的效果有點小複雜額,如下:
1 --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 insert21 ON Student 22 FOR EACH ROW23 BEGIN24 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 建立自增列