Constraints in DBMS
Constraints are rules applied to database tables to ensure the accuracy,
consistency, and integrity of data stored in the database.
They restrict the type of data that can be inserted into a table.
1. What are Constraints?
Constraints are conditions enforced on columns or tables to maintain data validity and reliability in a database.
- Prevent invalid data entry
- Maintain data consistency
- Ensure data integrity
2. Need for Constraints
- To avoid duplicate data
- To enforce business rules
- To maintain relationships between tables
- To protect data from errors
3. Types of Constraints in DBMS
- Domain Constraint
- Key Constraint
- Entity Integrity Constraint
- Referential Integrity Constraint
4. Domain Constraint
A domain constraint specifies the permissible values for a given attribute.
Age INT CHECK (Age >= 18)
Gender CHAR(1) CHECK (Gender IN ('M','F'))
- Restricts data type and range
- Improves data accuracy
5. Key Constraint
Key constraints ensure that a key attribute uniquely identifies each record.
STUDENT ----------------- Roll_No (Primary Key) Name Marks
- No duplicate key values allowed
- Ensures uniqueness
6. Entity Integrity Constraint
Entity integrity ensures that the primary key of a table cannot have NULL values.
PRIMARY KEY (Roll_No) → Roll_No cannot be NULL
- Each record must be uniquely identifiable
7. Referential Integrity Constraint
Referential integrity maintains consistency between related tables. A foreign key must match a primary key value in another table.
STUDENT (Roll_No) ← Foreign Key ENROLL (Roll_No) Foreign Key value must exist in STUDENT table
- Maintains table relationships
- Prevents orphan records
8. Constraint Violation Example
INSERT INTO ENROLL VALUES (999); Error: Foreign key constraint fails (No student with Roll_No 999)
9. Importance of Constraints
- Improves data quality
- Ensures database reliability
- Enforces business rules
- Reduces chances of invalid data
Practice Questions
- What are constraints in DBMS?
- Explain domain constraint with example.
- What is entity integrity constraint?
- Explain referential integrity constraint.
- Why constraints are important?
Practice Task
Design a table and apply:
✔ Primary Key
✔ Foreign Key
✔ CHECK constraint
✔ NOT NULL constraint