Primary and Foreign Key Relationships
You'll learn how primary and foreign keys create the foundation for INNER JOINs to connect related tables.
What Are Primary and Foreign Keys?
A primary key is a unique identifier for each row in a table—like an employee ID number that no two employees share. Every table typically has one primary key column that guarantees you can pinpoint any specific record.
A foreign key is a column in one table that references the primary key of another table. Think of it as a "pointer" or "link" between tables. For example, an orders table might have a customer_id foreign key that points to the customer_id primary key in a customers table.
How They Enable JOINs
When you write an INNER JOIN, you're almost always connecting these key relationships:
SELECT customers.name, orders.order_date
FROM customers
INNER JOIN orders ON customers.customer_id = orders.customer_id
Here, customers.customer_id is the primary key (unique per customer), and orders.customer_id is the foreign key (multiple orders can belong to one customer). The ON clause matches these keys to combine related data from both tables.
Why This Matters
This key relationship ensures data integrity—you can't have an order pointing to a non-existent customer. It also makes JOINs efficient and meaningful: you're connecting records that genuinely belong together, not arbitrary matches.
Think of it like a library system: each book has a unique catalog number (primary key), and each checkout record stores that catalog number (foreign key) to track which book was borrowed.
Key Takeaway: Primary keys uniquely identify rows in a table, foreign keys reference those primary keys from other tables, and INNER JOINs use these relationships to meaningfully combine data across tables.