SQLCODE -624: Table already has a primary key
SQLCODE -624 (SQLSTATE 42889) means an ALTER TABLE tried to add a primary key to a table that already has one, or to add a unique constraint with the same columns as one the table already has.
Other names for this code
Also written as SQL -624, SQLCODE=-624, SQLCODE = -624, db2 -624, sqlstate 42889, SQL0624N, table already has a primary key.
What it means
A table can have only one primary key. It can have several unique constraints, but each must cover a different set of columns. When ALTER TABLE ADD PRIMARY KEY runs against a table that already has a primary key, or ADD UNIQUE repeats the columns and periods of an existing constraint, Db2 rejects it with -624. The table is unchanged. The message names the table.
-624 comes up mostly when DDL is run twice or when a migration script doesn't know what the table already has. It doesn't tell you whether the existing key uses the columns you wanted. Check the catalog before you change anything.
Common causes
- A migration script adds a primary key that the CREATE TABLE already defined.
- An ALTER script is rerun after it succeeded the first time.
- Someone tries to change the primary key by adding a new one instead of dropping the old one first.
- ADD UNIQUE repeats the columns of an existing unique constraint.
- A modeling or schema compare tool generates an ADD PRIMARY KEY because its copy of the model doesn't match what's in the catalog.
How to find the problem
- List the table's keys with
SELECT CONSTNAME, TYPE FROM SYSIBM.. TYPE P is the primary key and U a unique constraint.SYSTABCONST WHERE TBCREATOR = 'PROD' AND TBNAME = 'CUSTOMER' - Get each constraint's columns from SYSIBM.
SYSKEYCOLUSE, or the primary key columns from SYSIBM.SYSCOLUMNS where KEYSEQ is greater than zero. - Compare them with the key in your ALTER statement.
- If a script was rerun, read the output of the earlier run. The ALTER often succeeded the first time, and the second run is the one that failed.
How to fix it
- If the existing key is the one you wanted, skip the ALTER.
- To change the primary key, drop the old one with ALTER TABLE ... DROP PRIMARY KEY, then add the new one. Dropping a primary key also drops the foreign keys in other tables that reference it, so plan to re-create those.
- Make migration scripts check SYSIBM.
SYSTABCONST before adding keys. - If a tool generated the ALTER, refresh its copy of the model from the catalog before you generate the next change script.
What it looks like
DSNT408I SQLCODE = -624, ERROR: TABLE PROD.CUSTOMER ALREADY HAS A PRIMARY KEY OR UNIQUE CONSTRAINT WITH SPECIFIED COLUMNS AND PERIODS DSNT418I SQLSTATE = 42889 SQLSTATE RETURN CODE
IBM documentation
IBM documentation: -624. Use the documentation for your installed release.
Related codes
Questions
Can a table have more than one primary key?
No. It can have one primary key and several unique constraints on different columns.
Is -624 the same on Db2 for LUW?
Yes. Db2 for LUW reports SQL0624N with SQLSTATE 42889 when the table already has that kind of key.
What happens to foreign keys if I drop the primary key?
The foreign keys in other tables that reference it are dropped too. Re-create them after you add the new primary key.