Integrity Constraints are rules applied to database tables to ensure the accuracy, consistency, and reliability of data. They prevent invalid or illogical data from being entered into the database.
✅ Purpose of Integrity Constraints:
🎯 Goal | 📖 Explanation |
|---|---|
✅ Data Accuracy | Prevents incorrect or contradictory data from being inserted |
🔄 Data Consistency | Maintains uniformity and logical correctness across related tables |
🚫 Avoid Invalid Data | Stops nulls or duplicates where they're not allowed |
🔗 Maintain Relationships | Ensures linked records exist (e.g., student must belong to a valid department) |
🔒 Enforce Business Rules | Applies real-world logic, like a product must have a positive price |
🛠️ Types of Integrity Constraints:
🔐 Constraint Type | 🌟 Ensures That... |
|---|---|
Primary Key | Each row is uniquely identifiable and not null |
Foreign Key | Data in one table must match existing data in another |
Unique | Column values are distinct (but can be null once) |
Not Null | No empty (null) values are allowed |
Check | Column values must satisfy a specific condition |
Default | Sets a default value if none is provided |
💡 Real-Life Analogy:
Imagine filling a college admission form:
You must provide your name →
NOT NULLYour ID must be unique →
PRIMARY KEYYou must select a valid course from a list →
FOREIGN KEYYour age must be >= 17 →
CHECK constraint