Example of ORACLE Field Auto-Increment

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.