Loading content...
Data without constraints is chaos. Imagine a database where customer ages can be negative, order totals can exceed available inventory, employees can report to themselves, and accounts can have balances that violate business policies. Such a system would be worse than useless—it would actively mislead.
Constraints are the rules that separate valid data from invalid data. They encode the business logic that ensures every record in the database represents something real, possible, and acceptable within the organization's context.
Constraint identification is the systematic process of discovering, documenting, and specifying these rules. It ensures that the database enforces data integrity—preventing errors at the source rather than detecting them after damage is done.
By the end of this page, you will understand how to identify and specify constraints for database design. You'll learn to recognize different constraint types, extract constraints from interviews and documents, formally specify constraints, and understand how constraints are implemented in database systems.
A constraint is a rule that restricts the values that can be stored in a database. Constraints ensure that data conforms to business reality and organizational policies.
Why Constraints Matter:
Data Quality: Constraints prevent invalid data from entering the database. An age cannot be -5. A hire date cannot be in the future. An order cannot reference a non-existent customer.
Business Rule Enforcement: Constraints encode business policies. A credit limit cannot be exceeded. An employee cannot be their own manager. A product cannot be sold below minimum price.
Referential Integrity: Constraints maintain relationships between entities. Every order must belong to a valid customer. Every line item must reference a valid product.
Consistency Guarantees: Constraints prevent inconsistent states. If a warehouse is deleted, what happens to its inventory? Constraints define and enforce these rules.
| Category | Description | Examples |
|---|---|---|
| Domain Constraints | Restrict values to valid sets for an attribute | Status ∈ {Active, Inactive, Suspended}; Age > 0 |
| Entity Constraints | Rules about individual entity instances | Every employee must have a unique ID; SSN format validation |
| Key Constraints | Uniqueness and identification rules | CustomerID is unique; OrderNumber is unique per year |
| Referential Constraints | Relationships between entities | Every Order must reference a valid Customer |
| Semantic Constraints | Business meaning and logic rules | EndDate must be after StartDate; ReorderPoint < MaxStock |
| Temporal Constraints | Time-based rules | RetirementDate > HireDate + 20 years; ContractEnd ≤ 5 years from ContractStart |
| Cardinality Constraints | Quantity limits on relationships | Each Department has 1-3 Managers; each Project has 2-10 Team Members |
There's a distinction between database constraints (enforced by the DBMS, cannot be violated) and application validation (enforced by code, can be bypassed). During requirements, capture all rules regardless of where they'll be implemented. The design phase decides what goes where.
Domain constraints define the set of valid values for each attribute. They are the most fundamental constraints because they establish what data is acceptable at the most basic level.
Types of Domain Constraints:
| Type | Description | Examples | SQL Implementation |
|---|---|---|---|
| Data Type | Basic type of value | Integer, String, Date, Boolean | Column data type declaration |
| Length | Size limits for text/binary | FirstName max 50 chars | VARCHAR(50) |
| Range | Numeric bounds | Quantity: 1-10000; Percentage: 0-100 | CHECK (Quantity BETWEEN 1 AND 10000) |
| Enumeration | Fixed list of valid values | Status: {Draft, Active, Archived} | CHECK (Status IN ('Draft', 'Active', 'Archived')) |
| Pattern | Format specification | PhoneNumber: (NNN) NNN-NNNN | CHECK (Phone LIKE '([0-9]{3}) [0-9]{3}-[0-9]{4}') |
| Nullability | Whether NULL is allowed | CustomerName cannot be null | NOT NULL |
| Default | Value when not specified | CreatedDate defaults to today | DEFAULT GETDATE() |
Constraint Extraction from Requirements:
Domain constraints emerge from various sources:
From Forms:
From Documents:
From Interviews:
From Existing Systems:
Enumerated domains (lists of valid values) often change over time. New statuses are added. Country codes change. Product categories evolve. Document not just today's valid values but also: Who can modify the list? What happens to existing data when values are removed? Consider lookup tables instead of hard-coded CHECK constraints.
Key constraints ensure that entity instances can be uniquely identified and that no two instances share the same identity.
Types of Key Constraints:
| Constraint Type | Description | Example |
|---|---|---|
| Primary Key | Main unique identifier for an entity; cannot be NULL | Customer.CustomerID is primary key |
| Unique | Alternative unique identifier; may be nullable | Customer.Email must be unique (when provided) |
| Composite Key | Combination of attributes that together are unique | (OrderID, LineNumber) is unique in OrderItem |
| Surrogate Key | System-generated unique identifier | Auto-increment ID, GUID |
| Natural Key | Business-meaningful unique identifier | ISBN for books, SSN for US persons |
Complex Uniqueness Constraints:
Uniqueness is not always global. Some constraints are conditional:
Scoped Uniqueness:
Implementation: Composite unique constraint on (ScopeKey, ValueColumn)
Temporal Uniqueness:
Implementation: Requires checking overlap of date ranges
Conditional Uniqueness:
Implementation: Partial indexes or CHECK + UNIQUE combination
Don't overlook case sensitivity in uniqueness constraints. Is 'john@email.com' the same as 'John@Email.com'? For email addresses, typically yes (case-insensitive). For product codes, often no (case-sensitive). Document the intended behavior explicitly.
Referential integrity constraints maintain the consistency of relationships between entities. They ensure that references always point to valid, existing records.
The Core Principle:
If entity A references entity B (foreign key relationship), then:
| Action | On Delete | On Update | Use Case |
|---|---|---|---|
| RESTRICT / NO ACTION | Prevent deletion if references exist | Prevent update if references exist | Protect critical data; force explicit handling |
| CASCADE | Delete referencing records | Update foreign key values | Dependent data that has no meaning without parent |
| SET NULL | Set foreign key to NULL | Set foreign key to NULL | Optional relationships; preserve orphaned data |
| SET DEFAULT | Set foreign key to default value | Set foreign key to default value | Reassign to default category/owner |
Referential Constraint Specification:
For each relationship, document:
RELATIONSHIP: Order references Customer
Foreign Key: Order.CustomerID → Customer.CustomerID
Mandatory: Yes (every order must have a customer)
On Delete of Customer:
- Action: RESTRICT
- Reason: Cannot delete customers with order history
- Alternative: Soft-delete customer, keep orders accessible
On Update of Customer.CustomerID:
- Action: CASCADE (if surrogate key changes are possible)
- Reason: Orders should follow customer identity
- Note: Surrogate keys should not change; this is precautionary
Business Rule:
- Orders can only be placed by Active customers
- When customer becomes Inactive, existing orders remain valid
Complex Referential Scenarios:
Self-Referential:
Multiple References:
Polymorphic References:
Orphaned records—data that should reference something but doesn't—are a major data quality problem. Referential integrity prevents future orphans, but existing databases may already have them. During requirements, document both the constraint (prevent new orphans) and any cleanup needs (handle existing orphans).
Semantic constraints encode business meaning and logic that goes beyond simple domain or referential rules. They ensure data makes sense in the context of the business.
Categories of Semantic Constraints:
Semantic Constraint Examples:
CONSTRAINT: Order Total Consistency
Rule: The sum of OrderItem.LineTotal for an Order must
equal Order.SubTotal + Order.Tax + Order.Shipping - Order.Discount
Enforcement: Trigger or application logic
(too complex for CHECK constraint)
Violation Action: Reject transaction; log discrepancy
---
CONSTRAINT: Manager Level Hierarchy
Rule: An employee's manager must have a higher Level than the employee
(Level 1 reports to Level 2+; Level 2 reports to Level 3+)
Exception: CEO (highest level) has no manager (ManagerID is NULL)
Enforcement: Trigger on INSERT and UPDATE of Employee
---
CONSTRAINT: Inventory Availability
Rule: Cannot ship more than available inventory
Shipment.Quantity ≤ Product.QuantityOnHand - Product.QuantityReserved
Enforcement: CHECK constraint with subquery (if supported)
or trigger with reservation system
Concurrency Note: Requires locking or optimistic concurrency control
| Element | Description |
|---|---|
| Constraint Name | Unique identifier for the constraint |
| Description | Plain English explanation of the rule |
| Formal Expression | Logical or mathematical formulation |
| Entities Involved | Which entities/tables are affected |
| Trigger Events | What operations activate the constraint (INSERT, UPDATE, DELETE) |
| Enforcement Level | Database constraint, trigger, or application code |
| Violation Response | Reject operation, warn, log, or other action |
| Exceptions | Conditions where the rule doesn't apply |
| Source | Where this requirement came from (interview, policy, regulation) |
Complex semantic constraints that span multiple tables or require aggregation are expensive to enforce. Every INSERT, UPDATE, or DELETE may require additional queries. Document the expected frequency of constraint-related operations and discuss performance implications during design.
Temporal constraints govern the time-related aspects of data—validity periods, sequences, durations, and date-based rules.
Types of Temporal Constraints:
| Category | Description | Examples |
|---|---|---|
| Sequence Constraints | Dates must occur in a specific order | StartDate < EndDate; HireDate < TerminationDate |
| Duration Constraints | Time spans must fall within limits | Contract duration: 1-5 years; Trial period: max 90 days |
| Currency Constraints | Rules about current vs. historical data | Only one active price per product at a time |
| Future/Past Constraints | Rules about dates relative to now | BirthDate must be in the past; ScheduledDate must be in the future |
| Calendar Constraints | Rules about specific dates/times | Appointments only on business days; No deliveries on holidays |
| Overlap Constraints | Prevent or require overlapping periods | Room bookings cannot overlap; Employee assignments must have no gaps |
Temporal Overlap Prevention:
A common requirement is preventing overlapping time periods. For example:
Non-Overlap Constraint:
For records with (EntityID, StartDate, EndDate), ensure no two records for the same EntityID have overlapping periods:
Two periods (S1, E1) and (S2, E2) overlap if: S1 < E2 AND S2 < E1
Implementation Approaches:
Temporal Validity Tracking:
Many entities track when facts are valid:
A bi-temporal database tracks both, enabling queries like:
For global systems, temporal constraints must account for timezones. 'Before 5:00 PM' depends on which timezone. Document whether times are stored in UTC, local time, or timezone-aware formats, and how timezone conversions affect constraint validation.
Cardinality constraints specify the minimum and maximum number of entity instances that can participate in a relationship.
Cardinality Notation:
Cardinality is expressed as (min, max) where:
| Cardinality | Meaning | Example |
|---|---|---|
| (0,1) : (0,1) | Optional one-to-one | Person optionally has Passport; Passport optionally has Person |
| (1,1) : (0,N) | Mandatory one, optional many | Each Order has exactly one Customer; Customer may have zero or more Orders |
| (0,N) : (0,N) | Optional many-to-many | Products may be in Categories; Categories may contain Products |
| (1,1) : (1,N) | Mandatory on both sides | Each OrderItem belongs to exactly one Order; Each Order has at least one OrderItem |
| (1,3) : (0,N) | Specific range | Each Project has 1-3 Managers; Managers may manage any number of Projects |
Enforcing Minimum Cardinality:
Maximum cardinality of 1 is straightforward (unique constraint). Minimum cardinality > 0 (mandatory participation) is more challenging:
For 'each X must have at least one Y':
Example Constraint:
CONSTRAINT: Order Must Have Items
Rule: Every Order must have at least one OrderItem
Problem: How to insert Order before OrderItems exist?
Solution 1: Deferred constraint
- Begin transaction
- Insert Order
- Insert OrderItems
- Commit (constraint checked here)
Solution 2: Business rule enforcement
- Application ensures Order + first Item created together
- Prevent deleting last OrderItem
- Prevent creating Order without Items
For each relationship, document both the inherent cardinality (can there be multiple?) and the business cardinality (should there be limits?). A one-to-many relationship might have a business rule like 'maximum 5 addresses per customer' even though technically unlimited addresses are possible.
Constraints are embedded throughout requirements sources. Systematic extraction ensures none are missed.
| Source | Look For | Constraint Example |
|---|---|---|
| Interview Notes | Words like 'must', 'cannot', 'always', 'never', 'only if', 'at least', 'no more than' | "Customers must have an email address" |
| Forms | Required field markers, input validation, dropdown values, format masks | Asterisk on field → NOT NULL constraint |
| Reports | Grouping assumptions, filter expectations, data relationships | Report groups by region → Region is required attribute |
| Policies | Rules, restrictions, approvals, limits | "Expenses over $500 require manager approval" |
| Regulations | Compliance mandates, retention rules, privacy requirements | "Medical records retained for 7 years minimum" |
| Error Messages | Current system rejection criteria | "Error: End date must be after start date" |
| Exception Procedures | What happens when rules are violated | "If inventory negative, create backorder" |
Constraint Extraction Techniques:
Trigger Word Analysis: Scan documents for constraint indicator words:
Negative Case Inquiry: During interviews, ask explicitly:
Edge Case Exploration:
Many constraints are assumed rather than stated. Users assume 'of course dates can't be in the future' or 'obviously you can't delete a customer with orders.' Make implicit constraints explicit by asking 'what if?' questions for every attribute and relationship.
Constraints must be documented in a structured format that enables clear communication, validation, and eventual implementation.
Example Constraint Documentation:
CONSTRAINT: BR-ORD-003
Name: Order Cannot Exceed Credit Limit
Category: Semantic Constraint
Description:
The total value of a customer's open orders (not yet paid)
cannot exceed their assigned credit limit.
Formal Expression:
FOR ALL Customer c:
SUM(Order.Total WHERE Order.CustomerID = c.CustomerID
AND Order.Status IN ('Pending', 'Confirmed', 'Shipped'))
≤ c.CreditLimit
Entities Involved:
- Customer (CreditLimit attribute)
- Order (Total, Status, CustomerID attributes)
Enforcement Level:
- Database trigger on Order INSERT and UPDATE
- Application pre-check before order submission
Violation Response:
- Reject order creation
- Return error message: "Order exceeds available credit.
Available credit: $X. Order total: $Y."
Exceptions:
- Customers with CreditLimit = NULL have no limit (premium customers)
- Manager override with documented approval (log override)
Source:
- Finance Policy FP-2023-04, Section 3.2
- Interview: CFO, 2024-01-15
Priority: Critical
Test Cases:
- Order within limit → Accept
- Order exactly at limit → Accept
- Order $0.01 over limit → Reject
- Customer with no limit → Always accept
- Manager override → Accept with logging
Every constraint should trace back to a source (regulation, policy, interview) and forward to implementation (database constraint, trigger, application code). This traceability ensures constraints aren't lost during design and enables impact analysis when business rules change.
Constraint identification completes the requirements gathering phase. With data requirements, functional requirements, and constraints, you have a comprehensive specification of what the database must store, what it must do, and what rules it must enforce.
Module Conclusion:
With this page, you have completed the Requirements Gathering module. You now understand how to:
These skills form the foundation for all subsequent database design activities. A well-gathered set of requirements prevents costly errors, reduces rework, and ensures the final database serves its intended purpose.
Congratulations! You have completed the Requirements Gathering module. You now possess the skills to systematically gather, document, and validate database requirements. The next modules in the Database Design Process will show you how to transform these requirements into conceptual, logical, and physical database designs.