SQL Query to Demonstrate Deletion Anomaly in Referential Integrity in a Table
Last Updated :
20 Jun, 2022
In SQL, there exists a concept of referential integrity. This means that a foreign key can take reference from the primary key of another table. There exists basically 3 anomalies in this concept. Here, we discuss about Deletion Anomaly. This means that if an entry is present in the foreign key column of the target table, then that entry cannot be deleted from the primary key column of the base table. This is illustrated below. For this article, we will be using the Microsoft SQL Server as our database.
Step 1: Create a Database. For this use the below command to create a database named GeeksForGeeks.
Query:
CREATE DATABASE GeeksForGeeks
Output:

Step 2: Use the GeeksForGeeks database. For this use the below command.
Query:
USE GeeksForGeeks
Output:

Step 3: Create a table STUDENT_INFO inside the database GeeksForGeeks. This table has 3 columns namely ROLL_NO, STUDENT_NAME and BRANCH containing the roll number, name and branch of various students.
Query:
CREATE TABLE STUDENT_INFO(
ROLL_NO INT PRIMARY KEY,
STUDENT_NAME VARCHAR(10),
BRANCH VARCHAR(5)
);
Output:

Step 4: Describe the structure of the table STUDENT_INFO.
Query:
EXEC SP_COLUMNS STUDENT_INFO;
Output:

Step 5: Create a table STUDENT_MARKS inside the database GeeksForGeeks. This table has 3 columns namely ROLL_NO, SUBJECT and MARKS containing the roll number, subject and marks of various students. Here the ROLL_NO column acts as a foreign key referencing from the STUDENT_INFO table's ROLL_NO column, which is the primary key for the STUDENT_INFO table.
Query:
CREATE TABLE STUDENT_MARKS(
ROLL_NO INT REFERENCES STUDENT_INFO(ROLL_NO),
SUNJECT VARCHAR(10),
MARKS INT
);
Output:

Step 6: Describe the structure of the table STUDENT_MARKS.
Query:
EXEC SP_COLUMNS STUDENT_MARKS;
Output:

Step 7: Insert 3 rows into the STUDENT_INFO table.
Query:
INSERT INTO STUDENT_INFO VALUES(1,'JIM','CSE');
INSERT INTO STUDENT_INFO VALUES(2,'TIM','ELE');
INSERT INTO STUDENT_INFO VALUES(3,'PAM','ECE');
Output:

Step 8: Display all the rows of the STUDENT_INFO table.
Query:
SELECT * FROM STUDENT_INFO;
Output:

Step 9: Insert 2 rows into the STUDENT_MARKS table.
Query:
INSERT INTO STUDENT_MARKS VALUES(1,'CPP',98);
INSERT INTO STUDENT_MARKS VALUES(2,'DBMS',89);
Output:

Step 10: Display all the rows of the STUDENT_MARKS table.
Query:
SELECT * FROM STUDENT_MARKS;
Output:

Step 11: Delete 1 row from the STUDENT_INFO table where ROLL_NO is 3. The entry for the ROLL_NO column here i.e. 3 does not exist in the target table i.e. STUDENT_MARKS.
Query:
DELETE FROM STUDENT_INFO WHERE ROLL_NO=3;
Note: The deletion is successful as the deletion anomaly is not being violated since the roll number 3 is absent in the target table STUDENT_MARKS.
Output:

Step 12: Delete 1 row from the STUDENT_INFO table where ROLL_NO is 1. The entry for the ROLL_NO column here i.e. 1 exists in the target table i.e. STUDENT_MARKS.
Query:
DELETE FROM STUDENT_INFO WHERE ROLL_NO=1;
Note - The deletion is not successful and a referential integrity error is thrown as the deletion anomaly is being violated since roll number 1 is present in the target table STUDENT_MARKS.
Output:

Step 13: Delete 1 row from the STUDENT_MARKS table where ROLL_NO is 1.
Query:
DELETE FROM STUDENT_INFO WHERE ROLL_NO=1;
Output:

Step 14: Delete 1 row from the STUDENT_INFO table where ROLL_NO is 1. The entry for the ROLL_NO column here i.e. 1 does not exist in the target table i.e. STUDENT_MARKS.
Query:
DELETE FROM STUDENT_INFO WHERE ROLL_NO=1;
Note - The deletion is successful as the deletion anomaly is not being violated since the roll number 1 is absent in the target table STUDENT_MARKS.
Output:

Step 15: Display all the final rows of the STUDENT_INFO table.
Query:
SELECT * FROM STUDENT_INFO;
Output:

Step 16: Display all the final rows of the STUDENT_MARKS table.
Query:
SELECT * FROM STUDENT_MARKS;
Output:

Similar Reads
SQL Query to Demonstrate Updation Anomaly in Referential Integrity in a Table
In SQL, there exists a concept of referential integrity. This means that a foreign key can take reference from the primary key of another table. There exist basically 3 anomalies in this concept. Here, we discuss about Updation Anomaly. This means that if an entry is present in the foreign key colum
3 min read
SQL Query to Demonstrate Addition Anomaly in Referential Integrity in a Table
In SQL, there exists a concept of referential integrity. This means that a foreign key can take reference from the primary key of another table. There exists basically 3 anomalies in this concept. Here, we discuss about Addition/Insertion Anomaly. This means that if an entry is absent in the primary
3 min read
Cascading Referential Integrity Constraints in SQL Server Management Studio
In the Microsoft SQL server if we want to delete any record or column from one table but that record or column is a foreign key for another table then we will get the error to solve this problem we use Cascading referential integrity constraint. It allows the actions that SQL Server should take when
4 min read
How to Delete a Row by Reference in data.table in R?
In R Language the data.table package is highly efficient for data manipulation, especially for large datasets. One of its powerful features is the ability to modify data by reference, which avoids copying the entire dataset and thus improves performance. This article will guide you through the proce
3 min read
How to List all Foreign Keys Referencing a Given Table in SQL Server?
SQL Server is a Relational Database Management System(RDBMS) that allows users to create and manage databases efficiently. In SQL Server, understanding the table's relationship is very important for database design and maintenance. Foreign keys play an important role in establishing relations betwee
5 min read
SQL Query to DELETE Certain Rows Based on Comparisons with Other Tables
In this article, we will see, how to delete certain rows based on comparisons with other tables. We can perform the function by using a subquery in place of the condition in WHERE Clause. A subquery is a query that appears inside another query. It can also be called a nested query. Syntax: SELECT *
2 min read
SQL Query to Display All the Existing Constraints on a Table
In SQL, we sometimes need to display all the currently existing constraints on a table. The whole process for doing the same is demonstrated below. For this article, we will be using the Microsoft SQL Server as our database. Step 1: Create a Database. For this use the below command to create a datab
3 min read
Difference between Entity constraints, Referential constraints and Semantic constraints
Constraints are used in relational databases to follow the standards of the database and guarantee that the data is valid. Users are usually confused about various kinds of constraints most of which include entity constraints, referential constraints and semantic constraints. In this article, We wil
6 min read
How can you deal with duplicate data points in an SQL query?
Dealing with duplicate data points in SQL is a common challenge faced by database administrators, developers, and data analysts. Duplicate records can lead to inaccurate data analysis, increased storage costs, and degraded performance. This article will explore various methods to identify, handle, a
7 min read
SQL Server - Find Records From One Table Which Don't Exist in Another
When working with databases, it is often necessary to compare data between tables to find records that exist in one table but not in another. In SQL Server, this can be achieved using various methods. In this article, we will explore two common approaches to finding records from one table that don't
3 min read