Chapter :- 3 (English)
Topic Wise Chapter (English)
2. Relational Database Constraints
Relational Database Constraints
Constraints in a relational database are rules applied to table columns to ensure data accuracy, integrity, and reliability. They prevent invalid data from being inserted into the database.
Types of Relational Database Constraints
1. PRIMARY KEY Constraint
· Uniquely identifies each row in a table.
· Cannot be NULL.
Advantages
· Ensures each record is unique.
· Speeds up searching and indexing.
Disadvantages
· Requires careful design to avoid complex composite keys.
· Changing a primary key can be difficult.
2. FOREIGN KEY Constraint
· Ensures relationship between two tables.
· Enforces referential integrity.
Advantages
· Prevents orphan records.
· Maintains consistency across tables.
Disadvantages
· Can make deletion or updating of records difficult due to dependency.
· Slows down operations if many relationships exist.
3. UNIQUE Constraint
· Ensures all values in a column are unique.
· Allows only one NULL (varies by DBMS).
Advantages
· Prevents duplicate entries for important fields (e.g., email).
· Helps maintain data accuracy without needing a primary key.
Disadvantages
· May slow down insert operations due to uniqueness checks.
· Not suitable for fields that may need duplicates.
4. NOT NULL Constraint
· Ensures a column cannot store NULL values.
Advantages
· Guarantees required fields are always filled.
· Prevents accidental missing data.
Disadvantages
· Reduces flexibility if NULL values might be meaningful.
· Poorly applied NOT NULL constraints can result in dummy or invalid values.
5. CHECK Constraint
· Validates data based on a logical expression.
Example: age > 18
Advantages
· Enforces complex business rules.
· Prevents invalid data that meets the datatype but fails rules.
Disadvantages
· Poorly designed check constraints can cause errors.
· Complex checks may add performance overhead.
6. DEFAULT Constraint
· Sets a default value for a column when no value is provided.
Advantages
· Eliminates need to manually insert common values.
· Ensures fields are never left empty.
Disadvantages
· Might unintentionally fill values the user never intended.
· Overuse may hide missing data patterns.
ü Overall Advantages of Constraints
· Improve data integrity.
· Prevent invalid or inconsistent data.
· Enforce business rules automatically.
· Reduce the need for application-level validations.
· Maintain data consistency across related tables.
ü Overall Disadvantages of Constraints
· Can make database design more complex.
· May reduce performance for large-scale insert/update operations.
· Hard-to-change constraints may slow down development.
· Overuse can make database structures rigid and less adaptable.