Informix Error -703: Primary key on table table-name has a field with a null key value.
Cause and resolution
Primary key on table table-name has a field with a null key value.
An attempt was made either to insert a null value into a column that is part of a primary key, or to add a primary constraint to a table that has a NULL value in one of the key columns.
Oninit® Troubleshooting Guidance
Reasons / Common Causes
-703 fires in two related situations, per the official guidance: an INSERT/UPDATE attempting
to put a NULL value into a column that's part of a primary key, or an ALTER TABLE ... ADD CONSTRAINT PRIMARY KEY against a table that already has a NULL in one of the intended key
columns.
- An
INSERT/UPDATEsupplying NULL for a primary-key column — per the official guidance, primary keys never allow NULL in any of their columns, by definition. ALTER TABLE ... ADD CONSTRAINT PRIMARY KEYagainst existing data that already contains a NULL in one of the columns being made the key — per the official guidance, the constraint can't be added while that data exists.
Solutions / Resolution
- Supply a non-NULL value for every primary-key column on insert/update.
- For an existing table gaining a primary key, find and fix rows with NULL in the intended key
column(s) first:
update or delete those rows before retryingSELECT * FROM orders WHERE order_id IS NULL;ALTER TABLE ... ADD CONSTRAINT PRIMARY KEY.
Examples
Inserting NULL into a primary-key column
INSERT INTO orders (order_id, status) VALUES (NULL, 'pending');
-- -703: order_id is the primary key and can't be NULL
Adding a primary key against data with an existing NULL
ALTER TABLE orders ADD CONSTRAINT PRIMARY KEY (order_id);
-- -703: some existing rows already have order_id IS NULL
SELECT * FROM orders WHERE order_id IS NULL;
-- fix or remove those rows, then retry the ALTER TABLE
Diagnostic Checks
- Query the table for existing NULLs in the intended primary-key column(s) before adding the
constraint, or check the specific value being inserted/updated if the error is on a live
INSERT/UPDATE.
Related Errors / Related Topics
- -704 — "Primary key already exists on the table." A related primary-key error, about a table already having one rather than a NULL value in the key.
A NULL is never allowed in any primary-key column — clean up existing NULLs before adding the constraint, or supply a real value on insert/update.