How to create an auto-incrementing primary key in Oracle

Source: Internet
Author: User


How to create an auto-incrementing primary key in Oracle 1. create a table and set the primary key. 2. create a sequence. 3. create trigger .www.2cto.com. For example: assume that the table name of the data table is tbl_name, the primary key column is tbl_id, and other columns include tbl_name, tbl_age, tbl_xxx .... then CREATE an Oracle SEQUENCE: SQL code CREATE SEQUENCE seq_name -- use the uniform prefix name as much as possible, easy Management of increment by 1 -- Auto INCREMENT step here is set to 1 start with 1 -- count START here is set to 1 NOMAXVALUE -- do not set the maximum value option MAXVALUE | minvalue nocycle -- always accumulate, no loop CACHE 10; -- if efficiency is pursued, you can set the CACHE. if the service is interrupted abnormally, such as Oracle downtime or power failure, the sequence may not continue, if no cache is used, write NOCHACHE here and create the trigger: www.2cto.com SQL code CREATE OR REPLCAE TRIGGER tri_name -- TRIGGER name BEFORE INSERT ON tbl_name -- triggers FOR EACH ROW BEFORE inserting data to the tbl_name table -- indicates that the TRIGGER is a ROW-Level TRIGGER BEGIN -- the TRIGGER starts -- run the following code to SELECT seq_name.nextval: new. tbl_id FROM tbl_name -- seq_name.nextval returns the next value of the sequence. Set this value to a new value and insert it into the tbl_id column of the primary key in the tbl_name table. END -- when the trigger ends, the Oracle primary key auto-increment settings are completed.

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.