MODULE 05 · DATABASE & SQL
Foreign Key
A key that references a key in another table and helps enforce a relationship between tables.
01 · What is it?
A foreign key references a key in another table and helps enforce a relationship between tables.
It usually stores a value that points to a primary key in a related table.
Simple Definition: A foreign key is a link from one table to another.
02 · Think of it like…
Think of a foreign key as a label on an order that says which customer owns it.
The customer keeps the identity. The order keeps the reference.
Customers
id = 42
→
orders.customer_id
= 42
→
The order belongs to Customer 42
03 · What does it do?
A foreign key helps a database:
Connect TablesPoint a row in one table to a row in another.
Protect ReferencesPrevent links to records that do not exist.
Keep Data ConsistentMake related records agree about their connections.
Support JoinsGive SQL a reliable path for combining related data.
04 · Where is it used?
Foreign keys are used wherever one kind of record belongs to another.
Orders → CustomersEach order points to the customer who placed it.
Reviews → ProductsEach review points to the product being reviewed.
Comments → PostsEach comment points to the post it belongs to.
Employees → DepartmentsEach employee can point to a department.
05 · Example
An orders table can store the customer relationship directly in the row.
CREATE TABLE orders (
id INTEGER PRIMARY KEY,
customer_id INTEGER,
FOREIGN KEY (customer_id)
REFERENCES customers(id)
);
orders.customer_id is the foreign key.
customers.id is the referenced key.
The database can reject an order that points to a customer that does not exist.
06 · How it connects
Frontend→API→Backend→SQL→orders.customer_id→customers.id
The foreign key gives a query a safe, meaningful connection between child and parent rows.
07 · Do Not Confuse
Foreign Key
Points from a row in one table to a key in another table.
Primary Key
Uniquely identifies a row inside its own table.
A primary key identifies the record. A foreign key identifies the record it is related to.
08 · Topic Glossary
| ENGLISH TERM | 中文 | SIMPLE ENGLISH | 中文小白理解 |
| Foreign Key | 外键 | A key that references a key in another table. | 指向另一个数据表中某个键的字段。 |
| Primary Key | 主键 | A value that uniquely identifies a row in a table. | 在表中唯一标识一行数据的值。 |
| Parent Table | 父表 | The table whose key is referenced by another table. | 被另一个表引用的那张表。 |
| Child Table | 子表 | The table that stores the foreign key. | 保存外键、用来指向另一张表的表。 |
| Reference | 引用 | A link that points to a related key or record. | 指向相关键或记录的连接。 |
| Constraint | 约束 | A rule the database checks for stored data. | 数据库保存数据时必须遵守的规则。 |
| Relationship | 关系 | A connection between records in different tables. | 不同数据表中的记录之间建立的联系。 |
| One-to-Many | 一对多 | One parent record is related to many child records. | 一条父记录可以对应多条子记录。 |
| NULL | 空值 | A value that means no value is present. | 表示当前没有填写或没有对应值。 |
| Referential Integrity | 引用完整性 | The rule that table references remain valid. | 保证表之间的引用不会指向不存在记录的规则。 |
09 · What to Remember
A foreign key connects a row to a key in another table.
It helps the database protect relationships and keep related data consistent.
In a common one-to-many relationship, the child table stores the foreign key.
Parent Primary Key→Child Foreign Key