[Oracle11g auto-increment primary key], oracle11g primary key
Oracle Auto-incrementing primary keys must be implemented using sequences and triggers. First, we create a table where the primary key is int to implement auto-incrementing. CreatetableTEST ( ID intPRIMARYKEY, Test1 varchar2 (20 ), Test2 varchar2 (20 ), Test3 varchar2 (20 ))
Create a sequence: Create sequence SEQ_TEST Minvalue 1 -- Minimum value Maxvalue 999999999999999999999999999 -- maximum value Start with 1 --- start from 1 Increment by 1 --- add 1 each time Nocycle -- always accumulate without repeating nocache;
Create a trigger to implement auto-increment of the primary key when inserting data:
Create or replace trigger tg_test // Test table name, id primary key name, seq_test.nextval Sequence Before insert on testFOR each row when (new. id is null) Beginselect seq_test.nextval into: new. id from dual; End; When the primary key of the inserted data is null, the sequence is used to generate the auto-incrementing primary key. // ------------------ Trigger Style Create or replace trigger TRI_SCORE BEFORE INSERT OR UPDATE ON XUANKE FOR EACH ROW BEGIN // If score is empty, score = 0 IF: new. score is null then: new. SCORE: = 0; End if; END; |