Microsoft SQL Server Tutorial Series: Beginner to Expert
Part 6: SQL Constraints – PK, FK, UNIQUE, NOT NULL
Welcome back! In this part of our SQL Server series, we’ll explore SQL Constraints, the rules that help ensure your database is accurate, consistent, and reliable.
๐ What You’ll Learn
- What SQL Constraints are and why they matter
- How to use PRIMARY KEY, FOREIGN KEY, UNIQUE, and NOT NULL
- How to add constraints while creating or altering tables
- Real-world examples using
Students
andCourses
tables
๐ What Are SQL Constraints?
Constraints are rules applied to table columns to control the type of data that can be stored. They help enforce data integrity and prevent bad or duplicate data from being inserted.
๐งฑ 1. PRIMARY KEY Constraint
The PRIMARY KEY uniquely identifies each row in a table. It must be:
- Unique (no duplicates)
- Not NULL
CREATE TABLE Students (
StudentID INT PRIMARY KEY,
FirstName VARCHAR(50),
LastName VARCHAR(50)
);
๐ก You can only have one primary key per table, but it can consist of one or more columns (composite key).
๐ 2. FOREIGN KEY Constraint
The FOREIGN KEY establishes a link between two tables. It ensures that the value in one table matches a value in another table.
CREATE TABLE Courses (
CourseID INT PRIMARY KEY,
CourseName VARCHAR(100)
);
CREATE TABLE Enrollments (
EnrollmentID INT PRIMARY KEY,
StudentID INT,
CourseID INT,
FOREIGN KEY (StudentID) REFERENCES Students(StudentID),
FOREIGN KEY (CourseID) REFERENCES Courses(CourseID)
);
๐ก If a referenced value doesn’t exist in the parent table, SQL Server will throw an error — this maintains referential integrity.
๐ 3. UNIQUE Constraint
The UNIQUE constraint ensures that all values in a column are different.
CREATE TABLE Teachers (
TeacherID INT PRIMARY KEY,
Email VARCHAR(100) UNIQUE,
Name VARCHAR(50)
);
๐ก Unlike PRIMARY KEY, a table can have multiple UNIQUE constraints, but each column must still allow only unique values.
๐ซ 4. NOT NULL Constraint
NOT NULL means a column must have a value — it can’t be left empty.
CREATE TABLE Departments (
DeptID INT PRIMARY KEY,
DeptName VARCHAR(50) NOT NULL
);
๐ก Use this for fields like names, dates, or emails that should never be blank.
๐งพ SQL Cheat Sheet
-- PRIMARY KEY CREATE TABLE Example ( ID INT PRIMARY KEY ); -- FOREIGN KEY FOREIGN KEY (ColumnName) REFERENCES OtherTable(OtherColumn) -- UNIQUE Email VARCHAR(100) UNIQUE -- NOT NULL Name VARCHAR(50) NOT NULL
๐ Real-World Example
In a school system:
StudentID
in Students is a PRIMARY KEYCourseID
in Enrollments is a FOREIGN KEYEmail
in Teachers must be UNIQUECourseName
in Courses should be NOT NULL
๐ Quick Comparison Table
Constraint | Purpose | Allows NULL? | Allows Duplicates? |
---|---|---|---|
PRIMARY KEY | Uniquely identifies each row | No | No |
FOREIGN KEY | Creates link between tables | Yes | Yes |
UNIQUE | Ensures all values are different | Yes | No |
NOT NULL | Requires a value in the column | No | Yes |
✅ Best Practices
- Always use PRIMARY KEY for every table.
- Use FOREIGN KEYS to define relationships.
- Combine UNIQUE + NOT NULL to enforce data quality.
- Name your constraints explicitly for clarity (e.g.,
CONSTRAINT FK_StudentID
).
๐ง Summary
Today, you learned how constraints help maintain the accuracy and integrity of your data:
- PRIMARY KEY – Uniquely identifies each record
- FOREIGN KEY – Creates table relationships
- UNIQUE – Ensures no duplicates
- NOT NULL – Requires non-empty values
Constraints are essential in professional database design — now you know how to use them confidently! ๐
๐ Previous & Next Posts
Questions or feedback? Leave a comment below and share your progress! ๐