01 / 22
CONSTRAINTS

Table
Constraints in SQL

Learn how PRIMARY KEY, FOREIGN KEY, and UNIQUE constraints enforce data integrity and keep your databases reliable.

SQL

What Are Constraints?

Constraints are rules you define on a table's columns that the database enforces automatically. They prevent invalid data from ever entering your tables, acting like guardrails for your data.

Why Constraints Matter

✓ Data Integrity
Every row obeys the rules you set: no duplicates where they shouldn't be, no missing links.
✓ Automatic Enforcement
The database checks every INSERT and UPDATE for you: no extra application code needed.
✓ Clear Documentation
Constraints make the table schema self-describing: anyone reading the DDL sees the rules.

The Big Three Constraints

PRIMARY KEY
Uniquely identifies each row. Combines UNIQUE + NOT NULL. One per table.
FOREIGN KEY
Links a column to a PRIMARY KEY in another table. Ensures referential integrity.
UNIQUE
Prevents duplicate values in a column. Unlike PRIMARY KEY, allows NULL and you can have many per table.

PRIMARY KEY: The Row ID

students student_id (PK) name email 1 Alice alice@school.edu 2 Bob bob@school.edu 3 Clara clara@school.edu 2 Dave dave@school.edu ✕ REJECTED: Duplicate primary key "2"

PRIMARY KEY Syntax

CREATE TABLE students ( student_id INT PRIMARY KEY, name VARCHAR(100) NOT NULL, email VARCHAR(200) );

FOREIGN KEY: The Link

students student_id (PK) name 1 Alice 2 Bob 3 Clara enrollments id (PK) student_id (FK) 101 1 102 3 103 99 ✕ No student_id = 99 exists REFERENCES

FOREIGN KEY Syntax

CREATE TABLE enrollments ( id INT PRIMARY KEY, student_id INT, course VARCHAR(100), FOREIGN KEY (student_id) REFERENCES students(student_id) );

UNIQUE: No Duplicates

users id (PK) name email (UNIQUE) 1 Alice alice@mail.com 2 Bob bob@mail.com 3 Clara NULL 4 Dave alice@mail.com ✕ REJECTED: Duplicate email "alice@mail.com"

UNIQUE Syntax

CREATE TABLE users ( id INT PRIMARY KEY, name VARCHAR(100) NOT NULL, email VARCHAR(200) UNIQUE, username VARCHAR(50) UNIQUE );

PRIMARY KEY vs UNIQUE

PRIMARY KEY UNIQUE
Allows NULLs? No Yes
How many per table? Exactly 1 Many
Enforces uniqueness? Yes Yes
Creates an index? Yes Yes
Can be referenced by FK? Yes Yes

How to Apply Constraints

1
Identify the row identifier. Pick one column (or composite) that uniquely identifies each row: that's your PRIMARY KEY.
2
Find other uniqueness requirements. Columns like email, username, or SSN that must not repeat get the UNIQUE constraint.
3
Spot cross-table relationships. Any column that refers to another table's primary key should be a FOREIGN KEY.
4
Write CREATE TABLE. Add constraints inline or at the table level. Test with sample INSERT statements.

Example 1: Simple Table

A school needs a courses table. Each course has a unique ID, a name, and a department code. No two courses should share the same name.

CREATE TABLE courses ( course_id INT PRIMARY KEY, course_name VARCHAR(150) UNIQUE NOT NULL, department VARCHAR(50) NOT NULL );

Example 2: Linked Tables

Now add a grades table that links students to courses. Both student_id and course_id must reference existing records.

CREATE TABLE grades ( grade_id INT PRIMARY KEY, student_id INT NOT NULL, course_id INT NOT NULL, grade CHAR(2), FOREIGN KEY (student_id) REFERENCES students(student_id), FOREIGN KEY (course_id) REFERENCES courses(course_id) );

Example 3: Full Schema

students PK student_id name UQ email courses PK course_id UQ course_name department grades PK grade_id FK student_id FK course_id grade FK FK

Common Mistakes

✕ Multiple Primary Keys
A table can have only one PRIMARY KEY. If you need uniqueness on more columns, use UNIQUE.
✕ Wrong Reference Order
The referenced (parent) table must be created before the table containing the FOREIGN KEY.
✕ Type Mismatch
A FOREIGN KEY column must have the same data type as the PRIMARY KEY it references.

Summary Table

Feature PRIMARY KEY FOREIGN KEY UNIQUE
Purpose Row identity Link tables No duplicates
NULLs? No Yes Yes
Per table 1 Many Many
Uniqueness Yes No Yes
References? Is referenced References PK Can be referenced

Question 1 of 5

Which constraint uniquely identifies each row and does not allow NULLs?

Question 2 of 5

How many PRIMARY KEY constraints can a single table have?

Question 3 of 5

What does a FOREIGN KEY enforce?

Question 4 of 5

Which of the following INSERTs would the database accept?

Question 5 of 5

A table needs columns for id, email, and phone. Both email and phone must be unique. Which setup is correct?