Oracle self-Increment column creation method

Source: Internet
Author: User

Oracle does not have functionality such as a self-increment field, but it can be implemented through triggers (trigger) and sequences (sequence).

Build a test table first:

CREATE TABLE Userlogin
(

ID Number (6) is not NULL,

Name VARCHAR2 (+) NOT NULL primary key

)

Tablespace users

/

First step: Create sequence


Create sequence Userlogin_seq increment by 1 start with 1 minvalue 1 maxvalue 9999999999999 nocache order;


Step two: Create a before insert trigger based on the table, using the sequence you just created in the trigger


Create or Replace Trigger Userlogin_trigger
Before insert on Userlogin
For each row
Begin
Select Userlogin_seq.nextval into:new.id from Sys.dual;
End

/

Step three: Test in the Userlogin table

Write an INSERT statement, insert a record, see the ID field has been increased since the increase is OK.

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.