MySQL has many-to-many tables

Source: Internet
Author: User

Description

The article here is followed by the previous MySQL table one-to-many write.

Multiple-to-many tables

Can be understood as polygamy and polygamy.

Men's Watch:

Nid Name
1 Xxx
2 yyy
3 zzz

Women's Watch:

Nid Name
1 Aaa
2 Bbb
3 Ccc

To create a relationship between two tables, you can use a foreign key to bind each table by adding one column to each other, allowing them to constrain each other.

However, this is not ideal, after all, to modify the table.

In general, a single table is created, and the table has 2 data and 2 table IDs associated with each other. This table can be called a relational table.

Practice:
2 foreign keys are set on the relational table, respectively, associated with the corresponding male and female tables.

Nid man_id women_id
1 1 3
2 3 1
3 2 2

Description

Set the two foreign keys on the relational table so that man_id corresponds to the nid,women_id in the Women's table for the male table, so that the two tables are linked by setting up a table alone.

Examples of many-to-many understanding:

Each data in a man's table can correspond to multiple data in a woman's table, and each data in a woman's table can correspond to multiple data in a man's table.

MySQL has many-to-many tables

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.