Table in Oracle database set PRIMARY KEY auto-increment

Source: Internet
Author: User

There is no self-increment field in Oracle, which can be implemented indirectly via sequence + trigger, Sqlplus login in cmd and run directly. It usually takes a few steps:

1 Creating a data sheet


CreateTable test_increase (
            userid number (10)  primarykey,   /* primary key, automatically add */
           username  varchar2 20            );

2 Creating an auto-grow sequence

CREATESEQUENCE testincrease_sequence
INCREMENTBy1--Add a few each time
STARTWith 1      --  count starting from 1   
     nomaxvalue        --  does not set the maximum value   
      Nocycle          --  has been accumulating, does not cycle   
      cache 10

3 Creating a Trigger

CREATE TRIGGER test_increase before
Insert on Test_increase for each ROW
Begin
Select testincrease_sequence.nextval into: New.userid from dual;

end;

4 Submit

Commit

5 Testing

Execute the following statement repeatedly:

INSERT INTO test_increase (Username) values('Test')

6 View Insert Results:

UserID username

1 Test
2 Test
3 Test
4 test
5 test
6 test
7 Test
8 test
9 test

Turn:http://blog.163.com/[email protected]/blog/static/165190577201082910371842/

Table in Oracle database set PRIMARY KEY auto-increment

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.