Solution for creating Oracle Auto-increment Fields

Source: Internet
Author: User

The following articles describe the content of Oracle Auto-increment fields. We all know that Oracle is different from ms SQL, and there is no relevant auto-increment column by default, however, we can create auto-incrementing fields by creating relevant sequences and triggers. The following describes how to create the Oracle Auto-incrementing field Oracle 11g ).

1. Create a TestTable table,

 
 
  1. CREATE TABLE TestTable  
  2. (  
  3. ID NUMBER NOT NULL,   

Used for auto-increment Columns

 
 
  1. NAME VARCHAR2(40)  
  2. )  
  3. ;  

2. Create a Sequence) and name it SEQ. We recommend that you use the built-in visualization tool to create 'dunk', which is the user name I log on to. Do not copy it all ).

 
 
  1. CREATE SEQUENCE "DUNCAN"."SEQ" MINVALUE 1 MAXVALUE 999999999999999999999999999  
  2. INCREMENT BY 1 START WITH 21 CACHE 20 NOORDER NOCYCLE ;  

3. Create a trigger

 
 
  1. create or replace  
  2. TRIGGER Test_Trr  
  3. BEFORE INSERT ON TestTable  

Create a trigger on 'testtable'

 
 
  1. FOR EACH ROW  
  2. BEGIN  
  3. SELECT SEQ.NEXTVAL INTO :new.ID FROM sys.DUAL;  

'Nextval = increase the sequence value, then return the sequence Value and insert this value into the ID'

 
 
  1. END; 

Now, the auto-increment column in The TestTable table is created. Just insert a piece of data to your colleagues! The above content is an introduction to the implementation of Oracle Auto-increment fields. I hope you will gain some benefits.

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.