Chapter 6: Constraints

Constraints are the rules you place on your columns to make sure the data inside your tables is correct, consistent, and meaningful. Think of them as the “guardrails” that prevent bad data from entering your database — like making sure no one enters a negative age, or duplicate email addresses, or orders linked to non-existent customers.

Today we’re going to learn all the major constraints you’ll use every day:

  • NOT NULL → This field cannot be empty
  • UNIQUE → No two rows can have the same value in this column
  • PRIMARY KEY → Unique + Not Null + identifies each row
  • FOREIGN KEY → Links this column to another table’s primary key
  • CHECK → Custom rule (e.g., age > 18)
  • DEFAULT → Automatically fill a value if none is provided
  • AUTO_INCREMENT / IDENTITY → Automatically generate unique numbers (usually for IDs)

We’ll explain each one in detail with real examples, common mistakes, best practices, and full runnable code. We’ll continue using our bookshop database as the example.

Let’s start!

1. NOT NULL – The Field Cannot Be Empty

This is the simplest and most common constraint. It means: You must provide a value when inserting or updating this column — no NULL allowed.

Syntax:

SQL

Example:

SQL

What happens if you try to insert without title?

SQL

Best Practice: Use NOT NULL for almost every important column: names, emails, prices, dates, etc. Only allow NULL when it truly makes sense (e.g., middle_name might be optional).

2. UNIQUE – No Duplicates Allowed

Ensures that all values in this column are different — no two rows can have the same value.

Syntax:

SQL

Example:

SQL

What happens if you try duplicate email?

SQL

Important: UNIQUE allows one NULL value (in most databases), but NOT NULL + UNIQUE is the common combo for emails, usernames, etc.

3. PRIMARY KEY – The Unique Identifier for Each Row

A primary key is a column (or combination of columns) that:

  • Is UNIQUE
  • Is NOT NULL
  • Uniquely identifies every row in the table

Almost every table should have one!

Syntax:

SQL

Example:

SQL

Alternative (composite primary key – rare but useful):

SQL

Best Practice: Use a single-column surrogate key (like id INT AUTO_INCREMENT PRIMARY KEY) — simple, fast, and reliable.

4. FOREIGN KEY – Linking Tables Together

A foreign key is a column that references the primary key of another table. It enforces referential integrity — you can’t have an order for a customer who doesn’t exist!

Syntax:

SQL

Real example – Two tables linked:

SQL

What happens if you try to insert a book with non-existent author?

SQL

Bonus options (very useful):

SQL

5. CHECK – Custom Rules for Values

Lets you define your own rule — e.g., price must be positive, age > 18, etc.

Syntax:

SQL

Example:

SQL

Modern syntax (column-level or table-level):

SQL

6. DEFAULT – Automatically Fill a Value

If no value is provided during INSERT, use this default value.

Syntax:

SQL

Example:

SQL

Insert without specifying:

SQL

7. AUTO_INCREMENT / IDENTITY – Automatic Unique Numbers

Automatically generates the next number for IDs — perfect for primary keys.

MySQL / MariaDB / SQLite:

SQL

SQL Server:

SQL

PostgreSQL:

SQL

Example:

SQL

Important: AUTO_INCREMENT resets only with TRUNCATE TABLE — not with DELETE.

Full Realistic Example – All Constraints Together

SQL

Common Mistakes & How to Avoid Them

Mistake Consequence Fix / Best Practice
Forgetting NOT NULL on important columns Lots of NULLs → bad data Always think: “Must this always have a value?”
Using FLOAT for money Rounding errors (₹9.99 → 9.989999) Use DECIMAL for money
No FOREIGN KEY → orphan records Orders for deleted customers Always add FK when tables are related
Forgetting to add index on FK columns Slow queries MySQL auto-indexes FK, but check performance
Using AUTO_INCREMENT without UNSIGNED

You may also like...

Leave a Reply

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