Create auto-increment Field sequence in Oracle

Source: Internet
Author: User

The auto-incrementing field sequence created by Oracle is not as convenient as the auto-incrementing SEQUENCE in SQL Server. If you want to achieve the effect in SQL Server, it is not very troublesome. You need to create an auto-incrementing sequence SEQUENCE, create a trigger. Www.2cto.com create sequence emp_sequence increment by 1 -- Auto INCREMENT step, where the step size is 1 start with 1 -- count NOMAXvalue from 1 -- do not set the maximum value. If you want to set the maximum value, that is, the value NOCYCLE that you want to set after MAXvalue -- always accumulate, no loop NOCACHE; -- do not set cache. If you want to set it, the format is the same as that of MAXvalue which defines emp_sequence. You can use CURRVAL, NEXTVAL. CURRVAL = returns the current sequence value, NEXTVAL = increases the sequence value, and then returns the sequence value. For example, in values of NSERT statements in the INSERT sub-query of SELECT statements in the SELECT statement, note: the first value returned by NEXTVAL is the initial value. The subsequent NEXTVAL will automatically increase the step size you defined and then return the added value. CURRVAL always returns the value of the current SEQUENCE, but CURRVAL can be used only after the first NEXTVAL initialization; otherwise, an error will occur. NEXTVAL increases the SEQUENCE value once, so if you use multiple NEXTVAL values in the same statement, their values are different. 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. The cache may be skipped. For example, if the database suddenly fails to be shut down (shutdown abort), the sequence in the cache will be lost. Therefore, nocache can be used to prevent this situation when creating sequence. Alter and drop are used to modify or delete a Sequence. Finally, we will provide you with a small example: [SQL] -- Creating an auto-incrementing sequence create sequence seqid NOMAXvalue start with 1 increment by 1 nocache order; -- create or replace trigger tri_test_id before insert on mytable -- <span style = "font-family: Arial, Helvetica, sans-serif; "> mytable </span> is the table for each row declare nextid number; begin IF: new. id is null or: new. id = 0 THEN -- id is the column name of the auto-increment sequence to insert select seqid. nextval into nextid from sys. dual;: new. id: = nextid; end if; end tri_test_id;

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.