Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to set auto increment column with sql developer

How do I set a column to increment automatically with Oracle SQL Developer? Why is the form disabled?

Oracle SQL Developer

Note: The image shows the Data Modeler, but the question and top answer talk about editing an existing database.

like image 910
Fendi Tri Cahyono Avatar asked Jun 12 '12 03:06

Fendi Tri Cahyono


People also ask

How do you auto increment an existing column in SQL?

If you're looking to add auto increment to an existing table by changing an existing int column to IDENTITY , SQL Server will fight you. You'll have to either: Add a new column all together with new your auto-incremented primary key, or. Drop your old int column and then add a new IDENTITY right after.

Is there auto increment in Oracle?

IDENTITY columns were introduced in Oracle 12c, allowing for simple auto increment functionality in modern versions of Oracle.

What is auto increment in Oracle SQL?

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

If you want to make your PK auto increment, you need to set the ID column property for that primary key.

  1. Right click on the table and select "Edit".
  2. In "Edit" Table window, select "columns", and then select your PK column.
  3. Go to ID Column tab and select Column Sequence as Type. This will create a trigger and a sequence, and associate the sequence to primary key.

See the picture below for better understanding.

enter image description here

// My source is: http://techatplay.wordpress.com/2013/11/22/oracle-sql-developer-create-auto-incrementing-primary-key/

like image 101
Daniel Perník Avatar answered Sep 21 '22 17:09

Daniel Perník


Unfortunately oracle doesnot support auto_increment like mysql does. You need to put a little extra effort to get that.

say this is your table -

CREATE TABLE MYTABLE (   ID NUMBER NOT NULL,   NAME VARCHAR2(100)   CONSTRAINT "PK1" PRIMARY KEY (ID) ); 

You will need to create a sequence -

CREATE SEQUENCE S_MYTABLE START WITH 1 INCREMENT BY 1 CACHE 10; 

and a trigger -

CREATE OR REPLACE TRIGGER T_MYTABLE_ID BEFORE INSERT ON MYTABLE REFERENCING NEW AS NEW FOR EACH ROW BEGIN   if(:new.ID is null) then   SELECT S_MYTABLE.nextval   INTO :new.ID   FROM dual;   end if; END; /  ALTER TRIGGER "T_MYTABLE_ID" ENABLE; 
like image 20
Kshitij Avatar answered Sep 19 '22 17:09

Kshitij