Here is an example of how to implement the self increase of the fields in Oracle.
First, create a table superamin
Copy Code code as follows:
CREATE TABLE Superadmin (
ID number (one) primary key,
Name varchar (one) is not null unique,
Password varchar (one) NOT NULL
)
And then just create a sequence
Copy Code code as follows:
Create sequence Autoid
Start with 1
Increment by 1
MinValue 1
Nomaxvalue
Then, when you insert the record, you can invoke the sequence created above to implement the field's self increase.
Copy Code code as follows:
Insert into Superadmin (Id,name,password) VALUES (Autoid.nextval, ' one ', ' one ')
After adding more than one record, you can see that the ID field is growing automatically, but this method is not convenient enough and requires us to manually enter the Autoid.nextval.
Next we can implement it through triggers. Create a trigger.
Copy Code code as follows:
Create Trigger Trg_superadmin_autoid
Before insert on Superadmin
For each row
Begin
Select Autoid.nextval Into:new.ID from dual;
End Trg_superadmin_autoid;
Insert Record
Copy Code code as follows:
Insert into Superadmin (Name,password) VALUES (' Three ', ' three ')
After inserting multiple records, you can find that the trigger also implements the same functionality and is more convenient when inserting records