← Database & SQL
MODULE 05 · DATABASE & SQL
Primary Key
A column, or group of columns, that uniquely identifies each row in a table.
01 · What is it?
A primary key is a column, or group of columns, that uniquely identifies each row in a table.
Every row has one primary-key value. That value lets software refer to one exact record without guessing which row it means.
Simple definition: A primary key is the table's unique label for each row.
02 · Think of it like…
Think of a primary key like a passport number or library card number.
Many people can share a name, but each person can have a different official number. A table uses its primary key in the same way: it points to one specific row.
TableUsersMany rows
→
RowRebeccaOne record
→
Primary keyuser_id = 42Unique value
→
ResultOne exact userNo ambiguity
03 · What does it do?
A primary key gives the database and application a stable way to:
Identify a rowFind one exact record in a table.
Prevent duplicatesStop two rows from using the same key.
Support relationshipsGive other tables a value they can reference.
Make updates safeChange or delete the intended row.
04 · Where is it used?
Primary keys are used in relational tables whenever software needs to keep records distinct and addressable.
Usersuser_id identifies one account.
Ordersorder_id identifies one purchase.
Productsproduct_id identifies one catalog item.
Paymentspayment_id identifies one payment record.
05 · Example
This table uses user_id as its primary key. The value 42 identifies exactly one row.
CREATE TABLE users (
user_id INTEGER PRIMARY KEY,
email TEXT NOT NULL,
display_name TEXT NOT NULL
);
SELECT * FROM users WHERE user_id = 42;
| user_id | email | display_name |
| 41 | alex@example.com | Alex |
| 42 | rebecca@example.com | Rebecca |
| 43 | sam@example.com | Sam |
The query can safely target Rebecca's row because user_id = 42 is unique.
06 · How it connects
ApplicationEdit user
→
SQLWHERE user_id = 42
→
Primary keyMatch 42
→
RowOne user record
→
Related dataOrders, profile
A foreign key in another table can store the primary-key value to connect related records.
07 · Do Not Confuse
Primary Key
Uniquely identifies each row in its own table.
Foreign Key
Stores a key from another table to represent a relationship.
Unique Constraint
Prevents duplicate values in a column, but it is not automatically the table's primary identity.
08 · Topic Glossary
| ENGLISH TERM | 中文 | SIMPLE ENGLISH | 中文小白理解 |
| Primary Key | 主键 | A column or group of columns that uniquely identifies each row. | 唯一标识数据表中每一行的一个字段或一组字段。 |
| Table | 表 | A structure that stores related data in rows and columns. | 用行和列保存一类相关数据的结构。 |
| Row | 行 | One complete record in a table. | 数据表中的一条完整记录。 |
| Column | 列 | One named type of information in a table. | 数据表中表示某一类信息的字段。 |
| Unique | 唯一 | Different from every other value in the relevant set. | 和同一范围内的其他值都不相同。 |
| Constraint | 约束 | A rule that limits which data the database accepts. | 限制数据库可以接受什么数据的规则。 |
| Foreign Key | 外键 | A column that refers to a key in another table. | 引用另一张表中键值、用来建立关联的字段。 |
| Composite Key | 复合键 | A key made from two or more columns together. | 由两个或多个字段共同组成的键。 |
| NULL | 空值 | A database value meaning that information is missing or unknown. | 表示信息缺失或未知,而不是普通的空字符串。 |
09 · What to Remember
A primary key uniquely identifies each row in a table.
It helps software find, update, delete, and connect one exact record.
Other tables can refer to it with a foreign key.