codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🗄️

SQL & Databases

11 / 67 topics
6Creating a Database7Tables and Columns8Data Types9Primary Keys10Foreign Keys11Constraints
Tutorials/SQL & Databases/Constraints
🗄️SQL & Databases

Constraints

Updated 2026-05-15
10 min read

Constraints

Introduction

In the realm of database management, ensuring data integrity is paramount. Constraints are essential tools that enforce specific rules and conditions on table data to maintain accuracy and consistency. They help prevent invalid or inconsistent data from being entered into the database, thereby safeguarding the reliability of your applications.

Constraints can be applied at the column level or the table level, and they come in various types, each serving a unique purpose. In this tutorial, we will explore different types of constraints, how to implement them, and their significance in maintaining data integrity.

Concept

What are Constraints?

Constraints are rules that limit the type of data that can be stored in a database table. They ensure that the data adheres to certain standards, thereby maintaining the accuracy and reliability of the information. Constraints can be applied at the column level or the table level, and they help prevent invalid or inconsistent data from being entered into the database.

Types of Constraints

  1. NOT NULL: Ensures that a column cannot have NULL values.
  2. UNIQUE: Ensures that all values in a column are unique.
  3. PRIMARY KEY: A combination of NOT NULL and UNIQUE constraints, ensuring that each row in a table has a unique identifier.
  4. FOREIGN KEY: Ensures referential integrity between tables by linking a column or set of columns to the primary key of another table.
  5. CHECK: Ensures that all values in a column satisfy a specific condition.

Examples

NOT NULL Constraint

The NOT NULL constraint ensures that a column cannot have NULL values. This is useful for ensuring that certain fields are always populated.

SQL
1CREATE TABLE Employees (
2 EmployeeID INT NOT NULL,
3 FirstName VARCHAR(50),
4 LastName VARCHAR(50)
5);

In this example, the EmployeeID column cannot have NULL values. Attempting to insert a row without specifying an EmployeeID will result in an error.

UNIQUE Constraint

The UNIQUE constraint ensures that all values in a column are unique. This is useful for columns like email addresses or usernames where uniqueness is required.

SQL
1CREATE TABLE Users (
2 UserID INT NOT NULL,
3 Username VARCHAR(50) UNIQUE,
4 Email VARCHAR(100)
5);

In this example, the Username column must contain unique values. Attempting to insert a row with a duplicate username will result in an error.

PRIMARY KEY Constraint

The PRIMARY KEY constraint is a combination of NOT NULL and UNIQUE constraints. It ensures that each row in a table has a unique identifier.

SQL
1CREATE TABLE Orders (
2 OrderID INT PRIMARY KEY,
3 CustomerID INT,
4 OrderDate DATE
5);

In this example, the OrderID column serves as the primary key for the Orders table. Each order must have a unique OrderID.

FOREIGN KEY Constraint

The FOREIGN KEY constraint ensures referential integrity between tables by linking a column or set of columns to the primary key of another table.

SQL
1CREATE TABLE Customers (
2 CustomerID INT PRIMARY KEY,
3 CustomerName VARCHAR(100)
4);
5
6CREATE TABLE Orders (
7 OrderID INT PRIMARY KEY,
8 CustomerID INT,
9 OrderDate DATE,
10 FOREIGN KEY (CustomerID) REFERENCES Customers(CustomerID)
11);

In this example, the CustomerID column in the Orders table is a foreign key that references the CustomerID column in the Customers table. This ensures that each order is linked to an existing customer.

CHECK Constraint

The CHECK constraint ensures that all values in a column satisfy a specific condition. This is useful for enforcing business rules or data validation.

SQL
1CREATE TABLE Products (
2 ProductID INT PRIMARY KEY,
3 ProductName VARCHAR(100),
4 Price DECIMAL(10, 2) CHECK (Price > 0)
5);

In this example, the Price column must have a value greater than zero. Attempting to insert a row with a non-positive price will result in an error.

What's Next?

Now that you understand how constraints work and how to implement them, you can move on to more advanced topics such as SQL Joins. In the next section, we will explore various types of joins and how they are used to combine data from multiple tables.

Stay tuned for more tutorials on database management and SQL!


PreviousForeign KeysNext Creating Tables

Recommended Gear

Foreign KeysCreating Tables