Foreign Keys and Table Relationships
# CHAPTER 13
Foreign Keys and Table Relationships
1. Introduction
If a database only has one table, it is just an expensive Excel spreadsheet. The true power of PostgreSQL lies in the word "Relational." Data is split into multiple tables to eliminate redundancy, and then mathematically connected. In this chapter, we will learn how to use Foreign Keys to forge unbreakable links between tables, enforcing "Referential Integrity" so that relationships can never be accidentally severed.2. Learning Objectives
By the end of this chapter, you will be able to:- Define a Foreign Key constraint.
- Architect One-to-Many (1:N) relationships.
- Architect Many-to-Many (N:M) relationships using Pivot tables.
- Understand Referential Integrity.
-
Use
ON DELETE CASCADEto manage child records automatically.
3. The FOREIGN KEY Constraint
A Foreign Key is simply a column in a "Child" table that holds the Primary Key of a "Parent" table.
By explicitly defining a Foreign Key constraint, PostgreSQL actively monitors the relationship. If a PHP application tries to insert an order for User #99, but User #99 does not exist in the users table, PostgreSQL will reject the query instantly!
4. One-to-Many Relationships (1:N)
This is the most common relationship in database design.- One User can have Many Orders.
- One Department can have Many Employees.
The Rule: The Foreign Key always goes in the "Many" table (the Child table).
5. Referential Integrity and ON DELETE CASCADE
What happens if HR completely deletes the "Sales" department (ID: 5) from the parent table, but there are still 100 employees in the child table who have a department_id of 5?
Those 100 employees become "Orphans." This is called corrupted data.
By default, PostgreSQL will strictly block you from deleting the Sales department, throwing an error to protect the children.
If you want PostgreSQL to automatically delete all the child employees when the parent department is deleted, you use ON DELETE CASCADE.
6. Many-to-Many Relationships (N:M)
A Student can enroll in Many Classes. A Class holds Many Students. You cannot put aclassid in the Student table, because they take 5 classes. You cannot put a studentid in the Class table, because it holds 30 students.
The Solution: You must build a 3rd table in the middle, called a "Pivot" (or Junction) table.
7. One-to-One Relationships (1:1)
One User has exactly One Social Security Number. In 95% of cases, you should just put the SSN as a column in theusers table.
However, if the SSN data is highly sensitive and you want to isolate it in a separate secure table, you put a Foreign Key in the secure table, and add a UNIQUE constraint to it so it can only link to the User once.
8. Mini Project: E-Commerce Architecture
Let's design the ultimate E-Commerce setup: Users, Orders, and Products.9. Common Mistakes
-
Creating the Child Table First: You cannot create the
employeestable if thedepartmentstable does not exist yet. You must always construct the Parent tables before the Child tables.
-
Data Type Mismatches: If the Parent Primary Key is a
BIGINT, the Child Foreign Key column MUST also be aBIGINT. If you make it a standardINT, PostgreSQL will throw a foreign key constraint error.
10. Best Practices
-
Naming Conventions: Always name your Foreign Key columns as the singular parent table name followed by
id(e.g.,userid,productid).
- Index your Foreign Keys: PostgreSQL does NOT automatically index Foreign Keys. To make JOINs fast, you should always manually create an index on every foreign key column (We will cover Indexing in Chapter 18).
11. Exercises
- 1. In a blog application, One User writes Many Posts. Which table contains the Foreign Key?
-
2.
What does
ON DELETE CASCADEdo?
12. SQL Challenges
Write the DDL to create areviews table that has a Foreign Key pointing to a products table (productid). Ensure that if a product is deleted, all its reviews are automatically deleted.
13. MCQ Quiz with Answers
In database architecture, what is a "Pivot" (or Junction) table used for?
By default (without CASCADE), what will PostgreSQL do if you attempt to DELETE a user who currently has 5 active orders pointing to them via a Foreign Key?
14. Interview Questions
- Q: Explain the concept of Referential Integrity. How do Foreign Keys enforce it?
-
Q: Describe a business scenario where using
ON DELETE CASCADEwould be extremely dangerous and ill-advised.
15. FAQs
Q: Can a Foreign Key be NULL? A: Yes! If a column is a Foreign Key, it can beNULL (meaning the row currently has no parent). However, if there IS data in it, that data MUST match a valid Primary Key in the parent table.
16. Summary
Foreign Keys are the mortar that holds the bricks of your database together. By establishing strict parent-child relationships and utilizing pivot tables, you can model infinitely complex real-world systems while PostgreSQL automatically guards against data corruption.17. Next Chapter Recommendation
Our tables are beautifully separated and linked. But when a user loads their dashboard, they don't want a randomuser_id number; they want the actual names and data from across all tables stitched back together! In Chapter 14: INNER JOIN, LEFT JOIN, RIGHT JOIN, and FULL JOIN, we will master the art of querying across relationships.