Learn With Grito

SQL Constraints
Explained for Beginners

SQL tables can store data. But without rules, data becomes chaos. Imagine a customer table where two customers have the same ID. That is exactly why SQL constraints exist.

Tutorial Series8 Mins ReadSQL Level 5

SQL tables can store data. But without rules, data becomes chaos.

Imagine a customer table where:

  • two customers have the same ID
  • names are blank
  • orders reference customers that do not exist
  • discount values are negative
  • critical fields are missing

That is exactly why SQL constraints exist. SQL constraints enforce rules on your data. If SQL data types define what kind of data a column can store, constraints define what rules that data must follow.

In this guide, you’ll learn:

  • what SQL constraints are
  • why they matter for data analysts
  • PRIMARY KEY
  • FOREIGN KEY
  • UNIQUE
  • NOT NULL
  • CHECK
  • DEFAULT
  • common mistakes
  • interview questions

This is foundational SQL.

What Are SQL Constraints?

SQL constraints are rules applied to table columns. They help maintain data integrity.

Simple meaning: Constraints stop bad data from entering your database.

Example Query
CREATE TABLE customers (
   customer_id INT PRIMARY KEY,
   customer_name VARCHAR(100) NOT NULL
);

This means:

  • customer_id must be unique
  • customer_name cannot be empty

Without constraints, databases become unreliable. And unreliable data creates bad analysis.

Why SQL Constraints Matter for Data Analysts

Beginners often think constraints are only for database engineers. That is a mistake. Analysts constantly deal with the consequences of weak database design.

Real-world problems caused by missing constraints:

Duplicate Customer IDs

customer_idname
101Rahul
101Priya

Now your joins break. Metrics become wrong.

Missing Customer Names

customer_idcustomer_name
102NULL

Your reports look broken.

Orders Without Customers

order_idcustomer_id
5019999

Customer does not exist. Relationship broken.

For analysts: Good constraints reduce dirty data. Dirty data reduces analysis quality.

Types of SQL Constraints

The beginner essentials:

  • PRIMARY KEY
  • FOREIGN KEY
  • UNIQUE
  • NOT NULL
  • CHECK
  • DEFAULT

These appear constantly in interviews, schemas, and real databases.

1. PRIMARY KEY in SQL

A PRIMARY KEY uniquely identifies each row in a table.

Rules:

  • must be unique
  • cannot be NULL
Example
CREATE TABLE customers (
  customer_id INT PRIMARY KEY,
  customer_name VARCHAR(100)
);
customer_idcustomer_name
101Rahul
102Priya
103Arjun
customer_idcustomer_name
101Rahul
101Priya

Why Analysts Care

Tables are joined using primary keys. Without reliable IDs:

  • joins break
  • duplicates happen
  • revenue gets overstated
Real Analyst Example
SELECT *
FROM customers c
INNER JOIN orders o
ON c.customer_id = o.customer_id;

This only works cleanly if customer IDs are trustworthy.

2. FOREIGN KEY in SQL

A FOREIGN KEY creates relationships between tables. It ensures referenced values exist elsewhere.

Example
-- Customers table:
CREATE TABLE customers (
   customer_id INT PRIMARY KEY
);

-- Orders table:
CREATE TABLE orders (
   order_id INT PRIMARY KEY,
   customer_id INT,
   FOREIGN KEY (customer_id) REFERENCES customers(customer_id)
);

Meaning: Every order must reference a valid customer.

order_idcustomer_id
5002999

Rejected. Because customer 999 does not exist.

Why Analysts Care

Foreign keys maintain business relationships. Without them:

  • orphan records appear
  • joins fail
  • business logic breaks

The Grito Factor Some companies skip foreign key enforcement entirely for performance or engineering flexibility. The result? Analysts discover “ghost orders,” missing users, and broken relationships months later during reporting.

3. UNIQUE Constraint in SQL

UNIQUE prevents duplicate values. Unlike PRIMARY KEY, UNIQUE prevents duplicates but is often used for business identifiers rather than being the official row identifier.

Example
CREATE TABLE users (
   email VARCHAR(255) UNIQUE
);

Meaning: No duplicate email addresses.

Good candidates for UNIQUE:

  • email
  • employee ID
  • username
  • SKU code

