Constraints are rules imposed upon certain elements in a database to preserve
data integrity[1]. Entity and attribute constraints are rules imposed upon entities and attributes.
Several constraints were mentioned in this module, and are summarized below.
Constraints in a database are rules that enforce data integrity by ensuring that the data entered into the database remains accurate, consistent, and reliable. They prevent invalid, incomplete, or inconsistent data from being stored, thus preserving the quality of information in the database.
Here’s how different types of constraints help maintain data integrity:
-
Primary Key Constraint (PK) – Ensures Uniqueness and Entity Integrity
- The Primary Key uniquely identifies each record in a table.
- It ensures that no two rows have the same key value, preventing duplicate records.
- It also enforces entity integrity, meaning that every record must be uniquely identifiable.
-
Foreign Key Constraint (FK) – Enforces Referential Integrity
- A Foreign Key establishes a relationship between two tables, linking a column in one table to a Primary Key in another.
- It ensures that referenced values exist in the parent table, preventing orphan records and maintaining referential integrity.
- It enforces cascading operations like ON DELETE CASCADE to maintain consistency when related records are removed.
-
Unique Constraint – Prevents Duplicate Values
- Ensures that all values in a specific column (or a combination of columns) are distinct.
- Unlike a Primary Key, a table can have multiple Unique Constraints, and they can allow NULL values (though usually only one per column).
-
Not Null Constraint – Ensures Required Data is Present
- Prevents a column from having NULL values, ensuring that essential data fields always contain valid information.
-
Check Constraint – Enforces Domain Integrity
- Defines a condition that values in a column must satisfy.
- Example:
CHECK (age >= 18)
ensures only valid age values are inserted.
- Maintains domain integrity by restricting values to a predefined set.
-
Default Constraint – Provides Default Values
- Assigns a default value to a column when no explicit value is provided.
- Ensures consistency in cases where a value is not specified by the user.
How These Constraints Preserve Data Integrity
- Prevention of Duplicate Data: Primary Key and Unique Constraints ensure that records remain unique and avoid redundancy.
- Enforcement of Valid Relationships: Foreign Key Constraints maintain referential integrity, preventing broken relationships in relational databases.
- Data Completeness: Not Null Constraints ensure that mandatory fields are always filled.
- Controlled Data Values: Check Constraints restrict column values to valid ranges or conditions.
- Consistency Across Transactions: Constraints work together to prevent anomalies, ensuring ACID (Atomicity, Consistency, Isolation, Durability) compliance in transactional databases.
The next lesson concludes the module.
[1] Data integrity: A term used to describe the quality (in terms of accuracy, consistency, and validity) of data in a database, in the sense that values required to enforce data relationships actually exist.
Problems with data integrity occur when a value in one table that is supposed to relate to a value in another cannot because the second value either has been deleted or was never entered.