
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
What happens if I will delete a row from MySQL parent table?
While deleting the row from the parent table, if the data of that row is used in the child table then MySQL will throw an error because of the failure of FOREIGN KEY constraint. It can be understood with the example of two tables named ‘customer’ and ‘orders’. Here, ‘customer’ is the parent table and ‘orders’ is the child table. We cannot delete a row, that is used in child table ‘orders’, from the ‘customer’ table. It can be demonstrated by deleting the values from the parent table as follows −
mysql> Select * from Customer; +----+--------+ | id | name | +----+--------+ | 1 | Gaurav | | 2 | Raman | | 3 | Harshit| | 4 | Aarav | +----+--------+ 4 rows in set (0.00 sec) mysql> Select * from orders; +----------+----------+------+ | order_id | product | id | +----------+----------+------+ | 100 | Notebook | 1 | | 110 | Pen | 1 | | 120 | Book | 2 | | 130 | Charts | 2 | +----------+----------+------+ 4 rows in set (0.00 sec)
Now, suppose if we will try to delete the row having id = 1 or id =2 (because of both the rows used in child table) from parent table ‘customer’, then MySQL throws an error as follows due to the failure of foreign key constraint.
mysql> Delete from customer where id = 1; ERROR 1451 (23000): Cannot delete or update a parent row: a foreign key constraint fails (`query`.`orders`, CONSTRAINT `orders_ibfk_1` FOREIGN KEY (`id`)REFERENCES `customer` (`id`)) mysql> Delete from customer where id = 2; ERROR 1451 (23000): Cannot delete or update a parent row: a foreign key constraint fails (`query`.`orders`, CONSTRAINT `orders_ibfk_1` FOREIGN KEY (`id`)REFERENCES `customer` (`id`))
- Related Articles
- Does deleting row from view delete row from base table in MySQL?
- How can we delete a single row from a MySQL table?
- How to delete a row from a table using jQuery?
- What happens if I will use integer values as arguments of MySQL LOCATE() function?
- Delete multiple entries from a MySQL table
- Display random row from a MySQL table
- How can I delete MySQL temporary table?
- How can I create a stored procedure to delete values from a MySQL table?
- What happens if I will try to drop PRIMARY KEY constraint from the AUTO_INCREMENT column?
- Delete all records from a table in MySQL?
- Delete all the records from a MySQL table?
- What happens if I will assign a value to a MySQL user variable using a statement that returns multiple rows?
- MySQL query to delete a row if two columns are equal
- How to delete duplicates and leave one row in a table in MySQL?
- How to delete a column from a table in MySQL?

Advertisements