Key
This page first looks at the primary key (PK), foreign key (FK), and how they differ from the UNIQUE constraint using project examples, and then covers how identifiers are classified in theory.
Primary Key (PK)
A value that uniquely identifies each row within a table. It cannot be NULL and cannot be duplicated. In this project, all 4 tables use INTEGER PRIMARY KEY AUTOINCREMENT, so every time a row is added, SQLite automatically fills in an ID that increases by 1.
Foreign Key (FK)
A column that connects two tables by storing another table's PK value as is. When an FK is set, the DB engine enforces a check of "does this value actually exist in the parent table?" If you try to insert a value that doesn't exist, it is blocked with a FOREIGN KEY constraint failed error, which is exactly what was checked directly in the integrity verification part of 03_queries.sql.
PK vs FK
| Category | PK (Primary Key) | FK (Foreign Key) |
|---|---|---|
| Role | Uniquely identifies each row in its own table | Points to another table's PK |
| Duplicates | Not allowed | Allowed (several child rows can point to the same parent) |
| NULL allowed | No | Depends on the column design (all NOT NULL in this project) |
| Count per table | Usually 1 | Can be several (likes and comments have 2 FKs each) |
UNIQUE vs PRIMARY KEY
Both "prevent duplicates," but a PK is effectively one per table (the table's identity) and does not allow NULL, while UNIQUE can be set on several columns in one table. In this project, UNIQUE was set on users.username and users.email separately to express "values that are not used as identifiers but still cannot be duplicated," and a composite UNIQUE on the two columns of likes(user_id, reel_id) was used to express "this combination must exist only once."
Types of Identifiers: Primary, Alternate, and Surrogate
An identifier is an attribute (or set of attributes) that uniquely distinguishes one row (instance) of a table. A good identifier must satisfy these 4 properties.
- Uniqueness: no duplicates
- Minimality: made up of only the attributes that are truly needed (don't bundle more than necessary)
- Immutability: once set, the value does not change
- Existence: cannot be NULL (empty)
A blank string is not the same as NULL
Existence means "it must not be NULL." Whether to allow a blank string (' ') is decided separately by business rules, independent of existence.
These identifiers can be divided by a few criteria.
Was It Chosen as the Representative?
- Primary identifier (PK): exactly one per table, the attribute that represents and identifies a row. Cannot be NULL.
- Alternate identifier: an attribute that is unique but wasn't chosen as the representative. Example: when the employee number is the PK, the resident registration number is an alternate identifier.
Did It Come Naturally from the Business, or Was It Made Artificially?
- Natural identifier: an attribute as it naturally arises in the business process (e.g. order date + product code)
- Surrogate key: an identifier created artificially for easier management when the natural identifier is too complex or tangled across several attributes. An auto-increment number or UUID is commonly used (e.g. a newly created "order number" instead of order date + product code).
One Attribute, or Several Combined?
- Simple identifier: uniqueness from 1 attribute
- Composite identifier: uniqueness only when several attributes are combined (e.g. student ID + course code)
Created Itself, or Inherited from Another Table?
- Internal identifier: generated within its own table
- External identifier: an identifier inherited through a relationship with another table, i.e. an FK
Comparison at a Glance
| Criterion | What to look at | Identifier types |
|---|---|---|
| Representativeness | Was it chosen as the PK? | Primary vs alternate identifier |
| How it was created | Did it arise naturally in the business, or was a number attached artificially? | Natural vs surrogate identifier |
| Number of attributes | Is it 1 column, or 2 or more? | Simple vs composite identifier |
| Inheritance | Did it create itself, or inherit from a parent (FK)? | Internal vs external identifier |
Alternate identifiers can also be made by combining several attributes. In a student table where the student ID is the primary identifier, combining "grade + class + number" can find one student, and this is called a composite alternate identifier.
Practice Problem: Classifying Identifiers
An identifier classification problem I solved myself (image)

Summary
| Concept | Key point |
|---|---|
| PK | Uniquely identifies a row, no NULL or duplicates, usually 1 per table (e.g. INTEGER PRIMARY KEY AUTOINCREMENT) |
| FK | Stores another table's PK to connect them, duplicates allowed, several per table possible, missing values cause FOREIGN KEY constraint failed |
| UNIQUE | Only blocks duplicates, several per table possible, composite UNIQUE prevents duplicate combinations |
| Good identifier | Uniqueness, minimality, immutability, existence |
| Identifier classification | Primary/alternate, natural/surrogate (Surrogate Key), simple/composite, internal/external (FK) |
Source: Codyssey-B1/B6-1
Reference: personal Notion study notes
