Course contentsShow
← Computer Science
Lesson 1557 of 2,87236. Database FundamentalsFree lesson

Relational Integrity Constraints

Enforcing data quality through entity integrity (primary keys cannot be null) and referential integrity (foreign keys must be valid).

Relational Integrity Constraints

What You'll Learn

You'll learn how databases enforce data quality through two fundamental rules: entity integrity (ensuring every row has a valid identifier) and referential integrity (ensuring relationships between tables remain valid).

The Core Idea

Databases aren't just fancy spreadsheets—they actively protect your data from becoming corrupted or inconsistent. Relational integrity constraints are the rules that databases enforce automatically to maintain data quality.

Entity Integrity

Entity integrity means that every row in a table must be uniquely identifiable and actually exist. This boils down to one simple rule:

Primary keys cannot be null.

Think of it like this: imagine a library where some books have no catalog number. How would you find them? How would you know if you have duplicates? Primary keys are the identity of each row—without one, the row is essentially invisible to the database's organizational system.

Referential Integrity

Referential integrity ensures that relationships between tables remain valid. When one table references another using a foreign key, that reference must point to something real.

Foreign keys must reference valid primary keys (or be null if allowed).

Analogy: If your employee record says you work in "Department 42," but Department 42 doesn't exist in the Departments table, you have a problem. Referential integrity prevents this orphaned data—it's like having a rule that says "you can't assign someone to a department that doesn't exist."

How It Works

When you try to:

  • Insert a row with a null primary key → blocked
  • Delete a department that has employees → blocked (or cascades, depending on rules)
  • Add an employee to a non-existent department → blocked

The database actively rejects these operations to keep your data consistent.

Key Takeaway: Entity integrity ensures every row has a valid identity (primary keys can't be null), while referential integrity ensures relationships between tables always point to real data (foreign keys must reference existing primary keys).