First, you must have a table!
Create Table example (
ID number (4) not null primary key,
Name varchar (25 ),
Phone varchar (10 ),
Address varchar (50)
);
Then, you need a custom Sequence
Create sequence emp_sequence
Increment by 1 -- add several
Start with 1 -- count from 1
Nomaxvalue -- do not set the maximum value
Nocycle -- always accumulate without repeating
Nocache -- do not create a buffer
The above code completes the establishment of a sequence, named emp_sequence. The range is from 1 to infinitely large (the degree of infinity is determined by your machine ), nocycle decides not to loop. If you set the maximum value, you can use cycle to make seq reach the maximum and then loop. for nocache, by the way, if you provide the cache value, the system will automatically read the seq of your cache value.
In this way, the operation speed will be accelerated during repeated operations. However, if an unexpected situation occurs, such as when the machine or Oracle is dead, the seq value retrieved next time will be inconsistent with the previous one. (If the connection is not coherent, we recommend using cache because time is money! Run the question !)
You only have tables and sequences. You also need a trigger to execute them! The Code is as follows:
Create trigger "trigger name" before
Insert on example for each row when (New. ID is null)
Begin
Select emp_sequence.nextval into: New. ID from dual;
End;
Close the work! Next, try to insert data.