First, create a table:
CREATE TABLE Example (
ID Number (4) Not NULL PRIMARY KEY,
NAME VARCHAR (25));
Then, customize a sequence (sequence):
CREATE SEQUENCE example_sequence
INCREMENT by 1--add a few each time
Start with 1-counting starting from 1
Nomaxvalue--Do not set the maximum value
Nocycle--keep accumulating, not looping
NOCACHE--Do not build buffers
Create a second trigger:
CREATE TRIGGER Example_triger before
INSERT on example for each ROW time (new.id is null)-- starts the trigger generation ID number only when the ID is empty
Begin
Select Example_sequence.nextval into:new.id from dual;
End
Test it, insert the data
Insert into example (name) VALUES (' Zhang San ');
Insert into example (ID, name) VALUES (111, ' John Doe ');
Input query statement:
Select t.* from EXAMPLE t
The result is:
| Id |
Name |
| 1 |
Tom |
| 111 |
John doe |
Oracle self-Increment ID implementation