Study smarter with Fiveable
Get study guides, practice questions, and cheatsheets for all your subjects. Join 500,000+ students with a 96% pass rate.
Database constraints are the guardrails that keep your data clean, consistent, and reliable—and they're absolutely central to how relational databases work. When you're tested on database systems, you're being evaluated on your understanding of data integrity, referential relationships, and validation mechanisms. These aren't just syntax rules to memorize; they represent fundamental design decisions that prevent corrupted data, broken relationships, and invalid entries from ever entering your system.
Think of constraints as a contract between your database and the real world it models. Primary keys ensure every entity is uniquely identifiable. Foreign keys maintain logical relationships between tables. Check and domain constraints enforce business rules at the data level. As you study these, don't just memorize what each constraint does—understand which integrity problem each one solves and when you'd choose one over another in a database design scenario.
These constraints answer a fundamental question: how do we ensure each record is distinct and identifiable? Without them, you'd have no reliable way to reference specific rows or prevent duplicate entries.
Compare: Primary Key vs. Unique Constraint—both enforce uniqueness, but primary keys cannot be NULL and serve as the table's official identifier. Use unique constraints for alternate keys like email addresses. If asked to design a schema, remember: one primary key, but potentially many unique constraints.
These constraints maintain the logical connections between tables. In a relational database, data is spread across multiple tables—these rules ensure those relationships stay valid.
Compare: Foreign Key Constraint vs. Referential Integrity—foreign keys are the mechanism, referential integrity is the principle. Think of referential integrity as the rule ("no orphans allowed") and foreign keys as the enforcement tool. FRQs often ask you to explain why a deletion fails—the answer is referential integrity violation.
These constraints control what data can be entered into specific columns. They're your first line of defense against invalid, incomplete, or nonsensical data.
Compare: Check Constraint vs. Domain Constraint—check constraints are specific rules you write (like ), while domain constraints represent the entire valid value space for a column including its data type. Domain is the concept; check is one implementation tool.
These constraints handle special cases: what happens when no value is provided, and how do you enforce rules that span multiple tables?
Compare: Check Constraint vs. Assertion—check constraints validate single rows in one table, while assertions can enforce conditions across multiple tables or aggregate values. If an exam question involves a rule spanning two tables, assertion is the theoretical answer (though triggers are the practical solution).
| Concept | Best Examples |
|---|---|
| Unique identification | Primary Key, Entity Integrity |
| Alternate uniqueness | Unique Constraint |
| Table relationships | Foreign Key, Referential Integrity |
| Mandatory values | Not Null Constraint |
| Value validation | Check Constraint, Domain Constraint |
| Automatic values | Default Constraint |
| Complex/multi-table rules | Assertion |
| Prevents orphaned records | Foreign Key, Referential Integrity |
Which two constraints both enforce uniqueness, and what key difference determines when you'd use each one?
A DELETE operation fails with a "referential integrity violation" error. Explain what caused this and name two ways to resolve it.
Compare and contrast check constraints and domain constraints—when would you use each, and how do they overlap?
You're designing a table where the "email" column must be unique but the "phone" column is optional. Which constraints would you apply to each, and why?
An FRQ asks you to enforce a rule that "no department's total salary budget can exceed $1 million." Why can't a check constraint handle this, and what constraint type would you theoretically use?