Select*fromCustomer;+----+---------+|id|name |+----+- --------+|1 |Gaurav ||2&"/> Select*fromCustomer;+----+---------+|id|name |+----+- --------+|1 |Gaurav ||2&">

Home  >  Article  >  Database  >  If there is a FOREIGN KEY constraint, what kind of relationship exists between the MySQL parent and child tables?

If there is a FOREIGN KEY constraint, what kind of relationship exists between the MySQL parent and child tables?

WBOY
WBOYforward
2023-09-16 15:41:071354browse

如果存在 FOREIGN KEY 约束,MySQL 父表和子表之间存在什么样的关系?

The relationship between the parent table and the child table is a one-to-many relationship. It can be understood using the examples of the two tables "customer" and "orders". Here, "customer" is the parent table and "orders" is the child table. This relationship is one-to-many because a customer can have multiple orders. It can be demonstrated by inserting values ​​in two tables as shown below -

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 multiple orders as the customer with id = 1 has two orders, the customer with id = 2 also has two orders.

The above is the detailed content of If there is a FOREIGN KEY constraint, what kind of relationship exists between the MySQL parent and child tables?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete