0% completed
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.
-
Course Table (Primary Key:
Course ID
)- Contains information about available courses.
Course ID Course Name Credits C101 Introduction to DB 3 C102 Data Structures 4 C103 Algorithms 3 -
Student Table (Foreign Key:
Course ID
)- Tracks student information and the course each student is enrolled in.
Student ID Name Course ID S001 Alice Smith C101 S002 Bob Johnson C102 S003 Carol White C103
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 validCourse 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 ID | Name | Course ID |
---|---|---|
S001 | Alice Smith | C101 |
S002 | Bob Johnson | C102 |
S003 | Carol White | C103 |
S004 | David Brown | C101 |
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 Type | Purpose | Example |
---|---|---|
Domain Constraint | Ensures attribute values fall within a defined range | CGPA between 0.0 and 4.0 |
Entity Integrity | Guarantees each row has a unique identifier | Roll Number as a non-null primary key |
Referential Integrity | Maintains consistency between related tables | Student ID in Enrollment matches Roll Number in Student |
Key Constraint | Ensures certain attributes have unique values | Email is unique in Student table |
Check Constraint | Validates custom conditions on attribute values | Age >= 18 in Student table |
.....
.....
.....
Table of Contents
Contents are not accessible
Contents are not accessible
Contents are not accessible
Contents are not accessible
Contents are not accessible