Oracle主鍵自增


1、創建table

1 CREATE TABLE demo6
2 (
3     id INT NOT NULL,
4     key1 VARCHAR2(40) NULL,
5     key2 VARCHAR2(40) NULL
6 );

2、設置主鍵

1 alter table demo6 add constraint demo6_pk primary key (id);

3、新建序列

1 create sequence demo6_id
2 minvalue 1
3 nomaxvalue 
4 increment by 1 
5 start with 1
6 nocache;

4、新建觸發器

1 create or replace trigger demo6_tg_insertId
2 before insert on demo6 for each row 
3 begin
4   select demo6_id.Nextval into:new.id from dual;
5 end;

5、插入數據

1 insert into demo6 (key1, key2)
2 values ('key1', 'key2');
3 insert into demo6 (key1, key2)
4 values ('key11', 'key22');

6、查詢table

1 select * from demo6;

 7、查詢當前序列值

1 select demo6_id.currval from dual;

參考文章:http://www.cnblogs.com/dshore123/p/8267240.html


 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM