Application of sequence-sequence in Oracle

Source: Internet
Author: User

Oracle does not have a field type similar to the field type Automatically numbered in Acces. Therefore, it is quite troublesome to use a field to automatically add value in oracle. Here we provide you with a method to use the sequence, the following describes how to create, use, and delete a sequence.

1. create sequence ------------ create sequence

Create sequence Seq_AutoID // The sequence name Seq_AutoID. You must remember this sequence name before calling it.

Minvalue 1 // the minimum value can be customized from 1.

Maxvalue 9999999999999999999 // The maximum sequence value is 999999999999999999999.

Start with 1 // can be customized from 1

Increment by 1 // The increment range is 1 and can be customized

Cache 20; // cache 20

2. Call the sequence to automatically add 1 ------------ Seq_AutoID.nextval

You can call this operation when you insert or update data to a table. Because you only use this method in your application

EG: insert into table1 (f1, f2, f3, f4) select f1, f2, f3, Seq_AutoID.nextval from table2 where Condition

Of course, the above sentence refers to batch data insertion. You can also use the values value to insert data one by one. Note that the table1.f4 field must be of the number type. In this way, the f4 field in your table 1 is a numerical value that increases progressively from 1, and the auto-increment function is realized. Of course, the range can be defined by yourself.

Another point is that the sequence is unique. As long as you keep increasing with nextval, the sequence number will not be repeated in oracle. you can use CurrVal to query the current sequence value. After nextval, currval will also change.

3. sequence deletion ------------ drop sequence

In order to reset the sequence or not, you can delete the sequence first.

EG: drop sequence Seq_AutoID;

4. Use a sequence to modify the value of a field

Eg: update cse_depot set jobid = Seq_AutoID.nextval

In the preceding example, the current sequence value is used to assign a value to the jobid field of cse_depot, enabling the jobid field to automatically add 1.

The above points are summarized by myself and according to my own use, and I hope you will join us in the discussion.

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.