1. PRIMARY KEY in SQL
A PRIMARY KEY uniquely identifies each row in a table.
Rules:
- must be unique
- cannot be NULL
CREATE TABLE customers (
customer_id INT PRIMARY KEY,
customer_name VARCHAR(100)
);| customer_id | customer_name |
|---|---|
| 101 | Rahul |
| 102 | Priya |
| 103 | Arjun |
| customer_id | customer_name |
|---|---|
| 101 | Rahul |
| 101 | Priya |
Why Analysts Care
Tables are joined using primary keys. Without reliable IDs:
- joins break
- duplicates happen
- revenue gets overstated
SELECT *
FROM customers c
INNER JOIN orders o
ON c.customer_id = o.customer_id;This only works cleanly if customer IDs are trustworthy.