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.
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.
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.
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.
Course Table (Primary Key: Course ID
)
Course ID | Course Name | Credits |
---|---|---|
C101 | Introduction to DB | 3 |
C102 | Data Structures | 4 |
C103 | Algorithms | 3 |
Student Table (Foreign Key: Course ID
)
Student ID | Name | Course ID |
---|---|---|
S001 | Alice Smith | C101 |
S002 | Bob Johnson | C102 |
S003 | Carol White | C103 |
In this setup:
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 ID | Name | Course ID |
---|---|---|
S001 | Alice Smith | C101 |
S002 | Bob Johnson | C102 |
S003 | Carol White | C103 |
S004 | David Brown | C101 |
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.
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.
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 |
.....
.....
.....