Home:ALL Converter>How to create id with AUTO_INCREMENT on Oracle?

How to create id with AUTO_INCREMENT on Oracle?

Ask Time:2012-07-02T23:09:48         Author:Sushan Ghimire

Json Formatter

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?

Author:Sushan Ghimire,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/11296361/how-to-create-id-with-auto-increment-on-oracle
Nate Zaugg :

Starting with Oracle 12c there is support for Identity columns in one of two ways:\n\n\nSequence + Table - In this solution you still create a sequence as you normally would, then you use the following DDL:\n\nCREATE TABLE MyTable\n(ID NUMBER DEFAULT MyTable_Seq.NEXTVAL, \n...)\nTable Only - In this solution no sequence is explicitly specified. You would use the following DDL:\n\nCREATE TABLE MyTable (ID NUMBER GENERATED AS IDENTITY, ...)\n\n\nIf you use the first way it is backward compatible with the existing way of doing things. The second is a little more straightforward and is more inline with the rest of the RDMS systems out there. ",
2016-02-24T15:53:29
emkays :

Here is complete solution w.r.t exception/error handling for auto increment, this solution is backward compatible and will work on 11g & 12c, specifically if application is in production.\n\nPlease replace 'TABLE_NAME' with your appropriate table name\n\n--checking if table already exisits\nBEGIN\n EXECUTE IMMEDIATE 'DROP TABLE TABLE_NAME';\n EXCEPTION WHEN OTHERS THEN NULL;\nEND;\n/\n\n--creating table\nCREATE TABLE TABLE_NAME (\n ID NUMBER(10) PRIMARY KEY NOT NULL,\n .\n .\n .\n);\n\n--checking if sequence already exists\nBEGIN\n EXECUTE IMMEDIATE 'DROP SEQUENCE TABLE_NAME_SEQ';\n EXCEPTION WHEN OTHERS THEN NULL;\nEND;\n\n--creating sequence\n/\nCREATE SEQUENCE TABLE_NAME_SEQ START WITH 1 INCREMENT BY 1 MINVALUE 1 NOMAXVALUE NOCYCLE CACHE 2;\n\n--granting rights as per required user group\n/\nGRANT SELECT, INSERT, UPDATE, DELETE ON TABLE_NAME TO USER_GROUP;\n\n-- creating trigger\n/\nCREATE OR REPLACE TRIGGER TABLE_NAME_TS BEFORE INSERT OR UPDATE ON TABLE_NAME FOR EACH ROW\nBEGIN \n -- auto increment column\n SELECT TABLE_NAME_SEQ.NextVal INTO :New.ID FROM dual;\n\n -- You can also put some other required default data as per need of your columns, for example\n SELECT SYS_CONTEXT('USERENV', 'SESSIONID') INTO :New.SessionID FROM dual;\n SELECT SYS_CONTEXT('USERENV','SERVER_HOST') INTO :New.HostName FROM dual;\n SELECT SYS_CONTEXT('USERENV','OS_USER') INTO :New.LoginID FROM dual; \n .\n .\n .\nEND;\n/\n",
2016-02-17T11:12:47
Eugenio Cuevas :

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:\n\nTable definition:\n\nCREATE TABLE departments (\n ID NUMBER(10) NOT NULL,\n DESCRIPTION VARCHAR2(50) NOT NULL);\n\nALTER TABLE departments ADD (\n CONSTRAINT dept_pk PRIMARY KEY (ID));\n\nCREATE SEQUENCE dept_seq START WITH 1;\n\n\nTrigger definition:\n\nCREATE OR REPLACE TRIGGER dept_bir \nBEFORE INSERT ON departments \nFOR EACH ROW\n\nBEGIN\n SELECT dept_seq.NEXTVAL\n INTO :new.id\n FROM dual;\nEND;\n/\n\n\nUPDATE:\n\nIDENTITY column is now available on Oracle 12c:\n\ncreate table t1 (\n c1 NUMBER GENERATED by default on null as IDENTITY,\n c2 VARCHAR2(10)\n );\n\n\nor specify starting and increment values, also preventing any insert into the identity column (GENERATED ALWAYS) (again, Oracle 12c+ only)\n\ncreate table t1 (\n c1 NUMBER GENERATED ALWAYS as IDENTITY(START with 1 INCREMENT by 1),\n c2 VARCHAR2(10)\n );\n\n\nAlternatively, Oracle 12 also allows to use a sequence as a default value:\n\nCREATE SEQUENCE dept_seq START WITH 1;\n\nCREATE TABLE departments (\n ID NUMBER(10) DEFAULT dept_seq.nextval NOT NULL,\n DESCRIPTION VARCHAR2(50) NOT NULL);\n\nALTER TABLE departments ADD (\n CONSTRAINT dept_pk PRIMARY KEY (ID));\n",
2012-07-02T15:16:05
sowmya dantuluri :

