Welcome back. In Lesson 3, you learned how keys and constraints prevent the most expensive database problems:
duplicates, orphan records, and invalid values.
Now we’re going to connect the dots—literally. Real systems are not one table. They are many tables connected together.
Understanding relationships and referential integrity is what turns you from “someone who can write SQL”
into someone who can design and protect a real production database.
Lesson description: You will learn the main relationship types (one-to-many, one-to-one, many-to-many),
how to model them in SQL Server, how referential integrity works, what cascade actions do (and why DBAs use them carefully),
and how to spot and prevent design mistakes that cause missing data, double-counting, and broken reports.
A relationship describes how rows in one table connect to rows in another table. In SQL Server, relationships are
typically enforced using foreign keys.
Simple way to think about it: A relationship answers questions like:
DBA mindset: Relationships are not optional “nice-to-haves.” They are what makes the database consistent.
If you model relationships incorrectly, your application might still run—but your reports and operations will become unreliable.
One row in Table A can relate to many rows in Table B.
This is the most common relationship in OLTP systems.
How it is modeled: The “many” table stores the foreign key.
Example: Booking.CustomerID references Customer.CustomerID.
One row in Table A relates to exactly one row in Table B.
This relationship is less common and is often used for separation of concerns:
security, privacy, optional details, or very large columns you don’t want in the main table.
How it is modeled: Table B references Table A, and you enforce uniqueness so it cannot relate to multiple rows.
In practice: a foreign key plus a UNIQUE constraint (or shared primary key design).
DBA caution: Many teams create 1:1 tables unnecessarily. If you can keep data in one table safely, do it.
Use 1:1 when there is a clear reason (security boundaries, optional-heavy columns, performance considerations).
Many rows in Table A relate to many rows in Table B.
SQL Server does not model this directly—you implement it using a junction table (also called a bridge table).
How it is modeled: Create a junction table that contains two foreign keys:
one to Table A and one to Table B. The junction table is the “relationship store.”
DBA best practice: Add a composite primary key or unique constraint on the two foreign key columns
to prevent duplicate relationships (e.g., the same user-role assignment stored twice).
Referential integrity means: every foreign key value must reference an existing primary key value.
In other words, SQL Server ensures relationships remain valid.
If referential integrity is not enforced, you get:
DBA rule: Enforce referential integrity by default. Avoid exceptions unless you have a documented reason.
SQL Server foreign keys can be configured with cascading behaviors. This controls what happens in the “child” table
when a row in the “parent” table is updated or deleted.
DBA caution (very important): Cascades can cause unexpected large deletes or updates and can impact performance.
In production systems, many DBAs prefer:
NO ACTION + explicit application logic (or stored procedures) for controlled deletes.
Professional standard: If cascades are used, they should be intentional, documented, and tested in non-production first.
Example: storing roles as 'Admin,Reporting,Support' in a single column.
This breaks relational design and makes queries, constraints, and indexing much harder.
Fix: Create a junction table (UserRole) and enforce relationships properly.
This creates silent data corruption over time. Application code changes; the database must stay correct.
Fix: Enforce foreign keys unless you have a proven exception and can accept the risk.
Example: assuming one customer can only have one booking, then later discovering repeat customers exist.
Fix: Model relationships based on real business requirements and future growth, not a temporary assumption.
Example: the same user-role assignment stored multiple times.
Fix: Use a composite primary key or UNIQUE constraint on (UserID, RoleID).
Relationships must be verified, not assumed. In a lab/test environment, DBAs validate that referential integrity
is truly enforced.
This evidence-first discipline will become your signature behavior as a professional DBA.
In the next lesson, we will study transactions: how SQL Server guarantees “all-or-nothing” reliability,
what ACID really means in production, and how mistakes (long transactions, poor isolation choices, bad error handling)
can cause blocking, deadlocks, and inconsistent business results.
Not a member yet? Register now
Are you a member? Login now