Correct use of Oracle sequence

Source: Internet
Author: User

We all know that the Oracle database does not have fields related to the auto-increment type, therefore, we usually need to define a data about Oracle sequence as its Automatically increasing Type field. So I recorded some information about Oracle sequence, which is available for future reference!

Sequence in Oracle)

1: how to define a sequence

Only forward numeric variables (and automatic numbers in SQL are a bit like identity (1, 2 ))

Format:

Create sequence <sequence Name>

Start with <start number>

Increment by <growth volume>

[Maxvalue value]

[Minvalue value]

[When cycle reaches the maximum value, it will start from scratch]

[Nocycle -- always accumulate without repeating]

[Cache]

Note:

The first value returned by nextval is the initial value. The subsequent NEXTVAL will automatically increase the value of your defined increment by and then return the added value. CURRVAL always returns the value of the current Oracle SEQUENCE, but CURRVAL can be used only after the first NEXTVAL initialization; otherwise, an error occurs. NEXTVAL increases the value of SEQUENCE once.

If the CACHE value is specified, Oracle can place some sequence in the memory in advance, so that the access speed is faster. After the cache is obtained, Oracle automatically retrieves another group to the cache. If the cache is used, it may jump. For example, if the database suddenly fails to shut down abort), the Oracle sequence in the cache will be lost. Therefore, nocache can be used to prevent this situation during the create sequence operation.

 
 
  1. example:  
  2. create sequence mySeq  
  3. start with 1  
  4. increment by 2  
  5. maxvalue 40  
  6. minvalue 1  
  7. cycle  

2: how to retrieve sequence data:

 
 
  1. currval--->curenvalue  
  2. nextVal-->NextVlaue  
  3. example:  
  4. select mySeq.nextVal from dual  

Note: currval can be used only after nextVal is executed once.

Sequence can be used when creating a table.

Example:

Create table basic info table

 
 
  1. (  
  2. ComPID int,  
  3. CompName varchar2(20)  
  4. )  

Insert into company basic info table values (mySeq. nextVal, 'A ')

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.