In case of FOREIGN KEY constraint, what kind of relationship is there between MySQL parent and child tables?


The relationship between parent and child table is One-to-Many relationship. 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. The relationship is one-to—many because a customer can have more than one order. 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)

From the above result set it is clear that one customer can have many orders because customer having id = 1 is having two orders and customer having id = 2 is having two orders as well.

Vikyath Ram
Vikyath Ram

A born rival

Updated on: 28-Jan-2020

293 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements