Relational Database Design and Modeling for Software Engineers
Ask Author
Back to course home

0% completed

Vote For New Content
Relational Integrity Constraints
Table of Contents

Contents are not accessible

Contents are not accessible

Contents are not accessible

Contents are not accessible

Contents are not accessible

Relational integrity constraints are rules that ensure data accuracy, consistency, and reliability within a relational database. These constraints help maintain the quality of data by enforcing certain conditions on the data stored in tables. Integrity constraints prevent invalid or inconsistent data from being inserted into the database and are essential for maintaining a trustworthy database structure.

Types of Relational Integrity Constraints

Domain Constraint

A domain constraint defines the permissible values for an attribute. Each attribute in a table has a domain, or a specific range of values it can take, such as a data type (e.g., integer, text) and range restrictions.

  • Example: In a Student table, the CGPA attribute might have a domain constraint that limits values to numbers between 0.0 and 4.0.

Entity Integrity Constraint

The entity integrity constraint ensures that each table has a unique primary key, and that this primary key cannot contain null values. This constraint guarantees that every row in a table is uniquely identifiable, making it impossible for records to be duplicated or left undefined.

  • Example: In the Student table, Roll Number might be the primary key, and it cannot be null, ensuring every student has a unique identifier.

Referential Integrity Constraint

The referential integrity constraint maintains consistency between two related tables by ensuring that foreign key values in one table match primary key values in another table. This constraint prevents orphan records, where a foreign key references a non-existent primary key in the related table.

  • Example: In an Student table that tracks the student data, the Course ID (foreign key) must correspond to a valid Course ID (primary key) in the Course table. This ensures that all enrolled students exist in the Course table.
  1. Course Table (Primary Key: Course ID)

    • Contains information about available courses.
    Course IDCourse NameCredits
    C101Introduction to DB3
    C102Data Structures4
    C103Algorithms3
  2. Student Table (Foreign Key: Course ID)

    • Tracks student information and the course each student is enrolled in.
    Student IDNameCourse ID
    S001Alice SmithC101
    S002Bob JohnsonC102
    S003Carol WhiteC103

In this setup:

  • The Course ID in the Student table is a foreign key that references the Course ID in the Course table.
  • This constraint ensures that every Course ID in the Student table must match a valid Course ID in the Course table.

Let's insert a new record into the Student table having a course id C101. The record will be successfully inserted in the table as course id c101 exists in the course table. Here’s how the Student table will look:

Student IDNameCourse ID
S001Alice SmithC101
S002Bob JohnsonC102
S003Carol WhiteC103
S004David BrownC101

Key Constraint (Uniqueness Constraint)

A key constraint enforces that certain attributes in a table must be unique, meaning no duplicate values are allowed. This constraint is typically applied to primary keys and can also be used for candidate keys.

  • Example: In the Student table, Email might have a uniqueness constraint, ensuring that no two students have the same email address.

Check Constraint

A check constraint defines a specific condition that each record must satisfy before being inserted into the table. This constraint adds more granular control over attribute values by allowing custom conditions.

  • Example: In the Student table, a check constraint might enforce that the Age attribute be greater than or equal to 18, ensuring only students of a certain age can be added to the table.

Summary Table of Integrity Constraints

Constraint TypePurposeExample
Domain ConstraintEnsures attribute values fall within a defined rangeCGPA between 0.0 and 4.0
Entity IntegrityGuarantees each row has a unique identifierRoll Number as a non-null primary key
Referential IntegrityMaintains consistency between related tablesStudent ID in Enrollment matches Roll Number in Student
Key ConstraintEnsures certain attributes have unique valuesEmail is unique in Student table
Check ConstraintValidates custom conditions on attribute valuesAge >= 18 in Student table

.....

.....

.....

Like the course? Get enrolled and start learning!

Table of Contents

Contents are not accessible

Contents are not accessible

Contents are not accessible

Contents are not accessible

Contents are not accessible