MODULE 05 · DATABASE & SQL

One-to-Many

A one-to-many relationship means one record in one table can be associated with many records in another table.

01 · What is it?

One parent record can connect to many child records, while each child record points back to one parent.

For example, one customer can place many orders. The relationship is stored with a foreign key in the table on the “many” side.

02 · Think of it like...

Think of one customer as a folder that can hold many order receipts.

The customer is the single parent. Each order is a separate child record, but every order keeps the customer’s ID so the system knows which folder it belongs to.

Customer 42
Order 1001
Order 1002
Order 1003

03 · What does it do?

A one-to-many relationship keeps related data connected without repeating the parent’s full details in every child row.

Connect Records

Use a foreign key to connect each child to its parent.

Keep Data Organized

Store customers, orders, or other entities in separate tables.

Query Related Data

Find all child records that belong to one parent.

04 · Where is it used?

One-to-many relationships appear wherever one entity can own, create, or contain multiple related entities.

Customers → Orders

One customer can place many orders.

Authors → Posts

One author can publish many posts.

Teams → Members

One team can include many members.

Departments → Employees

One department can contain many employees.

Invoices → Line Items

One invoice can list many purchased items.

Projects → Tasks

One project can contain many tasks.

05 · Example

CREATE TABLE orders (
  id INTEGER PRIMARY KEY,
  customer_id INTEGER NOT NULL,
  total DECIMAL(10, 2),
  FOREIGN KEY (customer_id)
    REFERENCES customers(id)
);

SELECT *
FROM orders
WHERE customer_id = 42;

The orders.customer_id foreign key points to customers.id. Customer 42 can therefore have many rows in orders, and the query returns every order for that customer.

The foreign key lives on the many side: every order stores the ID of its one customer.

06 · How it connects

customers.id
orders.customer_id
Many order rows
Customer order history

A primary key identifies the parent; matching foreign-key values gather the related child records.

07 · Do Not Confuse

One-to-Many

One parent record can relate to many child records.

Many-to-Many

Many records on both sides relate to each other, usually through a join table.

One-to-One

One record matches at most one record in the related table.

One-to-many describes the relationship’s cardinality; it does not mean one table is physically nested inside another.

08 · Topic Glossary

ENGLISH TERM中文SIMPLE ENGLISH中文小白理解
One-to-Many Relationship一对多关系One record is related to many records.一条记录可以对应很多条记录。
Parent Record父记录The single record at the one side.关系中“一”的那条记录。
Child Record子记录A record connected to a parent.连接到父记录的相关记录。
Primary Key主键A value that uniquely identifies a row.能唯一认出一行数据的值。
Foreign Key外键A value that points to a key in another table.指向另一张表中某个键的值。
Cardinality基数The number pattern between related records.描述两边记录数量关系的规则。
Join连接查询A query operation that combines related rows.把有关联的行查到一起。

09 · What to Remember

In a one-to-many relationship, one parent record connects to many child records. Put the parent’s primary key into the child table as a foreign key.

↑