Application of Oracle Auto-increment Fields

Source: Internet
Author: User

Today we will introduce Oracle Auto-increment fields. We all know that Oracle and ms SQL are different. By default, auto-increment columns do not exist, however, we can create a sequence and trigger to create an auto-incrementing column. The following describes how to create an auto-incrementing column in Oracle 11g ).

1. Create a TestTable table,

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

Used for Oracle Auto-incrementing Column

 
 
  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 Oracle Auto-increment column in The TestTable table is created. Just insert a piece of data to your colleagues!

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.