Foreign key
A foreign key is a column or set of columns in a relational table whose values reference a candidate key in the same or another table, establishing a relationship and supporting referential integrity.
Grokking Relational Database Design
A foreign key constraint requires values in a child table's foreign key column to correspond to valid primary key values in a parent table and restricts operations that would break that correspondence.
- How it is used
- Connects related tables and preserves referential integrity during database implementation and data management.
- How deep it goes
- Defines foreign keys, demonstrates creation and later addition, and traces rejected inserts and deletions that would create orphan records.
- Examples
- Uses product and review rows, user-to-review relationships, and author and book tables missing a needed constraint.
- What you can do after
- Build it from scratch by defining named FOREIGN KEY ... REFERENCES constraints when creating or altering child tables.
Read how Grokking Relational Database Design explains foreign key →
Build Financial Software with Generative AI (From Scratch)
A foreign key is a database constraint that references a key in another table to link related records and enforce valid relationships.
- How it is used
- Enforces valid ACH file, batch, record, and exception relationships while storing uploaded payment data.
- How deep it goes
- Defines foreign keys and demonstrates REFERENCES, NOT NULL, cascading actions, nullable links, and resulting test refactors.
- Examples
- Uses ACH file uploads, raw records, hierarchical batches and entries, and an exceptions table with optional record references.
- What you can do after
- Apply it via SQL REFERENCES and cascade clauses to preserve related ACH data and adapt test setup.
Read how Build Financial Software with Generative AI (From Scratch) explains foreign key →
Learn SQL in a Month of Lunches
A foreign key constraint is a database rule requiring designated column values to reference valid rows identified by a key in another table.
- How it is used
- Enforces valid relationships between parent and child tables while organizing repeated business data as key references.
- How deep it goes
- Defines parent-child rules, demonstrates ALTER TABLE syntax, and cautions about existing data, naming, diagrams, and indexes.
- Examples
- Uses orders and titles, title categories, order headers, order items, authors, payments, shipments, and a database relationship diagram.
- What you can do after
- Operate/configure it by adding named FOREIGN KEY constraints that reference parent-table keys and by checking existing data first.
Read how Learn SQL in a Month of Lunches explains foreign key →
The Well-Grounded Python Developer: How the pros use Python and Flask
A foreign key is a column or set of columns that references a primary key in another relational database table to establish a relationship.
- How it is used
- Connects separately stored customer, order, and address records so related data can be retrieved without repeating it in every row.
- How deep it goes
- Defines primary-to-foreign-key references, then works through SQL joins and SQLAlchemy model declarations without detailing database enforcement.
- Examples
- Uses customer orders and addresses, an invoice JOIN query, and Python models with Customer.orders.
- What you can do after
- Apply it via SQL joins or SQLAlchemy models to connect orders with customers, addresses, items, and products.
DuckDB in Action
A foreign key is a schema constraint requiring values in one table to reference corresponding values in another table.
- How it is used
- Uses foreign keys to preserve integrity while defining related monitoring tables.
- How deep it goes
- Defines declaration syntax and load-performance trade-offs, then distinguishes enforcement from joining.
- Examples
- Demonstrates system and reading tables, system_id-to-id references, composite reading keys, and joins between readings and systems.
- What you can do after
- Apply foreign keys via DuckDB CREATE TABLE definitions and distinguish integrity enforcement from SQL join behavior.
Build Python Web Apps with Streamlit
A foreign key constraint requires values in one table's column to correspond to existing values in a referenced table's key column.
- How it is used
- Models one-to-many relationships between tables while preserving valid references between related rows.
- How deep it goes
- Defines the relationship and shows its PostgreSQL declaration, but does not develop broader constraint-management or deletion behavior.
- Examples
- Uses users.username, haikus.author, the alice user, and inserted haiku rows to demonstrate the relationship.
- What you can do after
- Apply it via PostgreSQL by declaring a FOREIGN KEY that references a primary-key column in another table.
Read how Build Python Web Apps with Streamlit explains foreign key →
100 SQL Server Mistakes and How to Avoid Them
A foreign key is a column or set of columns whose values reference a candidate key in another or the same table.
- How it is used
- Protects relationships between normalized tables by rejecting references to nonexistent rows.
- How deep it goes
- Explains key types, referential integrity, operational failure scenarios, corrective SQL, and ERD usage, but not broader constraint internals.
- Examples
- Uses Employees and Offices, Sales Orders, and PurchaseOrderHeaders with PurchaseOrderDetails to demonstrate valid and invalid references.
- What you can do after
- Operate/configure SQL Server schemas by identifying missing relationships, repairing invalid data, and adding foreign key constraints.
Read how 100 SQL Server Mistakes and How to Avoid Them explains foreign key →
Just Use Postgres!
A foreign key constraint requires values in a column or set of columns to match existing unique or primary-key values in a referenced table.
- How it is used
- Uses database-level relationships to prevent reviews from referencing nonexistent products or customers.
- How deep it goes
- Explains references, uniqueness requirements, index-based verification, failed inserts, and blocked deletes without covering cascading actions.
- Examples
- Demonstrates product reviews, customer accounts, invalid product identifiers, valid inserts, blocked account deletion, and soft deletion.
- What you can do after
- Apply it via PostgreSQL ALTER TABLE statements and diagnose rejected inserts or deletes caused by broken references.
AI-Powered Search
A foreign key is a value or column in one table that references an entity or key in another table to link records.
- How it is used
- Links related rows or search documents so separate records can be combined or connected.
- How deep it goes
- Defines exact and fuzzy relationships with table and document examples, but stops short of implementation code.
- Examples
- Uses employee-company tables, shared “Haystack” documents, and full-name versus first-name entity references.
- What you can do after
- Evaluate how shared identifiers or resolved entity references connect records across structured tables and search documents.
Rust Servers, Services, and Apps
A foreign-key constraint is a database rule requiring values in one table's column to reference valid values in another table's key column.
- How it is used
- Enforces the tutor–course relationship in the database, blocking tutor deletion while course records still reference that tutor.
- How deep it goes
- Demonstrates the deletion consequence and required cleanup, but does not detail constraint declaration or implementation.
- Examples
- Uses an exercise that deletes a tutor with existing courses, then removes those courses before retrying.
- What you can do after
- Diagnose/debug with it: explain a rejected tutor deletion by checking for linked course records and removing them first.
Read how Rust Servers, Services, and Apps explains foreign key →
Effective Data Analysis
A foreign key is a column or set of columns whose values reference a key in another table to represent a relationship between rows.
- How it is used
- Connects new warehouse data to existing tables so analysts can query combined business information efficiently.
- How deep it goes
- Explains key matching, prejoining, quality checks, and grain, then applies relationships to fact, dimension, and star-schema design.
- Examples
- Uses dates, user IDs, location keys, customer identifiers, addresses, utilities, and customer activities as relationship examples.
- What you can do after
- Apply it via SQL and schema design to join reliable tables and propose connected fact and dimension models.
Kafka Streams in Action, Second Edition
A foreign key is a field whose value references a key in another table or record set.
- How it is used
- Enables KTable joins when a left value references a right table's key but the tables have different primary keys.
- How deep it goes
- Explains extraction, null handling, update triggers, and repartitioning, then illustrates lookup and matching internals.
- Examples
- Uses client contracts keyed by client ID whose values reference commodity codes keyed in a commodity table.
- What you can do after
- Apply it via Kafka Streams by supplying a foreign-key extractor and joiner to KTable.join.
Read how Kafka Streams in Action, Second Edition explains foreign key →
Java Persistence with Spring Data and Hibernate epub
A foreign key constraint enforces a referential relationship between values in one table and a candidate or primary key in another table.
- How it is used
- Enforces referential integrity while representing associations between relational tables.
- How deep it goes
- Defines the constraint, demonstrates SQL syntax, compares association cardinalities, and stops short of ORM inheritance solutions.
- Examples
- Uses User and BillingDetails tables, a USER_BILLINGDETAILS link table, and an IMAGE table referencing ITEM.
- What you can do after
- Apply it via SQL to preserve associations and choose schemas for one-to-many or many-to-many relationships.
Read how Java Persistence with Spring Data and Hibernate epub explains foreign key →
PostgreSQL Mistakes and How to Avoid Them
A foreign key relationship is a referential association in which columns in one table identify rows in another input table.
- How it is used
- Replaces table inheritance for parent-child relationships and enforces references while supporting partitioning-oriented redesigns.
- How deep it goes
- Explains migration steps, deferred validation, live-system triggers, and referencing-side indexing, but not constraint internals.
- Examples
- Uses events and meetings migration plus an invoices-to-customers query whose plan improves after indexing the foreign-key column.
- What you can do after
- Operate/configure PostgreSQL relationships, migrate inherited tables, and diagnose missing indexes with EXPLAIN ANALYZE.
Read how PostgreSQL Mistakes and How to Avoid Them explains foreign key →
Starting Data Analytics with Generative AI and Python
A foreign key is a column or set of columns whose values reference identifying values in another dataset's primary key.
- How it is used
- Validates relationships between DataFrames before cross-referencing or analyzing records from multiple data sources.
- How deep it goes
- Defines reference direction, explains expected relationships, and demonstrates violation detection, while deferring resolution as an exercise.
- Examples
- Uses customer orders, order payments, customer identifiers, order_id values, and a Python listing for detecting missing references.
- What you can do after
- Diagnose foreign-key violations by comparing identifier columns and inspecting unmatched rows before choosing corrective action.
Read how Starting Data Analytics with Generative AI and Python explains foreign key →
Django in Action
A foreign key relationship is a database relationship in which records in one table reference a record in another table through a foreign key.
- How it is used
- Connects related database objects in Django ORM models, especially when multiple records belong to one parent record.
- How deep it goes
- Defines stored IDs and contrasts one-to-many with relationship-table many-to-many designs, without detailing migrations or query code.
- Examples
- Uses venues and rooms for one-to-many relationships, and musicians and bands for many-to-many relationships.
- What you can do after
- Apply it via Django ORM by distinguishing ForeignKey columns from ManyToManyField relationship tables.
.NET in Action, Second Edition
A foreign key is a relational-database column or set of columns whose values reference a key in another table.
- How it is used
- Maps related EF Core entities to relational tables through principal and dependent keys.
- How deep it goes
- Explains convention-based mapping, navigation properties, relationship cardinality, and deferred database-level one-to-one enforcement.
- Examples
- Uses Book, Rating, and Tag entities, plus generated tables and C# queries that save and print ratings.
- What you can do after
- Apply it via EF Core conventions to model and query related Book and Rating entities.
Read how .NET in Action, Second Edition explains foreign key →
Unit Testing Principles, Practices, and Patterns
A foreign key constraint requires values in one database column or set of columns to correspond to valid values in another table.
- How it is used
- Protects integration-test cleanup by requiring dependent rows to be removed in an order that preserves database integrity.
- How deep it goes
- Explains deletion-order consequences but stops short of defining constraint syntax or relationship modeling.
- Examples
- Uses a manual SQL script in a C# integration-test base class to delete rows from User and Company tables.
- What you can do after
- Operate/configure integration-test isolation by writing deletion scripts that respect foreign key constraints.
Read how Unit Testing Principles, Practices, and Patterns explains foreign key →
Related concepts
- primary keyuses · A primary key is a designated column or attribute, or combination of them, that uniquely identifies each row or item in a table or data store.
- constraintkind of · A constraint is a condition or rule that restricts the permissible states, values, actions, or solutions in a system or problem.
- on delete cascadeconfigured by · ON DELETE CASCADE is a relational foreign-key action that automatically deletes referencing rows when their referenced row is deleted.
- referential integrityimplements · Referential integrity is the database property that references between related records correspond to valid existing keys or records.
- joinused by · A join is an operation that combines records or rows from multiple data sources according to specified matching conditions or relationships.
- schemapart of · A data schema is a formal specification of the structure, fields, types, relationships, and constraints of data.
These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.

















