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.

User97693321
  • 3,336
  • 7
  • 45
  • 69
mallikarjun
  • 1,862
  • 5
  • 23
  • 47

4 Answers4

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;
Nir Alfasi
  • 53,191
  • 11
  • 86
  • 129
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;
Justin Cave
  • 227,342
  • 24
  • 367
  • 384
  • 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 Jul 13 '12 at 05:22
  • @mallikarjun - I'm having a hard time understanding what it is you're asking. What, exactly, is someone doing? What, exactly, is the error? – Justin Cave Jul 13 '12 at 05:24
  • CAN I CHANGE COLUMN ID OF NEW PRIMARY KEY – mallikarjun Jul 13 '12 at 05:35
  • @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? – Justin Cave Jul 13 '12 at 05:39
  • The second trigger query (SELECT INTO) is necessary for 10gR2 – Trent Jan 20 '16 at 18:38
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
Ben
  • 51,770
  • 36
  • 127
  • 149
Hermit
  • 377
  • 1
  • 3
  • can i change column id of new column – mallikarjun Jul 13 '12 at 05:23
  • Do you mean you want the column to be the first column in the table rather than the end of the table? – Hermit Jul 13 '12 at 05:32
  • 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. – Hermit Jul 13 '12 at 05:39
  • yes exactly i want to add this pk as row id 1 – mallikarjun Jul 13 '12 at 05:40
  • 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. – Hermit Jul 13 '12 at 05:43
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.