
- 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
When are two tables connected with MySQL FOREIGN KEY then how can we say that the integrity of data is maintained in child table?
Actually, foreign keys enforce referential integrity that helps us to maintain the consistency and integrity of the data automatically. 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 create an order for a non-existent customer. It can be demonstrated by inserting the values in both the tables 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 insert the value in the ‘orders’ table for a non-existing customer (id 10 is not existing in ‘customer’ table) then MySQL throws an error as follows due to the failure of foreign key constraint.
mysql> insert into orders values(400, 'Notebook',10); ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails (`query`.`orders`, CONSTRAINT `orders_ibfk_1` FOREIGN KEY (`id`) REFERENCES `customer` (`id`))
- Related Articles
- How can we compare data in two MySQL tables?
- Foreign key referencing of two DB2 tables
- What are the different ways to maintain data integrity in child table when the record is deleted in parent table?
- How can we say that in MySQL, AUTO_INCREMENT is taking precedence over PRIMARY KEY?
- In case of FOREIGN KEY constraint, what kind of relationship is there between MySQL parent and child tables?
- How can we add a FOREIGN KEY constraint to the field of an existing MySQL table?
- How can we remove FOREIGN KEY constraint from a column of an existing MySQL table?
- How can we add FOREIGN KEY constraints to more than one fields of a MySQL table?
- What do you mean by FOREIGN KEY and how can we use it in MySQL table?
- How to use Primary Key Constraints and Foreign Key Constraints to enforce database integrity in Oracle?
- How can we assign FOREIGN KEY constraint on multiple columns?
- How can we see MySQL temporary tables in the list of tables?
- How to refer primary key as Foreign to various table in MySQL?
- When do we say that work is done ?
- How can we upload data into MySQL tables by using mysqlimport?

Advertisements