The following example shows how to implement auto-increment of oracle fields.
First, create a table SuperAmin
Copy codeThe Code is as follows:
Create table SuperAdmin (
ID number (11) primary key,
Name varchar (11) not null unique,
Password varchar (11) not null
)
Then, create a sequence.
Copy codeThe Code is as follows:
Create sequence autoid
Start with 1
Increment by 1
Minvalue 1
Nomaxvalue
Then, when inserting records, you can call the sequence created above to implement field auto-increment.
Copy codeThe Code is as follows:
Insert into SuperAdmin (ID, Name, Password) values (autoid. nextval, 'one', 'one ')
After adding multiple records, we can see that the ID field is automatically increased, but this method is not convenient enough. We also need to manually enter autoid. nextval.
Next, we can use triggers. Create a trigger.
Copy codeThe Code is 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 codeThe Code is as follows:
Insert into SuperAdmin (Name, Password) values ('Three ', 'three ')
After inserting multiple records, you can find that the trigger has implemented the same function, and it is more convenient to insert records.