How to build a sequence in an Oracle table

Source: Internet
Author: User

In Oracle, sequence is the so-called serial number, which is automatically incremented each time it is taken, and is typically used where serial numbers need to be sorted.   1, create sequence   you first have to have create sequence or create any sequence permissions,   create sequence emp_sequence       increment by  1   --  Add a few        START WITH 1  each time     --  counting starting from 1        NOMAXVALUE        --  do not set the maximum value        NOCYCLE           --  always accumulates, does not circulate        cache  10;   Once you have defined emp_sequence, you can return the current value of  sequence with currval,nextval  currval=    nextval= increases the value of sequence and then returns the  sequence  value    such as:      emp_sequence. Currval     emp_sequence. nextval   can use sequence's place:  -  subqueries that do not contain subqueries, snapshot, view  SELECT  statements   - insert Statements   -  The values of the Nsert statement in the   - UPDATE   set      can be seen in the following example:   insert  INTO emp VALUES    (empseq.nextval,  ' LEWIS ',  ' clerk ',7902,  SYSDATE, 1200, NULL, 20);  select empseq.currval       FROM DUAL;   but note that:  -  first nextval returns the initial value, and then nextval automatically increases the increment you define. The  by value, and then returns the incremented value. currval  always returns the value of the current sequence, but the currval is not used until the first nextval is initialized, otherwise an error occurs. A nextval will increment the value of sequence once, so if you use multiple nextval in the same statement, the value is different. Got it?   -  If you specify the cache value, Oracle can pre-place some sequence in memory so that it accesses faster. After the cache is finished, Oracle automatically takes another set to the cache.   using the cache may jump number,  such as the database suddenly abnormal down (shutdown abort), the cache sequence will be lost .  so you can create  Sequence use NoCache to prevent this situation.   2, alter sequence   you or the owner of the Sequence, or have Alter any sequence  permission to change sequence.  can alter all sequence parameters except start to. If you want to change the start value, you must  drop    Example of sequence   re-create .  Alter sequence    alter sequence  emp_sequence       INCREMENT BY 10        MAXVALUE 10000       CYCLE      --  start from scratch        NOCACHE ;   affect initialization parameters of sequence after 10000:   sequence_cache_entries = sets the number of SEQUENCE that can be simultaneously CACHE.     can be very simple drop sequence  drop sequence order_seq;


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.