The application of Nextval,currval in the database.

Source: Internet
Author: User

X.nextval gets the next sequence value of the x sequence, X.currval gets the current sequence value of the x sequence.

Note: In reference to X. You must first refer to xbefore currval . Nextval, otherwise the ORA-08002 is reported: The sequence X.currval has not yet been defined in this session.

Specific applications

1. Create a sequence:

Create sequence Test_seq increment by 1 start with 1 maxvalue minvalue 0 nocycle cache order;

Select Sequence Test_seq.nextval from dual;//once per call nextval, sequence values are incremented one time (increment value custom)

Select sequence Test_seq.currval from dual;

2. Create a table:

CREATE TABLE test (col1 int, col2 int);

3. Insert Record:

INSERT into test values (0, 0);

4, the use of Nextval

INSERT into Test (col1, col2) VALUES (Test_seq.nextval, test_seq.nextval);//Do you guess col1 and col2 values will be the same?

If the test table has two records with a col1 value of 2, then:

Update Test Set col2 = test_seq.nextval where col1 = 2;//Do you guess the col2 value will be the same after the update?

The application of Nextval,currval in the database.

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.