How do I set a column to increment automatically with Oracle SQL Developer? Why is the form disabled?
Note: The image shows the Data Modeler, but the question and top answer talk about editing an existing database.
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.
IDENTITY columns were introduced in Oracle 12c, allowing for simple auto increment functionality in modern versions of Oracle.
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.
If you want to make your PK auto increment, you need to set the ID column property for that primary key.
See the picture below for better understanding.
// My source is: http://techatplay.wordpress.com/2013/11/22/oracle-sql-developer-create-auto-incrementing-primary-key/
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;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With