Data integrity is a crucial aspect of database management, ensuring the accuracy, consistency, and reliability of the data stored in relational databases. SQL constraints are the rules that you define to enforce data integrity and ensure that the data follows predefined business rules. Constraints in SQL are used to limit the type of data that can be inserted into a table.
In this article, we'll cover three essential types of constraints: PRIMARY KEY, FOREIGN KEY, and CHECK, and provide examples of how they are used to maintain data integrity.
1. PRIMARY KEY Constraint
What is it?
A PRIMARY KEY constraint ensures that each record in a table is unique and identifies a record uniquely. It also enforces NOT NULL on the column(s) involved, ensuring that the key column(s) cannot contain NULL values.
When to use it?
Use the PRIMARY KEY constraint to identify a unique record in the table.
A table can have only one primary key, but the primary key can consist of one or multiple columns (composite key).
Example:
Consider a table called students where we store the student ID and name.
CREATE TABLE students (
student_id INT PRIMARY KEY,
student_name VARCHAR(100) NOT NULL
);
In this case:
The
student_idis the primary key, meaning each student must have a unique ID.The
student_namecannot be NULL because it's specified asNOT NULL.
Inserting Data:
INSERT INTO students (student_id, student_name)
VALUES (1, 'John Doe'), (2, 'Jane Smith');
Result:
| student_id | student_name |
|---|---|
| 1 | John Doe |
| 2 | Jane Smith |
If we try to insert a record with a duplicate student_id:
INSERT INTO students (student_id, student_name)
VALUES (1, 'Jack Brown');
Error:
ERROR: Duplicate entry '1' for key 'PRIMARY'
The PRIMARY KEY constraint ensures that the student_id is unique.
2. FOREIGN KEY Constraint
What is it?
A FOREIGN KEY constraint is used to link two tables together. It ensures that the value in one table matches a value in another table, maintaining referential integrity between the two.
When to use it?
Use the FOREIGN KEY constraint when one table depends on another (e.g., a
childtable with references to theparenttable).It ensures that a value in the foreign key column matches a value in the referenced column of the other table, and prevents inserting a value in the foreign key column that does not exist in the referenced table.
Example:
Let's create a courses table where students enroll in courses. We'll use a FOREIGN KEY to link the students table to the courses table.
CREATE TABLE courses (
course_id INT PRIMARY KEY,
course_name VARCHAR(100) NOT NULL
);
CREATE TABLE enrollments (
enrollment_id INT PRIMARY KEY,
student_id INT,
course_id INT,
FOREIGN KEY (student_id) REFERENCES students(student_id),
FOREIGN KEY (course_id) REFERENCES courses(course_id)
);
Here:
enrollments.student_idreferences thestudents.student_id(the primary key in thestudentstable).enrollments.course_idreferences thecourses.course_id(the primary key in thecoursestable).
Inserting Data:
INSERT INTO courses (course_id, course_name)
VALUES (101, 'Math 101'), (102, 'History 101');
INSERT INTO enrollments (enrollment_id, student_id, course_id)
VALUES (1, 1, 101), (2, 2, 102);
Result:
students table:
| student_id | student_name |
|---|---|
| 1 | John Doe |
| 2 | Jane Smith |
courses table:

Join the conversation! Your thoughts help the community grow.