Home > Database > Mysql Tutorial > body text

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

WBOY
Release: 2023-09-16 15:41:07
forward
1320 people have browsed it

如果存在 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)
Copy after login

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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!