Query to create auto increment in oracle. In below query incrmnt column value will be auto incremented wheneever a new row is inserted\nCREATE TABLE table1(\nid RAW(16) NOT NULL ENABLE,\nincrmnt NUMBER(10,0) GENERATED ALWAYS AS IDENTITY\nMINVALUE 1 MAXVALUE 999999999999999999999999999 INCREMENT BY 1 START WITH 1 NOORDER NOCYCLE NOT NULL ENABLE,\nCONSTRAINT PK_table1 PRIMARY KEY (id) ENABLE);\n",
2021-10-12T11:02:17
Justin Cave :

SYS_GUID returns a GUID-- a globally unique ID. A SYS_GUID is a RAW(16). It does not generate an incrementing numeric value.\n\nIf you want to create an incrementing numeric key, you'll want to create a sequence. \n\nCREATE SEQUENCE name_of_sequence\n START WITH 1\n INCREMENT BY 1\n CACHE 100;\n\n\nYou would then either use that sequence in your INSERT statement\n\nINSERT INTO name_of_table( primary_key_column, <<other columns>> )\n VALUES( name_of_sequence.nextval, <<other values>> );\n\n\nOr you can define a trigger that automatically populates the primary key value using the sequence\n\nCREATE OR REPLACE TRIGGER trigger_name\n BEFORE INSERT ON table_name\n FOR EACH ROW\nBEGIN\n SELECT name_of_sequence.nextval\n INTO :new.primary_key_column\n FROM dual;\nEND;\n\n\nIf you are using Oracle 11.1 or later, you can simplify the trigger a bit\n\nCREATE OR REPLACE TRIGGER trigger_name\n BEFORE INSERT ON table_name\n FOR EACH ROW\nBEGIN\n :new.primary_key_column := name_of_sequence.nextval;\nEND;\n\n\nIf you really want to use SYS_GUID\n\nCREATE TABLE table_name (\n primary_key_column raw(16) default sys_guid() primary key,\n <<other columns>>\n)\n",
2012-07-02T15:16:08
ether6 :

This is how I did this on an existing table and column (named id):\n\nUPDATE table SET id=ROWNUM;\nDECLARE\n maxval NUMBER;\nBEGIN\n SELECT MAX(id) INTO maxval FROM table;\n EXECUTE IMMEDIATE 'DROP SEQUENCE table_seq';\n EXECUTE IMMEDIATE 'CREATE SEQUENCE table_seq START WITH '|| TO_CHAR(TO_NUMBER(maxval)+1) ||' INCREMENT BY 1 NOMAXVALUE';\nEND;\nCREATE TRIGGER table_trigger\n BEFORE INSERT ON table\n FOR EACH ROW\nBEGIN\n :new.id := table_seq.NEXTVAL;\nEND;\n",
2017-01-18T23:06:13
Nisar :

In Oracle 12c onward you could do something like,\n\nCREATE TABLE MAPS\n(\n MAP_ID INTEGER GENERATED ALWAYS AS IDENTITY (START WITH 1 INCREMENT BY 1) NOT NULL,\n MAP_NAME VARCHAR(24) NOT NULL,\n UNIQUE (MAP_ID, MAP_NAME)\n);\n\n\n\n\nAnd in Oracle (Pre 12c).\n\n-- create table\nCREATE TABLE MAPS\n(\n MAP_ID INTEGER NOT NULL ,\n MAP_NAME VARCHAR(24) NOT NULL,\n UNIQUE (MAP_ID, MAP_NAME)\n);\n\n-- create sequence\nCREATE SEQUENCE MAPS_SEQ;\n\n-- create tigger using the sequence\nCREATE OR REPLACE TRIGGER MAPS_TRG \nBEFORE INSERT ON MAPS \nFOR EACH ROW\nWHEN (new.MAP_ID IS NULL)\nBEGIN\n SELECT MAPS_SEQ.NEXTVAL\n INTO :new.MAP_ID\n FROM dual;\nEND;\n/\n",
2014-06-14T18:10:38
kumar venkatesan :

