0
0
SQLquery~3 mins

Why constraints matter in SQL - The Real Reasons

Choose your learning style9 modes available
The Big Idea

What if your database could catch your mistakes before they happen?

The Scenario

Imagine you are keeping track of your friends' phone numbers on paper. Sometimes you write the wrong number, or forget to write it at all. Later, when you want to call someone, you realize some numbers are missing or incorrect.

The Problem

Writing data manually or without rules leads to mistakes like duplicate entries, missing information, or wrong formats. Fixing these errors later is slow and frustrating, and can cause confusion or wrong decisions.

The Solution

Constraints in databases act like smart rules that automatically check your data. They stop wrong or duplicate information from entering the system, keeping your data clean and reliable without extra effort.

Before vs After
Before
INSERT INTO friends (name, phone) VALUES ('Alice', '12345');
INSERT INTO friends (name, phone) VALUES ('Alice', '');
After
ALTER TABLE friends ADD CONSTRAINT phone_not_null CHECK (phone <> '');
ALTER TABLE friends ADD CONSTRAINT unique_name UNIQUE (name);
What It Enables

With constraints, your database becomes a trustworthy assistant that keeps your data accurate and consistent automatically.

Real Life Example

Online stores use constraints to ensure every product has a unique ID and a price above zero, preventing errors that could confuse customers or cause financial mistakes.

Key Takeaways

Manual data entry often leads to errors and inconsistencies.

Constraints automatically enforce rules to keep data clean and reliable.

This saves time, reduces mistakes, and builds trust in your data.