(Turn) Triggers to realize primary key auto-increment under oracle

Use sequences to generate primary key values. 

A Sequence is a database object that can be used by multiple users to generate a series of unique numbers. The sequence definition is stored in the data dictionary, which simplifies the programming work by providing a sequential list of unique values, and can use the sequence to automatically generate the key value of the primary key. When a sequence is called for the first time by a query, it will return a predetermined value. On each subsequent query, the sequence will produce a value that grows by the specified increment. The sequence can be looped, or continuously incremented, until a specified maximum value is reached. 
The syntax for creating a sequence is as follows: create sequence [mode] sequence name [start with start number] [increment by increment][maxvalue maximum value|nomaxvalue][minvalue minimum value|nominvalue][cycle|nocuyle][cache number|nocache ][order|noorder]. 

The sequence starting number, maximum value, minimum value and increment value can be used to determine whether the sequence is in ascending or descending order, and how much to increase or decrease each time. The Nocyle option is used to determine that no more values ​​can be generated after the sequence reaches a maximum value (increasing sequence) or a minimum value (decrementing sequence) to prevent the sequence from wrapping around. 

Create a sequence in the Oracle database, and use the unique value generated by the sequence to automatically increase the primary key value in the table when writing data to the database using the SQL statement. E.g: 

  1. SQL>create table tablename    
  3.     (id number notnull,…);
  6. SQL >create sequence autoID increment by 1 start with 1 maxvalue 999999 cycle;  
  9. SQL >insert into tablename values(autoID.nextval,...); 

Multiple users can share a sequence, but it is for all tables, so the sequence number generated for a table is unique but not continuous. 

Use triggers to generate primary key values. 

In the data table, it is sometimes necessary to automatically increase the primary key value, but in the Oracle database, there is no data type that automatically increases like MySQL 's Autoincrement. When implementing the field auto-increment function of the Oracle database, DML triggers are used to complete it. 

Triggers are procedures that are executed when a specific database event occurs. Triggers can be used to extend referential integrity . DML is a data manipulation language, which is used by users or programmers to operate on the data in the database. Basic data operations are divided into two categories: retrieval (query) and update (insert, delete, modify). Triggers are similar to functions and procedures, which exist on their own in the database. The trigger event can be a DML (insert, update, or delete) operation on a database table, etc. DML trigger is currently the most widely used trigger, that is, a trigger triggered by a DML statement, and the statement determines the type of DML trigger. Its trigger events include insert (insert), update (update) and delete (delete). Regardless of the trigger event, you can create before and after triggers for each trigger event. For example, you can create a before insert statement on the table, which means to take action before the insert event occurs. 

The syntax for creating a trigger is as follows:

  1. create [or replace] trigger  trigger name    
  3. {before | after | instead  of } fires the trigger event    
  5. referencing_clause    
  7. [WHEN trigger_condition]    
  9. [FOR EACH ROW]   

The referencing_clause is used to refer to the data in the row being modified, if trigger_condition is specified in the WHEN clause, the condition is first evaluated. The trigger body will only run if the condition is true. The combination of triggers and sequences can automatically increase the primary key value in the table when performing DML operations . The implementation steps can refer to the following examples.


  1. drop table book;   
  2. --create table      
  3. create table book(       
  4.    bookId varchar2 (4)  primary  key ,   
  5.    name varchar2(20)         
  6. );   
  7. -- create sequence      
  8. create sequence book_seq start with 1 increment by 1;    
  10. --create trigger      
  11. create or replace trigger book_trigger       
  12. before insert on book       
  13. for each row       
  14. begin       
  15. select book_seq.nextval into :new.bookId from dual;      
  16. end ;   
  17. --adding data      
  18. insert into book(name)  values ('cc');    
  19. insert into book(name)  values ('dd');   
  21. commit;  



Query data: select  *  from  book; 

When you need to write the primary key value in the book database table, you can use the sql statement to use the sequence + trigger to automatically increase the primary key value.




Guess you like