Keys, Identifiers and Referential Integrity
Keys support identity inside a model; constraints protect relationships between records.
A key is one or more attributes used to identify a row or connect it to another row. Referential integrity means references point to valid related records, subject to the model's rules.
Key types
| Type | Role | Example |
|---|---|---|
| Candidate key | A minimal attribute set that can identify a row | Product code, if unique and stable |
| Primary key | Selected key for a row | Product ID |
| Composite key | Several attributes identify one row together | Order ID + line number |
| Foreign key | Reference to a key in another relation | Order line's order ID |
| Surrogate key | Generated technical identifier | Customer dimension key |
Referential actions are business choices
When a referenced row is updated or removed, a database may reject the change, cascade it or set a reference to null. Choose behavior based on lifecycle and audit needs. Deleting a customer, for example, may be prohibited, anonymized, archived or detached depending on the policy and use.
Constraints and testing
Where supported, database constraints can enforce uniqueness, required values and valid references at the storage boundary. Tests are still needed for rules that span systems, depend on timing, or cannot be expressed as a local constraint. PostgreSQL's documentation describes primary, unique, foreign-key, not-null and check constraints, with product-specific behavior and caveats: PostgreSQL constraints reference.
Common pitfalls
- Using a mutable name or email as a permanent key.
- Assuming a source identifier is globally unique.
- Allowing nulls in a key without defining what they mean.
- Deleting parent records when downstream history must remain explainable.
- Using a surrogate key while failing to preserve business and source identifiers.
Review checklist
- Are key columns unique and required where appropriate?
- Does every foreign reference have a defined target and lifecycle?
- Are duplicate, unknown and late-arriving records handled?
- Can a consumer trace a row to its business identity?
Related guides
- Entity Identity, Keys and Identifiers
- Relationships and Cardinality in Data Models
- Data Types, Constraints and Nullability
Separate key purposes
A primary key identifies a row within a model. A business key has meaning in a business process, such as an order number. A source key identifies a record within a producer system. A surrogate key is assigned to support implementation or versioning. One value can serve several roles, but document which guarantees apply and where uniqueness is scoped.
Natural identifiers can be readable and useful for integration, but they may change, be reused or expose sensitive information. Generated identifiers can remain stable through attribute changes, but they need a mapping to business and source identities. Choose based on stability, scope, exposure, interoperability and lifecycle—not on a universal preference.
Protect references and handle unresolved records
Referential integrity means a reference resolves to an intended parent under a defined rule. In a transactional database, a foreign key may enforce this. In an asynchronous pipeline, a child can arrive before its parent; the design may stage it, retry resolution, or represent an explicit unresolved status. Set an age threshold and owner for unresolved references so they do not become invisible permanent exceptions.
Soft deletion and historical retention complicate references. A referenced parent may no longer be active but still be needed to explain an old transaction. Define whether deletion means inaccessible, logically retired, or physically removed under an approved retention rule. Preserve enough history for valid downstream use without retaining data beyond policy.
Use composite keys when scope requires them
A source identifier may be unique only together with a source-system code, tenant or organization. A composite key expresses that scope. If a consumer needs a simple key, a generated identifier can be mapped to the composite natural key. Do not drop scope during integration or unrelated records may collide.
Uniqueness can also be conditional. An external identifier may be unique among active accounts but reused after closure. State whether uniqueness applies across all history or only current records and enforce the appropriate rule. Changes in those assumptions may require careful migration and duplicate resolution.
Test key quality and joins
- Check that each declared key is unique at the stated scope.
- Measure null, duplicate and unresolved reference rates.
- Verify that joins return expected row counts and do not multiply measures.
- Test source key reuse, merge, split and correction behavior.
- Confirm that key exposure complies with access and privacy controls.
When identities are uncertain, represent confidence and provenance rather than manufacturing certainty. A matching algorithm may produce candidates; an accountable process should decide merges where the impact warrants human review.
Continue to Entity Identity, Keys and Identifiers, Relationships and Cardinality in Data Models and Data Quality Dimensions, Rules and Monitoring.
Manage keys during migration
When moving systems, preserve source identifiers and define whether target keys are generated anew or mapped. Keep a crosswalk with source, target, validity, and provenance. Test duplicate, missing, reused, and changed natural keys. Do not assume a surrogate key remains stable if the target is rebuilt.
Validate foreign-key coverage and uniqueness after each migration step. Reconcile counts and document orphan handling. Provide downstream consumers with a compatibility window when identifiers change.