Auto-increment functionality in the MySQL database implemented in the Oracle database

Source: Internet
Author: User

In the MySQL database, you want to implement a self-increment function of the data (that is, when inserting this data, fill in null, System automatic + 1), you can use the statement auto-increment directly in the column.

int Primary Key Auto_increment

This is not used in Oracle databases. The following describes how to implement the self-increment of the ID column in an Oracle database.

1 Create a table cutomers. (Needless to say)

1 Create TableCustomers (2Id Number(Ten),  3Namevarchar( -) not NULL,4Addressvarchar( -),5Phonevarchar( -),6        constraintCustomers_id_pkPrimary Key(ID),7        constraintCustomers_name_ukUnique(name)8        9)

2 Create a sequence sequence.

1 CREATE SEQUENCE customers_sequence 2  by 1---Add 1 each time 3  with 1-----Starting from 1 4 nomaxvalue-------Not set the maximum value 5 nocycle----------always accumulate, do not cycle

3 Create a trigger so that each time the data is inserted it touches the trigger to automatically add 1 to the process.

1 CREATE TRIGGER Customersid_increase 2 before 3 INSERT  on  for Each ROW 4 begin 5 Select  into  from dual; 6 End;

4 Validation: When inserting data, the ID column can be directly written as null.

 1  insert  into  customers values  (null ,  tom   ", "  miami  , "   18376326890  ) 
2 Insert  into Values (null,'Wade','Chicago','  13157632690')

To view the data in a table, you can:

1 Tom Miami 18376326890 ...............
2 Wade Chicago ...... ..... ........................

The ID column implements the Automatic plus 1 feature.

Auto-increment functionality in the MySQL database implemented in the Oracle database

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.