Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create id with AUTO_INCREMENT on Oracle?

It appears that there is no concept of AUTO_INCREMENT in Oracle, up until and including version 11g.

How can I create a column that behaves like auto increment in Oracle 11g?

like image 387
Sushan Ghimire Avatar asked Jul 02 '12 15:07

Sushan Ghimire


People also ask

Can we implement AUTO_INCREMENT key in Oracle?

When you define a column in MySQL, you can specify a parameter called AUTO_INCREMENT. Then, whenever a new value is inserted into this table, the value put into this column is 1 higher than the last value. But, Oracle does not have an AUTO_INCREMENT feature.

How do I create an existing column auto increment in Oracle?

You can double click the name of the column or click on the 'Properties' button. Column Properties dialog box appears. Select the General Tab (Default Selection for the first time). Then select both the 'Auto Increment' and 'Identity Column' check boxes.

How do I auto increment a column in SQL Developer?

Double click your table, then go to the column section. Here double click on the column which will have the auto increment. In the general section there is a checkbox "autoincrement", just tick it. After that you can also go to the "autoincrement" section to customize it.

Can you create an auto increment on a unique key?

Auto-increment allows a unique number to be generated automatically when a new record is inserted into a table. Often this is the primary key field that we would like to be created automatically every time a new record is inserted.


2 Answers

There is no such thing as "auto_increment" or "identity" columns in Oracle as of Oracle 11g. However, you can model it easily with a sequence and a trigger:

Table definition:

CREATE TABLE departments (   ID           NUMBER(10)    NOT NULL,   DESCRIPTION  VARCHAR2(50)  NOT NULL);  ALTER TABLE departments ADD (   CONSTRAINT dept_pk PRIMARY KEY (ID));  CREATE SEQUENCE dept_seq START WITH 1; 

Trigger definition:

CREATE OR REPLACE TRIGGER dept_bir  BEFORE INSERT ON departments  FOR EACH ROW  BEGIN   SELECT dept_seq.NEXTVAL   INTO   :new.id   FROM   dual; END; / 

UPDATE:

IDENTITY column is now available on Oracle 12c:

create table t1 (     c1 NUMBER GENERATED by default on null as IDENTITY,     c2 VARCHAR2(10)     ); 

or specify starting and increment values, also preventing any insert into the identity column (GENERATED ALWAYS) (again, Oracle 12c+ only)

create table t1 (     c1 NUMBER GENERATED ALWAYS as IDENTITY(START with 1 INCREMENT by 1),     c2 VARCHAR2(10)     ); 

Alternatively, Oracle 12 also allows to use a sequence as a default value:

CREATE SEQUENCE dept_seq START WITH 1;  CREATE TABLE departments (   ID           NUMBER(10)    DEFAULT dept_seq.nextval NOT NULL,   DESCRIPTION  VARCHAR2(50)  NOT NULL);  ALTER TABLE departments ADD (   CONSTRAINT dept_pk PRIMARY KEY (ID)); 
like image 173
Eugenio Cuevas Avatar answered Sep 23 '22 23:09

Eugenio Cuevas


SYS_GUID returns a GUID-- a globally unique ID. A SYS_GUID is a RAW(16). It does not generate an incrementing numeric value.

If you want to create an incrementing numeric key, you'll want to create a sequence.

CREATE SEQUENCE name_of_sequence   START WITH 1   INCREMENT BY 1   CACHE 100; 

You would then either use that sequence in your INSERT statement

INSERT INTO name_of_table( primary_key_column, <<other columns>> )   VALUES( name_of_sequence.nextval, <<other values>> ); 

Or you can define a trigger that automatically populates the primary key value using the sequence

CREATE OR REPLACE TRIGGER trigger_name   BEFORE INSERT ON table_name   FOR EACH ROW BEGIN   SELECT name_of_sequence.nextval     INTO :new.primary_key_column     FROM dual; END; 

If you are using Oracle 11.1 or later, you can simplify the trigger a bit

CREATE OR REPLACE TRIGGER trigger_name   BEFORE INSERT ON table_name   FOR EACH ROW BEGIN   :new.primary_key_column := name_of_sequence.nextval; END; 

If you really want to use SYS_GUID

CREATE TABLE table_name (   primary_key_column raw(16) default sys_guid() primary key,   <<other columns>> ) 
like image 40
Justin Cave Avatar answered Sep 26 '22 23:09

Justin Cave