Categories: MySQL

Referential integrity

Share

It is relational database concept in which multiple tables shares consistent relationship based on the data stored on tables.

For example, a school X has 2 tables, a student table, and a student fee table. In the student table we have 2 columns – the student ID and the student name. In the student fee table, we have 2 columns – the student ID and the fee for the given ID.

Now, suppose we want to remove a student because he no longer study at school X. So we need to remove that student from student table. As the student ID from student table exists in student fee table then from that table we need to delete too manually. That’s a big deal if there are too many table attached.

By referential integrity we can solve this problem. Lets define the student ID column in the student table to be our primary key. Then define the student ID column in the student fee table to be a foreign key that points to a primary key that is the student ID column in the student table.

Once we define our foreign to primary key relationship, we need to add ‘constraint’ on student fee table i.e. ‘cascading delete’. Whenever an student is removed from the student table, any data that student has in the student fee table would automatically be removed from the student fee table.

Referential integrity example:

ALTER TABLE `tbl_1` ADD CONSTRAINT `tbl_std_constr` FOREIGN KEY (`student_id`) REFERENCES `tbl_2` (`student_id`) ON DELETE CASCADE ON UPDATE CASCADE;

Recent Posts

The Rise of NFTs: Exploring the Impact of Non-Fungible Tokens on the Digital Economy

NFTs, or Non-Fungible Tokens, are revolutionizing the digital economy. These unique digital assets, authenticated through…

11 hours ago

Unveiling the Truth: Is the Spread of Sinus Infections a Myth or Reality?

Sinus infections, impacting approximately 31 million Americans each year, represent a significant health concern stemming…

2 days ago

Best Exercises To Reduce Weight & Keep You Stronger & Fitter!

No doubt that balanced weight is the key to wellness. So, when it comes to…

2 days ago

ARTIFICIAL INTELLIGENCE: Advantages And Disadvantages? Everything You Need to Know

Pros And Cons Of AI: Artificial Intelligence directly translates to conceptualizing and building machines that…

2 days ago

Four common mistakes when picking an internet provider

We all live on the internet; we use it for everything. Thus, when it comes…

2 days ago

What Is Commonly Misdiagnosed as Pink Eye: Understanding Eye Conditions and Their Symptoms

Introduction: Pink eye, or conjunctivitis, is a common eye condition characterized by redness and inflammation…

2 days ago