PRIMARY KEY vs UNIQUE

PRIMARY KEY

Unique, not null, identifies table rows. (One per table)

UNIQUE

Prevents duplicates, often used for business identifiers. (Multiple allowed)

4. NOT NULL Constraint in SQL

NOT NULL prevents empty values.

Example
CREATE TABLE customers (
   customer_name VARCHAR(100) NOT NULL
);

Meaning: Every row must have a name.

Why Analysts Care

Missing values create reporting issues. Looks unprofessional. Breaks dashboards.

Good Candidates for NOT NULL:

  • names
  • signup dates
  • order totals
  • status fields
  • IDs

5. CHECK Constraint in SQL

CHECK enforces custom validation rules.

Example
CREATE TABLE employees (
   age INT CHECK (age >= 18)
);

Real Business Examples:

  • Discount percentage: CHECK (discount >= 0)
  • Ratings: CHECK (rating BETWEEN 1 AND 5)
  • Revenue: CHECK (revenue >= 0)
  • Subscription plan: CHECK (plan_type IN ('Free', 'Basic', 'Premium'))

Why Analysts Care: CHECK reduces garbage data. Cleaner data means cleaner metrics.

6. DEFAULT Constraint in SQL

DEFAULT automatically assigns a value if none is provided.

Example
CREATE TABLE users (
   is_active BOOLEAN DEFAULT TRUE
);

Real Business Examples:

  • Default signup date: DEFAULT CURRENT_DATE
  • Default account status: DEFAULT 'Active'
  • Default quantity: DEFAULT 1

Why Analysts Care: Defaults reduce missing values. This improves consistency.

SQL Constraints Summary Table

ConstraintPurpose
PRIMARY KEYuniquely identifies rows
FOREIGN KEYmaintains relationships
UNIQUEprevents duplicates
NOT NULLprevents empty values
CHECKenforces validation rules
DEFAULTauto-fills values

Real Business Schema Example

Ecommerce schema:

Schema Example
CREATE TABLE customers (
   customer_id INT PRIMARY KEY,
   customer_name VARCHAR(100) NOT NULL,
   email VARCHAR(255) UNIQUE,
   signup_date DATE DEFAULT CURRENT_DATE
);

CREATE TABLE orders (
   order_id INT PRIMARY KEY,
   customer_id INT,
   revenue DECIMAL(12,2) CHECK (revenue >= 0),
   FOREIGN KEY (customer_id) REFERENCES customers(customer_id)
);

This is realistic schema design.

Common Beginner Mistakes

1. Confusing PRIMARY KEY and UNIQUE

PRIMARY KEY is the main identifier. UNIQUE just prevents duplicates.

2. Forgetting FOREIGN KEY Relationships

Then joins become messy. Business logic breaks.

3. Allowing Critical NULL Values

Bad: customer_name VARCHAR(100)
Better: customer_name VARCHAR(100) NOT NULL

4. No CHECK Validation

Then bad values enter. Examples: negative revenue, impossible ratings, invalid ages.

5. Overusing Constraints Without Thinking

Constraints should protect logic, not create unnecessary friction.

SQL Constraints in Interviews

Interviewers care about practical understanding. Not textbook memorization.

Common questions:

  • What is a PRIMARY KEY?
  • PRIMARY KEY vs UNIQUE?
  • What is FOREIGN KEY?
  • Why use NOT NULL?
  • What does CHECK do?
  • What is DEFAULT?
  • Why are constraints important?

Practice Questions

  1. Which constraint uniquely identifies rows?
  2. Which constraint links tables?
  3. Which constraint prevents empty values?
  4. Which constraint prevents duplicates?
  5. Which constraint validates custom conditions?
  6. Which constraint auto-fills values?

What Comes Next?

Constraints define the rules. Next, you learn how SQL compares and filters values using operators. Because real querying depends on conditions.

Final Thoughts

SQL constraints are one of the most overlooked beginner concepts. But they shape data quality. And data quality shapes analysis quality.

For data analysts, understanding constraints helps you:

  • debug messy datasets
  • understand schemas faster
  • trust joins
  • interpret business tables correctly
  • perform better in interviews

Learn constraints early. They make everything else easier.

Grit Over Excuses.

— The Grito Team