Lesson 6 / 28
Primary & Foreign Keys
How tables link to each other and stay consistent.
Primary key
A primary key uniquely identifies each row in a table — no duplicates, no NULLs.
Foreign key
A foreign key in one table points to a primary key in another, enforcing the relationship.
CREATE TABLE orders (
id INT AUTO_INCREMENT PRIMARY KEY,
customer_id INT NOT NULL,
order_date DATE,
FOREIGN KEY (customer_id) REFERENCES customers(id)
);Like a library ticket
A foreign key is like a library slip that references a book's catalog number instead of copying the whole book's details onto every slip.