# SQLCODE -624: Table already has a primary key

Source: https://wndsoftware.com/mainframe-errors/sqlcode-624

### 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.SYSTABCONST WHERE TBCREATOR = 'PROD' AND TBNAME = 'CUSTOMER'`. TYPE P is the primary key and U a unique constraint.
- 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


```text
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](https://www.ibm.com/docs/en/db2-for-zos/13.0.0?topic=codes-624). Use the documentation for your installed release.

[Report a correction to WND Software](https://wndsoftware.com/contact#message).

Technical reviewer: [Albert Joseph](https://wndsoftware.com/authors/albert-joseph), WND Software.

Last reviewed 2026-10-18 by the WND Software team. Written in our own words. For the full reference, check IBM’s Db2 for z/OS documentation for your release.


### Related codes

[SQLCODE -601 Object name already exists Read →](https://wndsoftware.com/mainframe-errors/sqlcode-601)
[SQLCODE -542 Key column allows nulls Read →](https://wndsoftware.com/mainframe-errors/sqlcode-542)
[SQLCODE -538 Foreign key doesn't match parent key Read →](https://wndsoftware.com/mainframe-errors/sqlcode-538)
[SQLCODE -540 Table definition incomplete, missing index Read →](https://wndsoftware.com/mainframe-errors/sqlcode-540)
[SQLCODE -603 Duplicate rows block a unique index Read →](https://wndsoftware.com/mainframe-errors/sqlcode-603)

### 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.

[Db2 SQLCODEs](https://wndsoftware.com/mainframe-errors/db2-sqlcodes) · [SQLCODE -612](https://wndsoftware.com/mainframe-errors/sqlcode-612) · [SQLCODE -679](https://wndsoftware.com/mainframe-errors/sqlcode-679)

[← All error codes](https://wndsoftware.com/mainframe-errors/)
