Foreign Key
A `FOREIGN KEY` is a column or a set of columns in a DB2 for z/OS table that refers to the `PRIMARY KEY` (or a `UNIQUE KEY`) in the same table or another table. It establishes a **referential constraint** between the two tables, ensuring data integrity by enforcing predefined relationships.
Key Characteristics
-
- Referential Integrity: Enforces rules to maintain consistency between related tables, preventing the creation of orphaned child records or deletion of parent records that still have dependent children.
- Parent/Child Relationship: The table containing the
PRIMARY KEYis designated as the parent table, while the table with theFOREIGN KEYis the child table. - Matching Data Types: The
FOREIGN KEYcolumns must have data types and lengths compatible with thePRIMARY KEYcolumns they reference to allow for proper comparison and linking. - NULL Values: A
FOREIGN KEYcan containNULLvalues, indicating that a child record is not currently related to any parent record, unless the column is explicitly defined asNOT NULL. - DB2 Implementation: In DB2 for z/OS,
FOREIGN KEYconstraints are defined usingCREATE TABLEorALTER TABLEDDL statements, specifying the referenced table and columns. - Referential Actions: DB2 allows specifying actions to take when a parent row is deleted or updated, such as
CASCADE(delete/update child rows),SET NULL(set child FK to NULL),RESTRICT(prevent parent action), orNO ACTION(defer integrity check).
Use Cases
-
- Order Management System: Linking an
Orderstable (child) to aCustomerstable (parent) usingCustomerIDas theFOREIGN KEYto ensure every order placed belongs to an existing customer record. - Inventory Tracking: Connecting an
Inventorytable to aProductstable, whereProductIDinInventoryis aFOREIGN KEYreferencingProductIDinProducts, ensuring only valid products are inventoried. - Hierarchical Data Representation: Modeling organizational structures where an
Employeetable might have aFOREIGN KEYManagerIDreferencingEmployeeIDwithin the same table (a self-referencing foreign key). - Financial Transaction Systems: Linking
Transaction_Detailsrecords to aMaster_Accountrecord, whereAccountIDin the details table is aFOREIGN KEYto the master table.
- Order Management System: Linking an
Related Concepts
FOREIGN KEYs are fundamental to relational database management systems (RDBMS) like DB2 for z/OS, working in conjunction with PRIMARY KEYs and UNIQUE KEYs to enforce referential integrity. They are a core component of data modeling and database design, ensuring that relationships between entities are correctly maintained and that orphaned records cannot exist. This data consistency is crucial for applications written in COBOL, accessed via CICS transactions, or processed through JCL batch jobs, as it guarantees the reliability of data retrieved and updated.
- Define Explicitly: Always define
FOREIGN KEYconstraints explicitly in yourCREATE TABLEorALTER TABLEDDL to leverage DB2's built-in integrity enforcement mechanisms. - Choose Appropriate Referential Actions: Carefully select
ON DELETEandON UPDATEactions (CASCADE,SET NULL,RESTRICT,NO ACTION) based on the specific business rules to prevent unintended data loss or inconsistencies. - Index Foreign Keys: Create indexes on
FOREIGN KEYcolumns, especially if they are frequently used inJOINoperations or as part ofWHEREclauses, to significantly improve query performance. - Document Relationships: Clearly document all
PRIMARY KEYandFOREIGN KEYrelationships in your database schema for better understanding, maintenance, and troubleshooting by developers and DBAs. - Monitor Constraint Violations: Implement monitoring for
SQLCODEs related to constraint violations (e.g.,-530for referential integrity violation) to identify and address data integrity issues promptly.