Foundations › SQL › Foundations: Databases
️ Foundations: DatabasesTable Relationships: One-to-One, One-to-Many, Many-to-Many
A relationship describes how many rows in one table can link to rows in another.
| Type | Meaning | Example |
|---|---|---|
| One-to-one | A row matches exactly one row | employee ↔ access badge |
| One-to-many | A row matches many rows | one customer, many orders |
| Many-to-many | Many rows match many rows | an order has many products; a product is in many orders |
One-to-many is the most common, and the foreign key sits on the many side (orders.customer_id).
Many-to-many and the junction table
A many-to-many relationship is not stored directly; we add a junction table with two foreign keys. Our order_items links orders to products and carries quantity and price.
Each row here: one product inside one order. Open in SQL Lab
Normalization in short
Organizing tables to reduce repetition: each fact in one place and every column describing the key. The goal is more accurate data and easier edits.
For advanced readers Normalization in analytics: the star schema
Operational systems lean towards 3NF. Analytics warehouses often use a star schema: a central fact table surrounded by dimension tables, which speeds up aggregation and simplifies queries. Covered in the advanced stage.
❓ The relationship between customers and orders is…
One customer can place many orders.
❓ What is order_items for?
It is the junction table for the many-to-many relation between orders and products.
- One-to-many is the most common; the foreign key is on the many side.
- Many-to-many needs a junction table.
- Normalization reduces repetition and eases edits.
Want an expert to do it for you?
Gomaa Intelligence turns your data into clear reports and decisions. Tell us your problem and we will reply.