Sequence of Oracle (primary key auto-increment)

Source: Internet
Author: User


1. the auto-Increment Function in Oracle can only be completed by sequence. All auto-increment operations must be completed by the user. Sequence creation format: [SQL] CREATE SEQUENCE sequence [INCREMENT BY n] [STARTWITH n] [{MAXVALUE n | NOMAXVALUE}] [{MINVALUE n | NOMINVALUE}] [{CYCLE | NOCYCLE}] [{ CACHE n | NOCACHE}]; example of www.2cto.com: Create a sequence of myseq to verify auto-increment operations. [SQL] CREATE SEQUENCE myseq; after the SEQUENCE is created, the user should handle all automatic growth. Therefore, the following two operations are provided in the SEQUENCE: nextVal: the next content of the learned sequence, currVal: the current content example of the de-sequence: CREATE an operation for verifying the sequence [SQL] CREATE TABLE testseq (next NUMBER, curr NUMBER ); now, when adding data to a table, you need to manually use the sequence example: Use the sequence [SQL] INSERT INTO testseq (next, curr) VALUES (myseq. nextval, myseq. currval); www.2cto.com. Query the testseq table: [SQL] SELECT * FROM testseq; you can find that the content of newVal is an operation that increases automatically, while currVal uses the sequence result of the current operation. That is to say, the current growth of this sequence is 1, and the growth of the sequence can be modified. Example of each increment by length: delete SEQUENCE: [SQL] drop sequence myseq; re-create sequence: [SQL] CREATE SEQUENCE myseq INCREMENT BY 2; then CREATE a table and perform test: [SQL] drop table testseq; create table testseq (next NUMBER, curr NUMBER); www.2cto.com INSERT 5 pieces of data, test: [SQL] INSERT INTO testseq (next, curr) VALUES (myseq. nextval, myseq. currval); by default, the sequence starts from 1, you can also use start with to specify its starting position. [SQL] DROP SEQUENCE myseq; CREATE SEQUENCE myseq INCREMENT BY 2 STARTWITH 10; -- CREATE a SEQUENCE starting from 10 and set the fixed values to 1, 3, 5, 7, and 9, cyclic sequence. [SQL] DROP SEQUENCE myseq; CREATE SEQUENCE myseq MAXVALUE 10 INCREMENT BY 2 START WITH 1 CACHE 2 CYCLE;
 

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.