SQL Integrity Constraints Explained
SQL Integrity Constraints Explained
EN
T U T O R I AL S category
François Aubry
Passionate teacher crafting engaging online courses for all learners.
T O PI C S
SQL
Data Science
Imagine a hospital database where patient allergies can't be left blank or a financial
system where transaction amounts must be positive numbers. In these scenarios and
countless others, we rely on integrity constraints to ensure our data remains accurate,
consistent, and reliable.
In SQL, integrity constraints are rules that we impose on our database tables to maintain
data quality. They help prevent errors, enforce business rules, and ensure our data reflects
the real-world entities and relationships it represents.
In this article, we'll delve into the essential types of integrity constraints in SQL, providing
clear explanations and practical examples to illustrate their use in a PostgreSQL database.
While we'll use PostgreSQL syntax, the concepts easily translate to other SQL dialects.
If you want to learn more about SQL, check out this list of SQL courses.
Ensuring all data adheres to the expected types and value ranges.
In this article, we'll explore the following essential integrity constraints in SQL:
UNIQUE : Ensures that all values in a column or group of columns are unique.
Students table
The students table holds information on all university students.
Courses table
The courses table holds information on the courses available at the university.
Enrollments table
The enrollments table stores information on which students are enrolled in which courses.
Throughout this article, we’ll use this example database and show several ways to enforce
data integrity. We’ll use PostgreSQL syntax in our queries. However, the concepts easily
translate to other flavors of SQL.
The common solution for ensuring unique identification is to assign a unique identifier to
each student, which we store in the student_id column. We can apply a PRIMARY KEY
constraint to the student_id column to guarantee that each student has a unique
identifier.
This constraint is defined in the CREATE TABLE command, following the specification of
the column data type:
The above query creates the student table with the six columns we mentioned above.
Given that no single field can uniquely identify a row, each enrollment record is determined
using a combination of student_id , course_id , and year .
When multiple columns are involved, the PRIMARY KEY constraint is specified at the end of
the CREATE TABLE command.
Imagine the table already exists, but you forgot to specify the PRIMARY KEY constraint.
You can also define it after the table is created using the ALTER TABLE command, like so:
In the ALTER TABLE query, we named the constraint enroll_pk (standing for enrollment
primary key). This name can be any identifier of your choosing, but it’s recommended to
select a name that succinctly conveys the purpose of the constraint.
It allows for easier reference, especially when you need to modify or drop the
constraint in the future.
To overcome this, we can use NOT NULL constraints on these three columns when creating
the table:
The above query uses the NOT NULL constraints to ensure that the columns first_name ,
last_name , and email cannot have NULL (undefined) values.
Adding a NOT NULL constraint to an existing table can be done using the ALTER TABLE
command. The syntax for adding NOT NULL constraints is as follows:
UNIQUE Constraint
Email addresses are inherently unique to each individual. In situations where we’re sure that
a field should never have duplicate values, it’s good practice to enforce this at a database
level. This helps prevent mistakes and ensure data integrity.
However, in our case, we also want to enforce the NOT NULL constraint. We can enforce
multiple constraints on a single column by space separating them:
Note that the order doesn’t matter, we could also have used NOT NULL UNIQUE .
When multiple columns are involved, the constraint is added at the end of the CREATE
TABLE command:
Alternatively, we can add the constraint by altering an existing table. In this case, we
provide a tuple with the names of the columns:
The difference between NOT NULL UNIQUE and PRIMARY KEY on a table is their
intended purpose and usage.
While both enforce uniqueness and non-nullability on a column(s), a table can only have one
PRIMARY KEY , which is intended to uniquely identify each record in the table.
On the other hand, the combination NOT NULL UNIQUE constraint can be applied to
additional columns to enforce a unique value across each row, without null values, serving
to maintain data integrity for specific business rules. A table can have any number of NOT
NULL UNIQUE constraints.
The existence of both allows for greater flexibility in database design, enabling multiple
ways to enforce uniqueness and data integrity while distinguishing between the primary
identifier of a record and other important, unique attributes within a table.
DEFAULT Constraint
Students may need some time after university registration to select their major. The
university would like the value of the major column to be the string ’Undeclared’ for
students who haven’t selected their major yet.
To do so, we can set a default value of this column using the DEFAULT constraint. We can
alter the students table like so:
If, instead, we want to set a DEFAULT constraint when the table is created, we can do it
by declaring it after the column data type:
CHECK Constraint
In this particular university, grades go from 0 to 100. Without any constraint, the grade
column from the enrollment table accepts any integer value. We can fix that by using a
CHECK constraint to enforce the values to be between 0 and 100.
In general, CHECK constraints enable us to validate specific conditions we want the data
to satisfy. These are important to ensure data consistency and integrity.
A CHECK constraint can involve more than one column. Let’s use it to ensure that the
grade and is_passing_grade have consistent values. Say a grade is passing if its value is
at least 60. Then we can ensure that is_passing_grade is TRUE if and only if the grade is
at least 60. Let’s do it in the table creation to show how CHECK constraints are declared
in the CREATE TABLE command:
There is a problem with the above constraint. When a student enrolls in a course, they
won’t have a grade yet. So we should allow NULL values on the grade. When doing so, we
also need to update the passing grade constraint to be NULL when the grade is still
undefined. Here’s how to update the constraint to take these into account:
Note that on both the grade and is_passing_grade columns we added NULL on the data
type and as the default value. The purpose of these is simply to increase readability.
Let’s now see what conditions we can enforce with a CHECK constraint.
Range conditions
We can ensure that values in a column fall within a specific range.
List conditions
We can validate that a column's value matches one in a list of specific values.
Comparison conditions
We can compare values in a column to ensure they meet a specified condition (greater
than, less than, etc.).
Logical conditions
We can allow multiple conditions using logical operators ( AND , OR , NOT ).
Composite conditions
Applies a condition over multiple columns.
In our example, each record in the enrollments table refers to a student and a course
through the student_id and course_id columns, respectively. Without any constraint,
there’s nothing to ensure that the values of these identifiers in the enrollments table match
existing entries in the students and courses tables.
Note that for these to be foreign keys, they must be primary keys in the students and
courses tables, respectively.
Just as with other constraints, we could also declare these after table creation using the
ALTER TABLE command:
Finally, we'll define the enrollments table, establishing the relationships between students
and courses:
We’ve added a few constraints in this final example. A university has a known set of
departments and majors. Therefore, the set of values that the major and department
columns can have is finite and known in advance. In these situations, using a CHECK
constraint is recommended to ensure that the columns can only take values from that
known set of values.
Conclusion
In this article, we explored the different types of integrity constraints in SQL and how to
implement them using PostgreSQL. We covered primary keys, NOT NULL constraints,
UNIQUE constraints, DEFAULT constraints, CHECK constraints, and FOREIGN KEY
constraints, providing practical examples for each.
By understanding these concepts, we can ensure the accuracy, consistency, and reliability
of our data.
If you want to learn more about organizing data efficiently, check out this course on
Database Design.
A UTHO R
François Aubry
Teaching has always been my passion. From my early days as a student, I eagerly sought
out opportunities to tutor and assist other students. This passion led me to pursue a PhD,
where I also served as a teaching assistant to support my academic endeavors. During
those years, I found immense fulfillment in the traditional classroom setting, fostering
connections and facilitating learning. However, with the advent of online learning platforms,
I recognized the transformative potential of digital education. In fact, I was actively
involved in the development of one such platform at our university. I am deeply committed
to integrating traditional teaching principles with innovative digital methodologies. My
passion is to create courses that are not only engaging and informative but also accessible
to learners in this digital age.
T O PI C S
See Details Start Course See Details Start Course See Details Start Course
See More
Related
T U T O RI A L T U T O RIA L
T U T O RI A L
See More
L E AR N DATA C O U R S E S DATAL AB
RESOURCES P L AN S AB O U T
Help Center
Become an Affiliate
Privacy Policy Cookie Notice Do Not Sell My Personal Information Accessibility Security Terms of Use
DataCamp and our partners use cookies to improve your learning experience, offer content relevant to your interests and show more
Accept
relevant advertisements. You can change your mind at any time (learn more & configure).