FUNCTION GETUNIQUEID_2 RETURN VARCHAR2\nAS\nv_curr_id NUMBER;\nv_inc NUMBER;\nv_next_val NUMBER;\npragma autonomous_transaction;\nbegin \nCREATE SEQUENCE sequnce\nSTART WITH YYMMDD0000000001\nINCREMENT BY 1\nNOCACHE\nselect sequence.nextval into v_curr_id from dual;\nif(substr(v_curr_id,0,6)= to_char(sysdate,'yymmdd')) then\nv_next_val := to_number(to_char(SYSDATE+1, 'yymmdd') || '0000000000');\nv_inc := v_next_val - v_curr_id;\nexecute immediate ' alter sequence sequence increment by ' || v_inc ;\nselect sequence.nextval into v_curr_id from dual;\nexecute immediate ' alter sequence sequence increment by 1';\nelse\ndbms_output.put_line('exception : file not found');\nend if;\nRETURN 'ID'||v_curr_id;\nEND;\n",
2017-08-02T11:22:07
Mark Harrison :

Here are three flavors:\n\n\nnumeric. Simple increasing numeric value, e.g. 1,2,3,....\nGUID. globally univeral identifier, as a RAW datatype.\nGUID (string). Same as above, but as a string which might be easier to handle in some languages.\n\n\nx is the identity column. Substitute FOO with your table name in each of the examples.\n\n-- numerical identity, e.g. 1,2,3...\ncreate table FOO (\n x number primary key\n);\ncreate sequence FOO_seq;\n\ncreate or replace trigger FOO_trg\nbefore insert on FOO\nfor each row\nbegin\n select FOO_seq.nextval into :new.x from dual;\nend;\n/\n\n-- GUID identity, e.g. 7CFF0C304187716EE040488AA1F9749A\n-- use the commented out lines if you prefer RAW over VARCHAR2.\ncreate table FOO (\n x varchar(32) primary key -- string version\n -- x raw(32) primary key -- raw version\n);\n\ncreate or replace trigger FOO_trg\nbefore insert on FOO\nfor each row\nbegin\n select cast(sys_guid() as varchar2(32)) into :new.x from dual; -- string version\n -- select sys_guid() into :new.x from dual; -- raw version\nend;\n/\n\n\nupdate:\n\nOracle 12c introduces these two variants that don't depend on triggers:\n\ncreate table mytable(id number default mysequence.nextval);\ncreate table mytable(id number generated as identity);\n\n\nThe first one uses a sequence in the traditional way; the second manages the value internally.",
2010-01-12T23:44:52
kumar venkatesan :

FUNCTION UNIQUE2(\n seq IN NUMBER\n) RETURN VARCHAR2\nAS\n i NUMBER := seq;\n s VARCHAR2(9);\n r NUMBER(2,0);\nBEGIN\n WHILE i > 0 LOOP\n r := MOD( i, 36 );\n i := ( i - r ) / 36;\n IF ( r < 10 ) THEN\n s := TO_CHAR(r) || s;\n ELSE\n s := CHR( 55 + r ) || s;\n END IF;\n END LOOP;\n RETURN 'ID'||LPAD( s, 14, '0' );\nEND;\n",
2017-08-02T11:25:31
Corrado Piola :

Oracle Database 12c introduced Identity, an auto-incremental (system-generated) column.\nIn the previous database versions (until 11g), you usually implement an Identity by creating a Sequence and a Trigger.\nFrom 12c onward, you can create your own Table and define the column that has to be generated as an Identity.",
2013-10-10T14:47:03
Mohammad Ali Abdullah :

