Preloader

SQL Constraints

SQL
SQL Tutorials

SQL constraints are rules that control what data can go into a table. They help keep data clean, correct, and meaningful. When you use constraints, the database stops wrong or duplicate data before it gets stored.

Think of constraints as guards that protect your data.

Why SQL Constraints Are Important

SQL constraints help you
avoid empty values
prevent duplicate data
keep relationships between tables correct
reduce errors in applications

Because of this, good database design always includes constraints.

Types of SQL Constraints

1. NOT NULL Constraint

The NOT NULL constraint makes sure a column always has a value. It does not allow empty entries.

Example

CREATE TABLE users (
  user_id INT NOT NULL,
  name VARCHAR(100) NOT NULL,
  email VARCHAR(100)
);

Here
user_id and name must have values
email can be empty

2. UNIQUE Constraint

The UNIQUE constraint makes sure all values in a column are different.

Example

CREATE TABLE users (
  email VARCHAR(100) UNIQUE
);

This prevents two users from using the same email address.

3. PRIMARY KEY Constraint

The PRIMARY KEY uniquely identifies each row in a table. Every table should have one primary key.

Rules of PRIMARY KEY

A primary key
must be unique
cannot be NULL
exists only once per table

Example

CREATE TABLE users (
  user_id INT PRIMARY KEY,
  name VARCHAR(100)
);

Each user gets a unique user_id. The database will reject duplicate or empty values.

4. FOREIGN KEY Constraint

The FOREIGN KEY connects two tables. It makes sure data in one table matches data in another table.

Example

CREATE TABLE orders (
  order_id INT PRIMARY KEY,
  user_id INT,
  FOREIGN KEY (user_id) REFERENCES users(user_id)
);

Here
user_id in orders must exist in users table
orders cannot link to invalid users

5. CHECK Constraint

The CHECK constraint allows only values that meet a condition.

Example

CREATE TABLE products (
  price INT CHECK (price > 0)
);

This ensures the price is always greater than zero.

6. DEFAULT Constraint

The DEFAULT constraint sets a value when no value is given.

Example

CREATE TABLE users (
  status VARCHAR(20) DEFAULT 'active'
);

If no status is provided, the database automatically sets it to active.

Adding Constraints to an Existing Table

You can also add constraints after creating a table.

Example

ALTER TABLE users
ADD CONSTRAINT unique_email UNIQUE (email);

Removing Constraints

You can remove a constraint if needed.

Example

ALTER TABLE users
DROP CONSTRAINT unique_email;

Simple Practice Example

Create Table

CREATE TABLE students (
  student_id INT PRIMARY KEY,
  name VARCHAR(100) NOT NULL,
  email VARCHAR(100) UNIQUE,
  age INT CHECK (age >= 18),
  status VARCHAR(20) DEFAULT 'active'
);

Valid Insert

INSERT INTO students (student_id, name, email, age)
VALUES (1, 'Amit', 'amit@example.com', 20);

Invalid Insert

INSERT INTO students (student_id, name, age)
VALUES (1, NULL, 16);

This will fail because
student_id is duplicate
name is NULL
age is below 18

Summary

SQL constraints control the data stored in tables.
They prevent wrong or duplicate data.
PRIMARY KEY and FOREIGN KEY manage table relationships.
Using constraints makes databases reliable and safe.

Check out our resources!

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *