AiTechWorlds
AiTechWorlds
Walk into a university library. Every book on every shelf has a unique catalog number printed on its spine — something like QA76.9.D3 C67 2021. That number isn't just a label. It's used to locate the book, to reference it in the checkout system, to link it back to its author record, and to track every time it was ever borrowed.
Remove that number and replace it with just the title, and chaos follows. Two books named Introduction to Computing become indistinguishable. A checkout record that says "borrowed Introduction to Computing" no longer points to a specific book. The whole system unravels.
That catalog number is a primary key. This lesson explains why keys are the foundation of every relational database.
Edgar Codd's relational model uses precise mathematical language. Modern databases translate it to practical terms.
| Formal Term | Practical Term | What It Means |
|---|---|---|
| Relation | Table | A set of rows with the same structure |
| Tuple | Row / Record | One instance of the entity |
| Attribute | Column / Field | One property of the entity |
| Domain | Data Type | The set of allowed values for an attribute |
| Degree | — | Number of columns in a table |
| Cardinality | — | Number of rows in a table |
A table in the relational model is not an ordered list — it's a mathematical set. Sets have no duplicates and no inherent order. SQL tables add ordering and allow duplicates by default, but the relational model forbids both. That's why primary keys exist: to enforce the set property.
A candidate key is any column (or combination of columns) that could uniquely identify every row in a table. A table can have multiple candidate keys.
Example: In a Students table, both StudentID and Email could uniquely identify each student. Both are candidate keys.
The primary key (PK) is the candidate key the designer chooses as the official row identifier. Rules:
A natural key is a real-world value that happens to be unique (Social Security Number, ISBN, email address). A surrogate key is an artificial ID created purely for the database (auto-incremented integer, UUID).
Surrogate keys are usually preferred because natural keys change (people change their email) while surrogate keys never need to.
A foreign key (FK) is a column in one table that references the primary key of another table. It creates the link between tables.
students.dept_id → departments.dept_id
The table holding the FK is the child table. The table being referenced is the parent table.
A composite key is a primary key made of two or more columns. Common in junction tables.
Example: In an Enrollments table, neither student_id alone nor course_id alone is unique — a student enrolls in many courses. But the combination (student_id, course_id) is unique.
Any candidate key not chosen as the primary key becomes an alternate key. These are typically enforced with a UNIQUE constraint.
| Key Type | Definition | Example | SQL Syntax |
|---|---|---|---|
| Primary Key | Unique, non-null row identifier | student_id INT | PRIMARY KEY |
| Foreign Key | References PK of another table | dept_id INT | FOREIGN KEY ... REFERENCES |
| Composite Key | PK made of 2+ columns | (student_id, course_id) | PRIMARY KEY (col1, col2) |
| Candidate Key | Could be PK; unique + not null | email VARCHAR | UNIQUE NOT NULL |
| Alternate Key | Candidate key not chosen as PK | email (when student_id is PK) | UNIQUE |
| Surrogate Key | Artificial, auto-generated ID | id INT AUTO_INCREMENT | AUTO_INCREMENT or SERIAL |
Constraints are database-level rules that reject invalid data before it can be stored. They run every time data is inserted or updated.
The column must have a value. Used for required fields.
first_name VARCHAR(50) NOT NULL
All values in the column must be different. NULLs are typically allowed and not compared to each other.
email VARCHAR(100) UNIQUE
Combines NOT NULL + UNIQUE. Only one per table.
student_id INT PRIMARY KEY
Value must match an existing value in the referenced column, or be NULL.
FOREIGN KEY (dept_id) REFERENCES departments(dept_id)
Value must satisfy a boolean expression.
gpa DECIMAL(3,2) CHECK (gpa >= 0.0 AND gpa <= 4.0)
If no value is provided on INSERT, use this value.
enrollment_date DATE DEFAULT CURRENT_DATE
When a parent row is updated or deleted, what happens to child rows that reference it? The database needs a rule.
Scenario: Student with dept_id = 5 is enrolled. Department 5 is deleted.
| Action | Effect |
|---|---|
RESTRICT | Block the delete. You cannot delete the parent while children exist. (Default behavior) |
CASCADE | Automatically delete (or update) all child rows when the parent is deleted/updated |
SET NULL | Set the FK column in child rows to NULL |
SET DEFAULT | Set the FK column to its default value |
NO ACTION | Similar to RESTRICT; checked at end of transaction |
Choosing the right action:
CASCADE when children cannot exist without the parent (OrderItems when an Order is deleted)SET NULL when the child can exist independently (a Book's author_id when an Author is deleted)RESTRICT when you want to force manual cleanup before deletionCREATE TABLE departments (
dept_id INT PRIMARY KEY AUTO_INCREMENT,
dept_name VARCHAR(100) NOT NULL UNIQUE,
building VARCHAR(50)
);
CREATE TABLE students (
student_id INT PRIMARY KEY AUTO_INCREMENT,
first_name VARCHAR(50) NOT NULL,
last_name VARCHAR(50) NOT NULL,
email VARCHAR(100) NOT NULL UNIQUE,
gpa DECIMAL(3,2) DEFAULT 0.00
CHECK (gpa >= 0.00 AND gpa <= 4.00),
dept_id INT,
enrollment_date DATE DEFAULT (CURRENT_DATE),
FOREIGN KEY (dept_id)
REFERENCES departments(dept_id)
ON DELETE SET NULL
ON UPDATE CASCADE
);
CREATE TABLE courses (
course_id INT PRIMARY KEY AUTO_INCREMENT,
course_code VARCHAR(10) NOT NULL UNIQUE,
course_name VARCHAR(200) NOT NULL,
credits INT NOT NULL CHECK (credits BETWEEN 1 AND 6),
dept_id INT NOT NULL,
FOREIGN KEY (dept_id)
REFERENCES departments(dept_id)
ON DELETE RESTRICT
);
CREATE TABLE enrollments (
student_id INT NOT NULL,
course_id INT NOT NULL,
enrollment_date DATE DEFAULT (CURRENT_DATE),
grade CHAR(2),
PRIMARY KEY (student_id, course_id), -- composite primary key
FOREIGN KEY (student_id)
REFERENCES students(student_id)
ON DELETE CASCADE,
FOREIGN KEY (course_id)
REFERENCES courses(course_id)
ON DELETE RESTRICT
);
Notice how every design decision maps to a concept from this lesson:
AUTO_INCREMENT surrogate keys on students, departments, coursesNOT NULL on all truly required fieldsUNIQUE on email and course_code (alternate keys)CHECK on gpa and credits to reject bad valuesDEFAULT for dates that default to todayCASCADE on enrollments: if a student is deleted, their enrollments disappear tooKeys and constraints are not administrative overhead. They are the database doing your validation for you — consistently, every time, from every application that touches the data. No matter which programming language inserts a row, the database enforces the same rules.
Every layer built on top of this — queries, joins, indexes, transactions — depends on the integrity that keys and constraints provide.
Next lesson: Writing the DDL commands that create, modify, and drop these structures in SQL.
Get this course's notes on Telegram!
Free cheat sheets, summaries & practice exercises