Search code examples
firebird

How do I alter an existing primary key in a table?


I have a table with a primary key already defined in it. I would like to add a column to it which must also be part of the primary key. How can that be done?


Solution

  • If PK_MY_TABLE is constraint name of existing primary key :

    ALTER TABLE MY_TABLE DROP CONSTRAINT PK_MY_TABLE;
    
    COMMIT;
    
    alter table MY_TABLE
    add constraint PK_MY_TABLE
    primary key (ID,ID_1);
    

    or

    ALTER TABLE MY_TABLE DROP CONSTRAINT PK_MY_TABLE, ADD CONSTRAINT PK_MY_TABLE PRIMARY KEY (ID,ID_1);