Oracle database primary key automatically grows SQL

Source: Internet
Author: User

As we all know, in MySQL, it is very easy to automatically increment the primary key. You only need to add auto_increment to the primary key definition, but not in Oracle, and it takes a few steps to complete 1. Create a table Create tables account   (   number                     integer                          not null,                       CHAR (+)                           not null,   name of the householder                   CHAR (+)                          not null,   address                     CHAR ($)                         Not null,   undertaker                    CHAR (+)                          not null,   dates                     date                              not null,   constraint pk_ hukou primary key (household number));  2. Create a sequence sequence: CREATE SEQUENCE book_sequence increment by 1  --add several       START with 1  each time    --counting starting from 1       nomaxvalue      --not setting the maximum value        nocycle;        --keep accumulating, not looping  3. Create a trigger: CREATE TRIGGER book_increase BeforeInsert on account this for each rowbeginselect book_sequence.nextval into:new. Account number from dual;end;& nbsp; In this case, when inserting the account in this table, the primary key input NULL, you can achieve auto-increment. There is a place to emphasize that some beginners like to name in Chinese such a very troublesome. And it is not in line with the idea of internationalization, but sequence must be in English.

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.