In Oracle databases, Oracle Auto-increment fields are not supported, but can be implemented through triggers and sequence.
Assume that the [Table test] keyword segment is id and a sequence is created. The code is:
[Html]
- Create sequence seq_test
- Minvalue 1 -- Minimum value
- Maxvalue 99999999999 -- maximum value
- Start with 1 -- count from 1
- Increment by 1 -- add several
- Nocache -- no cache
- Order;
The code for creating a trigger is:
[Html]
- Create or replace trigger tri_test
- Before insert on test
- For each row
- Declare
- Nextid number;
- Begin
- IF: new. id is null or: New. id=0THEN
- Select seq_test.nextval
- Into nextid
- From sys. dual;
- : New. id:=Nextid;
- End if;
- End tri_test;
Keywords:
: NEW and: OLD use methods and meanings. new only appears in insert and update, and old only appears in update and delete. New indicates the row data to be inserted during insert, new indicates the new data to be replaced during update, and old indicates the original row to be changed, old indicates the data to be deleted.