Oracle implementation Field Self-adding example description _oracle

Source: Internet
Author: User
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

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.