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.
A one-to-many relationship means one record in one table can be associated with many records in another table.
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.
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.
A one-to-many relationship keeps related data connected without repeating the parent’s full details in every child row.
Use a foreign key to connect each child to its parent.
Store customers, orders, or other entities in separate tables.
Find all child records that belong to one parent.
One-to-many relationships appear wherever one entity can own, create, or contain multiple related entities.
One customer can place many orders.
One author can publish many posts.
One team can include many members.
One department can contain many employees.
One invoice can list many purchased items.
One project can contain many tasks.
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.
A primary key identifies the parent; matching foreign-key values gather the related child records.
One parent record can relate to many child records.
Many records on both sides relate to each other, usually through a join table.
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.
| 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. | 把有关联的行查到一起。 |
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.