PL/SQL設置主鍵自增


oracle沒有設置主鍵auto increment的功能,需要自己編寫序列和觸發器實現主鍵自動遞增。
示例:
創建表menu:
 

一、創建表

create table menumenuId number(10) not null primary key

                                   name varchar2(40) not null, 

                                 id_parent number(10) not null, 

                                url varchar2(300) null);

二、創建序列menu_autoinc_seq:
 create sequence  menu_autoinc_seq 

                                      minvalue 1 

                                  maxvalue 99999999

                                   start with 1 

                                increment by 1 

                                    nocycle                                    

             nocache                                       

            order;

三、創建觸發器menu_autoinc_tg:
 

create or replace trigger menu_autoinc_tg 

before insert on menu for each row 

begin 

select menu_autoinc_seq.nextval into :new.menuId from dual;

end menu_autoinc_tg;


免責聲明!

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



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