1. 程式人生 > >資料庫:ORACLE設定主鍵自動遞增

資料庫:ORACLE設定主鍵自動遞增

建立表STUDENT   假設主鍵為ID

一、先設定遞增:

    設定遞增需要建立序列,在plsql找到sequence,右鍵點選新建

        

        名稱隨便起一個  這裡為s_student. 其他的數字如圖

二、建立觸發器

        sql:

            create or replace trigger t_student_trigger (觸發器名字,隨意起)
                before insert on student (建立的表名) 
                for each row
                when(new.id(id:主鍵) is null)
                begin
                select s_student(s_student:序列的名字

).nextval into:NEW.ID(ID:主鍵) from dual;
                end;        

此時再往建立的表中插入資料可以不用插入主鍵,並且主鍵自動遞增