Welcome back. In the previous lesson, you learned how tables, rows, columns, and constraints create structure and reliability.
Now we’re going to focus on the rules that protect databases from the most expensive problems in the real world:
duplicates, orphan records, and invalid values.
Think of this lesson as your “DBA safety system” lesson. If you master keys and constraints, you will build databases that
applications can trust—even when users make mistakes or developers accidentally ship bugs.
Lesson description: You will learn exactly what primary keys, foreign keys, unique constraints, and check constraints do,
why they are essential in production, how to choose the right strategy (surrogate vs natural keys),
how to avoid common constraint mistakes, and how DBAs verify enforcement using evidence-first testing.
A DBA’s goal is not only to keep SQL Server online. A DBA must keep the data correct.
Keys and constraints are how SQL Server enforces correctness at the database level.
Without these rules, the application might still “work” for a while—but over time it becomes unreliable.
That unreliability shows up as billing mistakes, missing records, broken reports, and incidents that cost money.
A Primary Key uniquely identifies each row in a table. It answers the question:
“How do we point to exactly one row, every time, without confusion?”
In many production systems, the safest pattern is:
use a surrogate PK (like INT IDENTITY) for identity and joins,
and enforce real-world uniqueness using UNIQUE constraints (Email, InvoiceNumber, etc.).
A Foreign Key enforces relationships between tables.
It guarantees that when you store a reference (like CustomerID in an Order table),
that referenced Customer actually exists.
DBA note: Some teams try to avoid foreign keys for “performance reasons.”
In most systems, missing foreign keys cost more in operational failures than they save in performance.
A professional DBA pushes for integrity unless there is a proven, documented exception.
A UNIQUE constraint ensures that a value cannot appear more than once in a column (or combination of columns).
This prevents business-breaking duplicates.
SQL Server allows multiple NULLs in a UNIQUE constraint (because NULL means “unknown”).
If your system requires “value must be present and unique,” then enforce:
NOT NULL + UNIQUE together.
A CHECK constraint enforces rules about allowed values or ranges.
This prevents the database from storing values that should never exist.
TotalAmount >= 0Quantity > 0Status IN ('NEW','CONFIRMED','COMPLETED','CANCELED')DiscountPercent BETWEEN 0 AND 100DBA mindset: CHECK constraints are one of the easiest ways to stop data disasters early.
They create guardrails so the application cannot accidentally store nonsense.
When you review a table as a DBA, you want to see these protections in place:
In real DBA work, you never assume constraints are doing what you think.
You test them safely in a lab/test environment.
This is the same evidence-first discipline you’ll apply in backups, restores, performance tuning, and incident response.
In the next lesson, we will zoom out and focus on how tables connect to each other:
one-to-many, one-to-one, and many-to-many relationships, junction tables, and how referential integrity
keeps the entire database consistent as systems grow.
Not a member yet? Register now
Are you a member? Login now