# Primary & Foreign Keys — MySQL

Source: https://www.geekswithgeeks.com/en/mysql/sql-keys-relationships

> 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.

```sql
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.
