Physical Design   «Prev  Next»

Lesson 5 Constraints and Keys
Objective Describe common mistakes associated with constraints and keys in relational databases.

Common Mistakes with Constraints and Keys

Constraints and keys form the foundation of relational database integrity. A well-designed database enforces business rules at the schema level, ensuring that data remains accurate and consistent. However, when constraints are poorly defined or keys are misused, the results can be data anomalies, duplicate records, and lost referential integrity. This lesson explains common pitfalls and how to avoid them.

1. Mistakes with Constraints

Constraints define the rules that limit what data can be inserted or updated in a column. They are essential for enforcing business logic such as valid ranges, uniqueness, and relationships. One frequent source of errors is incorrectly defining or mistyping a constraint, which can silently invalidate a rule.

For example, to ensure that the total cost of an order is between $1 and $500, you might define a check constraint like this:


CHECK (OrderCost >= 1 AND OrderCost <= 500)

If the constraint is mistyped as:


CHECK (OrderCost < 1 AND OrderCost < 500)

the database will only accept values less than $1, making it impossible to insert valid orders. When troubleshooting constraint issues, always verify that the logic precisely matches the intended business rule and test the constraint with representative data values.

2. Not Planning for Change

Database design should anticipate growth and flexibility. Overly rigid designs can make future modifications difficult, especially when constraints or table structures fail to consider potential exceptions. Listen carefully during requirements gathering for phrases such as “sometimes,” “except,” or “unless,” as these indicate conditions that may require a more flexible structure.

For instance, a customer might say: “Each order form must have a billing and shipping address - unless it’s a split order.” This statement reveals that two address fields are insufficient, and the address data should instead be modeled in a separate Addresses table to allow one-to-many relationships.

3. Real-World Example - Youth Soccer Database

Consider a small application designed to manage a youth soccer league. Figure 1 shows a relational model that enforces data integrity through proper use of primary and foreign keys. Constraints ensure that every player belongs to one or more games, and each parent record corresponds to a player.

games-gameplayers tables in database
Figure 1: Youth sports league database schema illustrating relationships among games, players, and parents.
  • Games - Contains details such as date, time, field, opponent, coach, and snack bringer.
  • GamePlayers - A linking (junction) table establishing a many-to-many (M:N) relationship between Games and Players.
  • Players - Lists individual player details with a unique PlayerId.
  • Parents - Stores contact and address information for each player’s parent or guardian, related through PlayerId.

Relationships: Games ↔ Players (M:N via GamePlayers); Players ↔ Parents (1:N); and optional linkage between Games and Parents for coordination roles such as SnackBringer.

4. Balancing Integrity and Flexibility

While constraints and keys maintain order, too many rigid rules can make a system inflexible. The goal is balance: enforce what must never change, but leave room for controlled evolution. For example, you may apply ON UPDATE CASCADE for foreign keys that naturally follow parent changes, while enforcing NOT NULL and UNIQUE constraints only where absolutely necessary.

Thoughtful design allows the database to enforce correctness automatically while adapting smoothly to new requirements - without requiring a major schema overhaul.


The next lesson explores common mistakes with primary and foreign keys and their impact on referential integrity.


SEMrush Software 5 SEMrush Banner 5