72

I want to add a new auto increment primary column to a existing table which has data. How do I do that?

I first added a column and then try to add a sequence after that, I lost how to insert and make that column as primary key.

1
  • 6
    It's astounding that this question has been viewed nearly 27000 times but only has 16 upvotes. Commented May 15, 2014 at 15:29

4 Answers 4

52

Say your table is called t1 and your primary-key is called id
First, create the sequence:

create sequence t1_seq start with 1 increment by 1 nomaxvalue; 

Then create a trigger that increments upon insert:

create trigger t1_trigger before insert on t1 for each row begin select t1_seq.nextval into :new.id from dual; end; 
Sign up to request clarification or add additional context in comments.

4 Comments

can i change column id of new column
yes, that's exactly what :new.id is doing
There's no need to select into in 11g, you can assign the sequence value directly to :new.id.
Had to take the sequence from Hermit's answer.
34

If you have the column and the sequence, you first need to populate a new key for all the existing rows. Assuming you don't care which key is assigned to which row

UPDATE table_name SET new_pk_column = sequence_name.nextval; 

Once that's done, you can create the primary key constraint (this assumes that either there is no existing primary key constraint or that you have already dropped the existing primary key constraint)

ALTER TABLE table_name ADD CONSTRAINT pk_table_name PRIMARY KEY( new_pk_column ) 

If you want to generate the key automatically, you'd need to add a trigger

CREATE TRIGGER trigger_name BEFORE INSERT ON table_name FOR EACH ROW BEGIN :new.new_pk_column := sequence_name.nextval; END; 

If you are on an older version of Oracle, the syntax is a bit more cumbersome

CREATE TRIGGER trigger_name BEFORE INSERT ON table_name FOR EACH ROW BEGIN SELECT sequence_name.nextval INTO :new.new_pk_column FROM dual; END; 

5 Comments

i have done the above code and it works fine. but the column id value is changed the last inserted column got last column id. but in case if some of our colleagues insert data into table they start with inserting primary key column value then it will show error. Is there any way to change the column id.
@mallikarjun - I'm having a hard time understanding what it is you're asking. What, exactly, is someone doing? What, exactly, is the error?
CAN I CHANGE COLUMN ID OF NEW PRIMARY KEY
@mallikarjun - What does "column id" mean? What, exactly, are you trying to change? Are you trying to change the order of columns in a table?
The second trigger query (SELECT INTO) is necessary for 10gR2
19

Snagged from Oracle OTN forums

Use alter table to add column, for example:

alter table tableName add(columnName NUMBER); 

Then create a sequence:

CREATE SEQUENCE SEQ_ID START WITH 1 INCREMENT BY 1 MAXVALUE 99999999 MINVALUE 1 NOCYCLE; 

and, the use update to insert values in column like this

UPDATE tableName SET columnName = seq_test_id.NEXTVAL 

5 Comments

can i change column id of new column
Do you mean you want the column to be the first column in the table rather than the end of the table?
Anyway, if that is the case, Outside of dropping and re-creating the table, in oracle the answer is no. Typically the order of the columns is irrelevant. If you require the columns be in a specific order, I would suggest creating a view of the table with the columns in the order you want.
yes exactly i want to add this pk as row id 1
Yeah, like I said before, your options in oracle are to drop and re-create the table (rename table, create new table in the right order, reinsert to new table)...or to just simply create a view that selects from your table in the order you want.
5

You can use the Oracle Data Modeler to create auto incrementing surrogate keys.

Step 1. - Create a Relational Diagram

You can first create a Logical Diagram and Engineer to create the Relational Diagram or you can straightaway create the Relational Diagram.

Add the entity (table) that required to have auto incremented PK, select the type of the PK as Integer.

Step 2. - Edit PK Column Property

Get the properties of the PK column. 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.

Step 3. - Additional Information

Additional information relating to the auto increment can be specified by selecting the 'Auto Increment' tab.

  • Start With
  • Increment By
  • Min Value
  • Max Value
  • Cycle
  • Disable Cache
  • Order
  • Sequence Name
  • Trigger Name
  • Generate Trigger

It is usually a good idea to mention the sequence name, so that it will be useful in PL/SQL.

Click OK (Apply) to the Column Properties dialog box.

Click OK (Apply) to the Table Properties dialog box.

Table appears in the Relational Diagram.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.