Creating a Sequence:\nCREATE SEQUENCE SEQ_CM_LC_FINAL_STATUS\nMINVALUE 1 MAXVALUE 999999999999999999999999999 \nINCREMENT BY 1 START WITH 1 CACHE 20 NOORDER NOCYCLE;\n\nAdding a Trigger\nCREATE OR REPLACE TRIGGER CM_LC_FINAL_STATUS_TRIGGER\nBEFORE INSERT\nON CM_LC_FINAL_STATUS\nFOR EACH ROW\nBEGIN\n:NEW.LC_FINAL_STATUS_NO := SEQ_CM_LC_FINAL_STATUS.NEXTVAL;\nEND;\n\nThe first step is to create a SEQUENCE in your database, which is a data object that multiple users can access to automatically generate incremented values. As discussed in the documentation, a sequence in Oracle prevents duplicate values from being created simultaneously because multiple users are effectively forced to “take turns” before each sequential item is generated. –\nFinally, we’ll create our SEQUENCE that will be utilized later to actually generate the unique, auto incremented value. –\nWhile we have our table created and ready to go, our sequence is thus far just sitting there but never being put to use. This is where TRIGGERS come in. Similar to an event in modern programming languages, a TRIGGER in Oracle is a stored procedure that is executed when a particular event occurs. Typically a TRIGGER will be configured to fire when a table is updated or a record is deleted, providing a bit of cleanup when necessary. –\nIn our case, we want to execute our TRIGGER prior to INSERT into our CM_LC_FINAL_STATUS table, ensuring our SEQUENCE is incremented and that new value is passed onto our primary key column.",
2022-07-19T05:53:38
Phil Sandler :

Assuming you mean a column like the SQL Server identity column?\n\nIn Oracle, you use a SEQUENCE to achieve the same functionality. I'll see if I can find a good link and post it here.\n\nUpdate: looks like you found it yourself. Here is the link anyway:\nhttp://www.techonthenet.com/oracle/sequences.php",
2010-01-12T23:44:50
abhishek ringsia :

create trigger t1_trigger\n before insert on AUDITLOGS\n for each row\n begin\n select t1_seq.nextval into :new.id from dual;\n end;\n\n\nonly I have to just change the table name (AUDITLOGS) with your table name and new.id with new.column_name",
2015-09-21T06:46:21
sam :

it is called Identity Columns and it is available only from oracle Oracle 12c\n\nCREATE TABLE identity_test_tab\n(\n id NUMBER GENERATED ALWAYS AS IDENTITY,\n description VARCHAR2 (30)\n);\n\n\nexample of insert into Identity Columns as below \n\nINSERT INTO identity_test_tab (description) VALUES ('Just DESCRIPTION');\n\n\n\n 1 row created.\n\n\nyou can NOT do insert like below \n\nINSERT INTO identity_test_tab (id, description) VALUES (NULL, 'ID=NULL and DESCRIPTION');\n\n\n\n ERROR at line 1: ORA-32795: cannot insert into a generated always\n identity column\n\n\nINSERT INTO identity_test_tab (id, description) VALUES (999, 'ID=999 and DESCRIPTION');\n\n\n\n ERROR at line 1: ORA-32795: cannot insert into a generated always\n identity column\n\n\nuseful link",
2017-03-10T12:44:26
Kalpesh Soni :

oracle has sequences AND identity columns in 12c\n\nhttp://www.oracle-base.com/articles/12c/identity-columns-in-oracle-12cr1.php#identity-columns\n\nI found this but not sure what rdb 7 is\nhttp://www.oracle.com/technetwork/products/rdb/0307-identity-columns-128126.pdf",
2013-08-27T21:41:45
124 :

Trigger and Sequence can be used when you want serialized number that anyone can easily read/remember/understand. But if you don't want to manage ID Column (like emp_id) by this way, and value of this column is not much considerable, you can use SYS_GUID() at Table Creation to get Auto Increment like this.\n\nCREATE TABLE <table_name> \n(emp_id RAW(16) DEFAULT SYS_GUID() PRIMARY KEY,\nname VARCHAR2(30));\n\n\nNow your emp_id column will accept \"globally unique identifier value\".\nyou can insert value in table by ignoring emp_id column like this.\n\nINSERT INTO <table_name> (name) VALUES ('name value');\n\n\nSo, it will insert unique value to your emp_id Column.",
2014-01-18T06:08:37
yy