SQL Constraints are rules applied to table columns that control the type of data that can be stored in a database. Constraints help maintain data accuracy, consistency, reliability, and integrity by preventing invalid data from entering database tables.
In real-world database systems, thousands or even millions of records are stored and processed daily. Without constraints, users could enter duplicate values, invalid information, or inconsistent data. SQL constraints act as safeguards that ensure only valid data is stored.
For example, a student ID should be unique for every student, and an employee's salary should never be negative. Constraints make these rules enforceable automatically by the database management system.
Constraints play a crucial role in database design because they ensure data remains accurate and meaningful throughout the lifecycle of an application.
SQL provides several types of constraints to enforce different rules on table columns.
| Constraint | Purpose |
|---|---|
| NOT NULL | Prevents NULL values in a column |
| UNIQUE | Ensures all values are unique |
| PRIMARY KEY | Uniquely identifies each record |
| FOREIGN KEY | Creates relationships between tables |
| CHECK | Validates values using conditions |
| DEFAULT | Assigns a default value automatically |
The NOT NULL constraint ensures that a column cannot contain NULL values. Every record must provide a value for that column.
ColumnName DataType NOT NULL
CREATE TABLE Student ( StudentID INT, StudentName VARCHAR(100) NOT NULL );
In this example, StudentName cannot be left empty when inserting records.
The UNIQUE constraint ensures that all values in a column are different. Duplicate values are not allowed.
ColumnName DataType UNIQUE
CREATE TABLE Student ( StudentID INT, Email VARCHAR(100) UNIQUE );
Each email address must be unique in the table.
A PRIMARY KEY uniquely identifies every record in a table. It combines the properties of NOT NULL and UNIQUE.
ColumnName DataType PRIMARY KEY
CREATE TABLE Student ( StudentID INT PRIMARY KEY, StudentName VARCHAR(100) );
No two students can have the same StudentID, and StudentID cannot be NULL.
A FOREIGN KEY creates a relationship between two tables. It ensures that values in one table match values in another table.
CREATE TABLE Department ( DepartmentID INT PRIMARY KEY, DepartmentName VARCHAR(100) ); CREATE TABLE Employee ( EmployeeID INT PRIMARY KEY, EmployeeName VARCHAR(100), DepartmentID INT, FOREIGN KEY (DepartmentID) REFERENCES Department(DepartmentID) );
Employees can only be assigned to departments that already exist in the Department table.
The CHECK constraint restricts values based on a specified condition.
CREATE TABLE Student ( StudentID INT PRIMARY KEY, Age INT CHECK (Age >= 18) );
Only students with age 18 or greater can be inserted.
The DEFAULT constraint automatically assigns a value when no value is provided.
CREATE TABLE Employee ( EmployeeID INT PRIMARY KEY, Status VARCHAR(20) DEFAULT 'Active' );
If no status is specified, the value "Active" is automatically assigned.
CREATE TABLE Student ( StudentID INT PRIMARY KEY, StudentName VARCHAR(100) NOT NULL, Email VARCHAR(100) UNIQUE, Age INT CHECK (Age >= 18), Course VARCHAR(50) DEFAULT 'B.Tech' );
This table demonstrates multiple constraints working together to maintain data quality and integrity.
| Feature | PRIMARY KEY | UNIQUE |
|---|---|---|
| Duplicate Values | Not Allowed | Not Allowed |
| NULL Values | Not Allowed | Usually One NULL Allowed |
| Count Per Table | One | Multiple |
SQL Constraints are essential tools for maintaining data integrity and ensuring the accuracy of information stored in databases. They help enforce business rules, prevent invalid data, and improve overall database reliability.
By understanding and correctly implementing NOT NULL, UNIQUE, PRIMARY KEY, FOREIGN KEY, CHECK, and DEFAULT constraints, developers can build secure, efficient, and well-structured database systems.