SQL Constraints

Overview

SQL Constraints are rules enforced on data columns to limit the type of data that can go into a table. Constraints ensure data integrity and accuracy in your database.

Constraints can be specified when a table is created (with CREATE TABLE) or after the table is created (with ALTER TABLE). They help maintain data quality and prevent invalid data from entering the database.

Types of Constraints

SQL supports several types of constraints:

Constraint Description Purpose
PRIMARY KEY Uniquely identifies each row Entity integrity
FOREIGN KEY Links to another table's primary key Referential integrity
NOT NULL Column cannot be empty Required fields
UNIQUE Column values must be unique Prevent duplicates
CHECK Validates column values Value validation
DEFAULT Sets default value Default values

NOT NULL Constraint

The NOT NULL constraint ensures a column cannot have NULL values:

NOT NULL Example
CREATE TABLE Customers (
    customer_id INT PRIMARY KEY,
    name VARCHAR(100) NOT NULL,
    email VARCHAR(100) NOT NULL,
    phone VARCHAR(20)  -- Can be NULL
);

UNIQUE Constraint

The UNIQUE constraint ensures all values in a column are different:

UNIQUE Example
CREATE TABLE Customers (
    customer_id INT PRIMARY KEY,
    email VARCHAR(100) UNIQUE,
    phone VARCHAR(20) UNIQUE
);

CHECK Constraint

The CHECK constraint validates values in a column:

CHECK Example
CREATE TABLE Products (
    product_id INT PRIMARY KEY,
    product_name VARCHAR(200) NOT NULL,
    price DECIMAL(10, 2) CHECK (price > 0),
    stock_quantity INT CHECK (stock_quantity >= 0)
);

DEFAULT Constraint

The DEFAULT constraint sets a default value for a column when no value is specified:

DEFAULT Example
CREATE TABLE Orders (
    order_id INT PRIMARY KEY,
    order_date DATE DEFAULT CURRENT_DATE,
    status VARCHAR(20) DEFAULT 'Pending',
    total_amount DECIMAL(10, 2) DEFAULT 0.00
);

PRIMARY KEY Constraint

The PRIMARY KEY constraint uniquely identifies each row (covered in detail in PRIMARY KEY tutorial):

PRIMARY KEY Example
CREATE TABLE Customers (
    customer_id INT PRIMARY KEY,
    name VARCHAR(100) NOT NULL,
    email VARCHAR(100)
);

FOREIGN KEY Constraint

The FOREIGN KEY constraint links tables together (covered in detail in FOREIGN KEY tutorial):

FOREIGN KEY Example
CREATE TABLE Orders (
    order_id INT PRIMARY KEY,
    customer_id INT NOT NULL,
    order_date DATE,
    FOREIGN KEY (customer_id) REFERENCES Customers(customer_id)
);

Combining Constraints

You can use multiple constraints on a single column:

Multiple Constraints
CREATE TABLE Products (
    product_id INT PRIMARY KEY,
    product_name VARCHAR(200) NOT NULL UNIQUE,
    price DECIMAL(10, 2) NOT NULL CHECK (price > 0),
    category VARCHAR(50) DEFAULT 'General',
    created_date DATE DEFAULT CURRENT_DATE
);

Adding Constraints After Table Creation

Constraints can be added using ALTER TABLE:

Add Constraints Later
-- Add NOT NULL
ALTER TABLE Customers
MODIFY COLUMN email VARCHAR(100) NOT NULL;

-- Add UNIQUE
ALTER TABLE Customers
ADD UNIQUE (email);

-- Add CHECK
ALTER TABLE Products
ADD CHECK (price > 0);

-- Add DEFAULT
ALTER TABLE Orders
ALTER COLUMN status SET DEFAULT 'Pending';

Dropping Constraints

Constraints can be removed using ALTER TABLE:

Remove Constraints
-- Remove NOT NULL (varies by database)
ALTER TABLE Customers
MODIFY COLUMN email VARCHAR(100) NULL;

-- Remove UNIQUE
ALTER TABLE Customers
DROP INDEX email;

-- Remove CHECK (syntax varies)
ALTER TABLE Products
DROP CHECK chk_price;

Benefits of Constraints

Best Practices

Next Steps

Learn about specific constraints: PRIMARY KEY and FOREIGN KEY.