Gomaa Intelligence.Data Hub
عربي

Foundations › SQL › Foundations: Databases

️ Foundations: Databases

Table Relationships: One-to-One, One-to-Many, Many-to-Many

Lesson 3 / 14· 6 min

A relationship describes how many rows in one table can link to rows in another.

TypeMeaningExample
One-to-oneA row matches exactly one rowemployee ↔ access badge
One-to-manyA row matches many rowsone customer, many orders
Many-to-manyMany rows match many rowsan 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.

▶ Try it yourself

Each row here: one product inside one order. Open in SQL Lab

customersid PKnamecityemailordersid PKcustomer_id FKorder_datestatusproductsid PKnamecategorypriceorder_itemsid PKorder_id FKproduct_id FKquantity1N1N1N
Relationship diagram of the sample shop database

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…

❓ What is order_items for?

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

Get your data analyzed by an expert ↗