Referential Integrity in DBMS With Example

Referential Integrity in DBMS With Example

01 Oct 2024
Intermediate
593 Views
12 min read

Referential Integrity Constraint in DBMS

Referential integrity constraint is the key idea in the database management system that guarantees the correctness and consistency of data across linked tables. Data inserting, updating, removing, and other operations are carried out in the table in a way that guarantees data integrity is not jeopardized by integrity constraints in database management systems.

In the DBMS tutorial, we will review all the key ideas surrounding integrity constraints in DBMS and gain an understanding of them. These topics include the various kinds of integrity constraints, primary keys, foreign keys, table relationships, guaranteeing referential integrity, establishing constraints for foreign keys, tools and techniques utilized in integrity constraints, and much more. Let us examine the various integrity constraints.

Types of Integrity Constraints

  • Domain Constraints
  • Not-Null Constraints
  • Entity integrity Constraints
  • Key Constraints
  • Primary Key Constraints
  • Referential integrity constraints

Diagram of Types of Integrity Constraints

Note: In this article, we will only learn about Referential Integrity Constraints.

Read More:

What are Referential Integrity Constraints?

Referential Integrity in DBMS ensures that the relationship between tables remains logical. When a table has a foreign key that references the primary key of another table, referential integrity ensures that this foreign key value always refers to an existing, valid row in the referenced table.

Key Aspects of Referential Integrity

Here are the crucial aspects of referential integrity constraints:

  • Primary Keys
  • Foreign Key
  • Relationship between Tables

  • Primary Keys

The primary keys in DBMS are columns or combinations of columns that unequally identify each row of the data. The primary key provides some features.

  • Uniqueness: Each record must have a unique primary key value.
  • Not Null: A primary key cannot contain null values.
  • Minimal: The primary key should be as small as possible for performance reasons.

Example

In this example, we are creating a table named Student with the syntax:

CREATE TABLE Student (Student_Id int Primary Key, Student_Name varchar(25) );

Student Table

Student_IdStudent_Name
101Rakesh
102Ayansh
103Ankita
104Anusha

In the above example, Student_Id is the primary key.

  • Foreign Keys

The foreign key refers to a column or combination of columns that is used to establish and enforce a link between the data in two tables to control the data that can be stored in the foreign key table.

Example

In this example, we are creating a Course table that is referenced by the Student table. The syntax for that table:

CREATE TABLE Course ( Student_Id int references Student, Student_Section int, Student_Course varchar(10) );

Course Table

Student_IdStudent_SectionStudent_Course
1011MCA
1022BCA
1032BCA
1041MCA

In this example, Student_Id becomes a foreign key after being referenced by the Student table.

  • Relationship between tables

Referential integrity is the most important concept in database management. That ensures the relationship between tables remains consistent. It is managed by a foreign key that links rows in one table to rows in another. Here are the relationships between tables in referential integrity;

1. One-to-One (1-1) Relationship: A one-to-one relationship occurs when a single record in one table correlates to exactly one record in another table.

Example

    Table A ( Student )

    Student_IdStudent_Name
    101Ankita
    102Aman
    103Anurag

    Table B ( Course )

    Student_IdCourseAddress
    101BCAMumbai
    102BCADelhi
    103MCAKolkata

In the Student table, the primary key is the Student_Id, and in the Course table, the Student_Id is the foreign key and primary key, both of which define the One-to-One relationship in the table.

2. One-to-Many (1-M) Relationship: A one-to-many relationship occurs when a single record in one table can correlate to multiple records in another table, but a record in the second table can only correspond to one record in the first.

Example

Table A (Student)

Student_IdStudent_Name
101Aman
102Ankita
103Anurag

Table B (RollNumber)

Student_IdCourseStudent_Roll
101BCA01
102BCA02
103BCA03

In the given example, The foreign key is the Student_Id, and Student_Roll is the primary key. This key defines the possibility that multiple students can pursue the same course, showing One-to-Many relationships.

3. Many-to-Many (M-M) Relationship: A many-to-many relationship occurs when multiple records in one table are correlated with multiple records in another table. This kind of record can't be directly implemented using only two tables and requires a third table called a junction table or join table.

Example

Student Table

Student_IdStudent_Name
101Aman
102Ankita
103Anurag

Course Table

Course_IdCourse_Name
1001Java
1002Python
1003C++

Enrollment Table

