Sequence usage in Oracle

Source: Internet
Author: User

Sequence usage in Oracle:

Create table test
(Pid integer primary key,
Name varchar (20)
);

Select * from test;
Insert into test values (1, 'jack ');

Create sequence test_seq; -- create a sequence
Select test_seq.nextval from dual; -- add one after each use of the sequence
Select test_seq.currval from dual; -- query the value of the current sequence
Insert into test values (test_seq.nextval, 'Jack Or Duck? '); -- Sequence usage
Drop sequence test_seq; -- delete a sequence
Create sequence test_seq start with 8; -- specify the initial value of the sequence
Alter sequence test_seq minvalue 9; -- sets the minimum value of the sequence.
Alter sequence test_seq maxvalue 9999; -- sets the maximum value of the sequence.
Alter sequence test_seq increment by 1; -- sets the sequence step size.
Drop sequence test_seq_1;
Create sequence test_seq_1 start with 3 minvalue 1 maxvalue 30 increment by 1;
Select test_seq_1.nextval from dual;
Alter sequence test_seq_1 cycle;
Alter sequence test_seq_1 nocycle; -- disable the cyclic Value Function
Alter sequence test_seq_1 cache 10;

-- Set the sequence cache size. Note: The default value of the sequence cache is 20.
Create sequence test_seq_2 start with 1 minvalue 1 maxvalue 20 increment by 3;
Alter sequence test_seq_2 cycle;

-- The preceding statement reports an error: the Cache value must be smaller than the cycle value.

Alter sequence test_seq_2 increment by 2;
Alter sequence test_seq_2 cycle;
-- Error reported
Alter sequence test_seq_2 increment by 1;
Alter sequence test_seq_2 cycle;
-- Error reported
Alter sequence test_seq_2 maxvalue 21;
Alter sequence test_seq_2 cycle;
-- No error is reported.

-- Or modify
Alter sequence test_seq_1 cache 5;
-- Start with 1 and maxvalue 10 step 1 then 10 times cycle and bigger than 5

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.