Db2 Troubleshooting
SQLCODE -530 foreign key violation
SQLCODE -530: foreign key violation — the parent row for your foreign key does not exist.
What it means
- After every EXEC SQL, DB2 returns a SQLCODE: 0 means success, +100 means no row found, and negative values mean an error.
Common causes
- Inserting a child row with a parent key that is not in the parent table.
- Wrong key value from input data.
How to diagnose
- Verify the parent key exists: SELECT it in SPUFI.
- Check the input key values.
How to fix it
- Insert the parent row first, or correct the key.
- Validate keys against the parent table before insert.
Example: Validate against the parent first
EXEC SQL SELECT 1 INTO :WS-DUMMY FROM DEPT
WHERE DEPTNO = :WS-DEPT END-EXEC.
*> only insert employee if department exists
Parent-first ordering prevents -530.
Interview tip
- Q: How would you troubleshoot a SQLCODE -530 foreign key violation in production?
- A: I would check the SQLCODE and SQLERRM immediately after the failing statement, reproduce in SPUFI, then apply the specific fix.
Prevention checklist
- Always check SQLCODE after every EXEC SQL statement.
- Log the SQLCODE, SQLSTATE and failing statement for support.