Student_IdCourse_IdCourse_name
1011001Java
1011003C++
1021002Python
1021001Java
1031002Python

In this example, we saw that multiple students could enroll in multiple courses, which shows the many-to-many relationships.

Guaranteeing Referential Integrity

Guaranteeing referential integrity in a database is most important for managing the accuracy and consistency of data in related tables. Here are the key steps and methods for ensuring referential integrity;

Creating Foreign Key Constraints

We create foreign key constraints to link tables. This means that every foreign key value must match an existing primary key value in the referenced table.

Use Cascading Actions

Cascading actions in a DBMS are rules applied to maintain referential integrity when a primary key in the parent table is updated or deleted.

1. ON DELETE CASCADE

It refers to an operation in which when a row in the parent table is deleted, all related rows in the child table are also deleted.

2. ON UPDATE CASCADE

It refers to an operation in which when we update the primary key value in the parent table, all corresponding foreign key values in the child table are also updated.

Referential Integrity Constraints

Referential integrity constraints refer to the validity of the relationship between data in multiple tables and the fact that the data is not lost or orphaned.

Advantages of Managing Referential Integrity

There are several benefits of maintaining the referential integrity that are depicted below;

Data Accuracy

  • Implement checks and validation rules to ensure data entered into the database meets the standard.
  • Continuously run scripts to identify and correct errors in the database.

Data Consistency

  • Ensure the database transactions are atomic, meaning they are fully completed or fully rolled back, preventing partial updates.
  • Structure the database to remove redundancy and ensure logical data storage, reducing the risk of anomalies.

Avoiding Orphan Records

  • Using foreign key constraints to ensure that the child table records reference valid primary key records in the parent table.
  • Using triggers to validate foreign key relationships before inserting or updating records and handling related child records when a parent record is deleted.

Common Issues and Challenges

Managing referential integrity in DBMS can be challenging due to various issues and complexity. Here are some common problems;

  • Unintentional data loss due to cascading deletes and updates.
  • Difficulty in handling circular references between tables.
  • Complications in data insertions, updates, and deletes.
  • Impact on performance due to enforcing referential integrity constraints.
  • Risk of orphan records if constraints are not properly enforced.

Best Practices for Managing Referential Integrity

The various best practices for maintaining referential integrity are as follows;

  • Design the database schema carefully
  • Use appropriate constraints
  • Implement data validation
  • Regularly integrity checks
  • Use triggers for complex integrity rules

Tools and Techniques Used in Referential Integrity

There are several tools and technologies used in referential integrity:

  • Database Management System (DBMS)
  • Structured Query Language(SQL)
  • Database Design Tools(Visual Tools)
  • ORM Framework
  • Data Quality Tools
Conclusion

In this article, we have discussed all the important aspects of Referential integrity, which will definitely help you clarify your concept and understand its uses in maintaining consistency and accuracy in data management.

ScholarHat provides various Training and Certification Courses to help you in your end-to-end product development:

FAQs

A foreign key constraint violation typically results in an error or exception, preventing the operation from completing.

Preventing referential integrity violations is generally handled by;
  • Database-Specific Checks
  • SQL Queries
  • Data Quality Tools
  • Database Monitoring and Auditing

Yes, referential integrity constraints impact database performance, but the effect is generally minimal and often outweighed by the benefits of data integrity.

The key difference is 
  • ON DELETE CASCADE- When a row in the parent table is deleted, all related rows in the child table are also deleted.
  • ON UPDATE CASCADE- When a primary key value in the parent table is updated, all corresponding foreign key values in the child table are also updated.

Here are some strategies to handle referential integrity in a distributed environment,
  • Distributed transactions
  • Eventual consistency
  • Application level enforcement
  • Data replications
Share Article
About Author
Shailendra Chauhan (Microsoft MVP, Founder & CEO at Scholarhat by DotNetTricks)

Shailendra Chauhan is the Founder and CEO at ScholarHat by DotNetTricks which is a brand when it comes to e-Learning. He provides training and consultation over an array of technologies like Cloud, .NET, Angular, React, Node, Microservices, Containers and Mobile Apps development. He has been awarded Microsoft MVP 9th time in a row (2016-2024). He has changed many lives with his writings and unique training programs. He has a number of most sought-after books to his name which has helped job aspirants in cracking tough interviews with ease.
Accept cookies & close this