foreign key
A column holding another table's primary key value, which the database guarantees points at a row that exists.
In more detail
Declared with REFERENCES, it stops you inserting an order for a customer who does not exist and, depending on the delete action, stops you deleting a customer who still has orders. PostgreSQL always enforces foreign keys; SQLite enforces them only after PRAGMA foreign_keys = ON on each connection.
Example
customer_id INTEGER NOT NULL REFERENCES customers(id)Related terms
- primary key — The column, or columns, whose value uniquely identifies each row of a table and can never be NULL.
- navigation property — A property on an EF Core entity that points to related entities, such as an order's customer or lines.
- referential integrity — The guarantee that every foreign key value points at a row that really exists.
- referential action — The rule, declared on a foreign key, for what happens to child rows when the parent row they point at is deleted or changed.
- PRAGMA — A SQLite statement that reads or changes a setting of the database library or of the current connection.
- join — Combining rows from two tables into one result by pairing rows according to a rule, usually matching keys.
- unique constraint — A constraint that no two rows may have the same value in a column or combination of columns.
- relational database — A database that stores data in tables and links rows in one table to rows in another through shared key values.
Taught in
- Privacy Principles, GDPR Subject Rights & Minimization — Cybersecurity Fundamentals, GRC & Cryptography
- Entity Framework Core: Code First and Migrations — Modern .NET Core, C# & Enterprise Microservices
- Manual Object Mapping — Modern .NET Core, C# & Enterprise Microservices
- Why Tables And Relations Beat Files — PostgreSQL, Indexing & Relational Data Modeling
- Keys, Constraints And Referential Integrity — PostgreSQL, Indexing & Relational Data Modeling
- Inserting Data Safely — PostgreSQL, Indexing & Relational Data Modeling
- The Storage Decision: Relational Versus Document — Scalable System Design & Distributed Architecture