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.

2. Need for Constraints

3. Types of Constraints in DBMS

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'))

5. Key Constraint

Key constraints ensure that a key attribute uniquely identifies each record.

STUDENT
-----------------
Roll_No (Primary Key)
Name
Marks

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

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

8. Constraint Violation Example

INSERT INTO ENROLL VALUES (999);

Error: Foreign key constraint fails
(No student with Roll_No 999)

9. Importance of Constraints

Practice Questions

  1. What are constraints in DBMS?
  2. Explain domain constraint with example.
  3. What is entity integrity constraint?
  4. Explain referential integrity constraint.
  5. Why constraints are important?

Practice Task

Design a table and apply: ✔ Primary Key ✔ Foreign Key ✔ CHECK constraint ✔ NOT NULL constraint