What are Relational Integrity Constraints?
Types of Relational Integrity Constraints.
- Entity Integrity Constraint.
- Referential Integrity Constraint.
- Domain Integrity Constraint.
- Key Constraints (Uniqueness).
1. Entity Integrity Constraint.
- Primary Key (PK) must be unique (no duplicate values).
- PK cannot be NULL (every row must have an identifier).
| Employee_ID (PK) | Name | Department |
|---|---|---|
| 101 | Alice | HR |
| 102 | Bob | IT |
| 103 | Charlie | Finance |
Every Employee_ID (PK) must be unique and non-NULL in the Employees table.
✅ Valid: Employee_ID 103 (Charlie) is unique and not empty
❌ Invalid:
- Duplicate ID (101 for two employees)
- NULL value in Employee_ID
2. Referential Integrity Constraint.
- Ensures only valid relationships exist (e.g., no "ghost" customers for orders).
- Use ON DELETE CASCADE to auto-delete child records when a parent is removed.
- Blocks insertions/updates that break table relationships.
| Customer_ID (PK) | Name |
|---|---|
| 1 | Alice |
| 2 | Bob |
| Order_ID | Customer_ID (FK) | Amount |
|---|---|---|
| 101 | 1 | 99.99 |
| 102 | 2 | 50.00 |
Every Customer_ID in the Orders table must exist in the Customers table's Customer_ID column.
✅ Valid: Order 101 links to Customer 1 (Alice)
❌ Invalid: An order with Customer_ID=3 would be rejected (no such customer exists)
3. Domain Integrity Constraint.
- Data Type Enforcement: Columns must contain values of a specific type (e.g., INT, VARCHAR, DATE).
- Value Range/Rules: Uses CHECK constraints to limit values (e.g., Age >= 18).
- Mandatory Fields: NOT NULL ensures critical columns are never empty.
- Default Values: DEFAULT provides fallback values if none are specified.
| Employee_ID (INT PRIMARY KEY) | Name (VARCHAR(50) NOT NULL) | Age (INT CHECK >=18 AND <=70) | Email (VARCHAR(100) UNIQUE) | Join_Date (DATE DEFAULT CURRENT_DATE) | Salary (DECIMAL(10,2) CHECK >0) |
|---|---|---|---|---|---|
| 101 | Alice | 30 | alice@example.com | 2023-10-05 | 5000.00 |
Each column enforces specific data rules:
✅ Valid Example: All values meet their constraints
❌ Violation Examples:
- NULL Name: (102, NULL, 25, 'bob@example.com', '2023-10-06', 4500.00)
- Age < 18: (103, 'Charlie', 17, 'charlie@example.com', '2023-10-07', 3000.00)
- Duplicate Email/Negative Salary: (104, 'Dave', 45, 'alice@example.com', '2023-10-08', -2000.00)
Key Constraints Shown:
- NOT NULL - Required fields
- CHECK - Value range validation
- UNIQUE - No duplicate emails
- DEFAULT - Automatic current date
4. Key Constraints (Uniqueness).
CREATE TABLE Employees ( Employee_ID INT PRIMARY KEY, -- Primary Key (Unique + NOT NULL) Email VARCHAR(100) UNIQUE NOT NULL, -- Unique Key (No duplicates) Passport_Number VARCHAR(20) UNIQUE -- Unique Key (Allows one NULL) );
| Employee_ID (PK) | Email (Unique) | Passport_Number (Unique) |
|---|---|---|
| 101 | alice@company.com | A12345678 |
| 102 | bob@company.com | B87654321 |
| 103 | charlie@company.com | NULL |
| 104 | dave@company.com | C55555555 |
Key Observations:
- The Employee_ID column is the primary key - all values are unique and non-null
- The Email column has a unique constraint - no duplicates allowed (and NOT NULL enforced)
- The Passport_Number column has a unique constraint, but allows one NULL value




%20Diagram.png)
%20Diagram.png)


