01 / 21

Identifying Keys
in SQL

Learn how primary keys and foreign keys organize relational databases, enforce data integrity, and connect tables together.

SQL

What Is a Key?

In a relational database, a key is a column (or set of columns) that serves a special purpose: it either uniquely identifies each row in a table or links one table to another.

★ Uniqueness
A key value must be unique so every row can be told apart.
★ Relationships
Keys connect rows across different tables to model real-world links.

Why Keys Matter

1
Data integrity: Keys prevent duplicate or orphaned records from entering the database.
2
Fast lookups: The database engine uses keys to build indexes, making queries run much faster.
3
Table relationships: Keys let you split data across multiple tables while keeping everything connected.

The Two Main Key Types

Primary Key (PK)
A column (or set of columns) that uniquely identifies every row in its own table. Each table has exactly one primary key.
Foreign Key (FK)
A column that references the primary key of another table, creating a link between the two tables.

Primary Key Rules

1
Unique: No two rows can share the same primary key value.
2
NOT NULL: A primary key column can never be empty. Every row must have a value.
3
One per table: Each table may have only one primary key (though it can span multiple columns).
4
Immutable (best practice): Primary key values should rarely, if ever, change after a row is created.

Foreign Key Rules

1
References a PK: A foreign key must point to an existing primary key (or unique key) in another table.
2
Can be NULL: Unlike a PK, a foreign key column is allowed to be empty (unless explicitly restricted).
3
Can repeat: Many rows can share the same foreign key value (e.g., many orders reference the same customer).
4
Enforces referential integrity: The database rejects any FK value that doesn't match a valid PK in the referenced table.

How Tables Connect

Students Column Key student_id PK name email grade Enrollments Column Key enroll_id PK student_id FK course_name semester references Primary Key Foreign Key

Primary vs Foreign Key

Property Primary Key Foreign Key
Purpose Uniquely identifies a row References a row in another table
Allows NULL? No Yes (unless restricted)
Allows duplicates? No Yes
How many per table? Exactly one Zero or many
Auto-indexed? Yes (automatic) Varies by DBMS

How to Identify Keys

1
List every column. Look at the table schema and note each column name and data type.
2
Find the unique identifier. Ask: "Which column (or columns) can never repeat across rows?" That is the primary key candidate.
3
Check for NULLs. A true primary key must be NOT NULL. If a unique column allows NULLs, it may be a candidate key but not the PK.
4
Spot references. Does a column name match a primary key in another table (e.g., customer_id in an Orders table)? That is likely a foreign key.
5
Verify the relationship. Confirm that every value in the FK column exists in the referenced PK column.

Declaring Keys in SQL

CREATE TABLE Students ( student_id INT PRIMARY KEY, name VARCHAR(100) NOT NULL, email VARCHAR(255) ); CREATE TABLE Enrollments ( enroll_id INT PRIMARY KEY, student_id INT, course_name VARCHAR(100), FOREIGN KEY (student_id) REFERENCES Students(student_id) );

Example 1: Library Database

Books book_id (PK) title author genre Loans loan_id (PK) book_id (FK) borrower_name loan_date return_date One book can appear in many loans (one-to-many)

Example 2: E-Commerce Schema

Customers customer_id (PK) name email Orders order_id (PK) customer_id (FK) order_date total_amount Products product_id (PK) product_name price Each table has its own PK. Orders uses a FK to reference Customers.

Example 3: Composite Primary Key

Order_Items Column Key order_id PK, FK product_id PK, FK quantity } Composite PK The pair (order_id, product_id) is unique together

Candidate Keys & Unique Keys

Before choosing a primary key, you may find multiple columns that could uniquely identify rows. Each of these is a candidate key.

Candidate Key
Any column (or set) that is unique and NOT NULL. You pick one to be the PK; the rest remain candidate keys.
Unique Key
A constraint you can add to non-PK columns to enforce uniqueness (e.g., email). Unlike a PK, it allows NULL.

Common Mistakes

✕ Using names as PKs
Names are not unique ("John Smith" can appear twice) and they change. Use a numeric ID instead.
✕ Confusing PK with FK
The same column name (e.g., student_id) can be a PK in one table and a FK in another. Context matters.
✕ Forgetting NOT NULL
A primary key must never be NULL. If your PK column allows NULLs, the constraint is incomplete.

Key Types Summary

Key Type Unique? NOT NULL? Per Table
Primary Key Yes Yes Exactly 1
Foreign Key No No (optional) 0 or many
Candidate Key Yes Yes 1 or more
Unique Key Yes No (allows NULL) 0 or many
Composite Primary Key Yes (combined) Yes 0 or 1

Question 1 of 5

Which property is required of a primary key but not of a foreign key?

Question 2 of 5

In the schema above, what role does customer_id play in the Orders table?

Question 3 of 5

A table called Enrollment uses the combination of student_id and course_id as its primary key. What type of key is this?

Question 4 of 5

A student tries to insert a row into the Enrollments table with a student_id value of 999, but no student with that ID exists in the Students table. What happens?

Question 5 of 5

Which of the following is the worst choice for a primary key?