简体   繁体   中英

Simple many-to-many relationships

Apologies, I haven't slept, so just want to clarify something in my head.

A team can have many users, but a user can be a member of several teams.

Is this a many-to-many relationship? If so, do I need a third table to join them in MySQL?

Yes. And you almost certainly need to have a third 'relationship' table.

The easiest option is to have a relationship table that indicates users on teams.

Teams == Rel === Users

To query for users on a team, search rel for all usersIds for a teamId and vice versa to check for teams that a user belongs to.

Many to many relationships

Many to many relationships exist and you're right to point that it works somehow differently than many to one / one to many. So it's not at all a stupid question.

Most of the time, you will need to add extra fields that are characteristics to this relation. Because you'll want to get some informations about your relationships themselves.

Whether or not you need extra information (and thus extra fields) will determine if you need a third-part table or not.

Real life example : men & women

Let's say you have Men and Women tables. A man can date many women through the course of his live, and reciprocally. So, you have a typical many to many relationship. You can do it without a third table.

But now, suppose you want to add an information: for each relationship (be it in a romantic sense or in a database sense (never thought I would say this sentence one day in my life)), you want to tell when the union started, and when it ended.

THEN you need a third part table.

Structure

Let's have a look at what our real life example structure would look like.

I wrote this in simplified Doctrine2-inspired Yaml , the model that runs Symfony2 by default:

tableName: Man
    fields:
        id:
            type: int
            id: true
        firstName:
            type: string
        dateOfBirth:
            type: datetime
        favSport:
            type: string
        womenHePrefers:
            type: string
    oneToMany:
        relationships:
            target: ManMeetsWoman
            mappedBy: man
            cascade: remove

tableName: Woman
    fields:
        id:
            type: int
            id: true
        firstName:
            type: string
        dateOfBirth:
            type: datetime
        favAuthor:
            type: string
        menShePrefers:
            type: string
    oneToMany:
        relationships:
            target: ManMeetsWoman
            mappedBy: woman
            cascade: remove

tableName: ManMeetsWoman
    fields:
        id:
            type: int
            id: true
        dateOfEncounter:
            type: datetime
        dateOfBreakUp:
            type: datetime
    manyToOne:
        man:
            target: Man
            inversedBy: relationships
        woman:
            target: Woman
            inversedBy: relationships

Key points

  • Your Man and Woman tables both have a oneToMany relationship to your ManMeetsWoman table
  • Your ManMeetsWoman table will have a manyToOne relationship to both other tables
  • Ideally, you will place cascading rules to ensure that when a Man is deleted from the Man table, relationships referencing him will also disappear from the third table.

What if you don't want another table

Then you will have to declare actual many to many relationships .

It will work pretty much the same, but you won't have any additional information about what existed between this man and this woman, other than yeah, something happened between them. We don't know what, we don't know when, we don't know for how long.

To do this, you will replace the oneToMany and manyToOne by this:

# Man:
    ManyToMany:
        woman:
            target: Woman

# Woman:
    ManyToMany:
        man:
            target: Man

But then it begins to get tricky to manage, without the support of a good ORM. So, the third-part table usually remains the best and easiest solution. Plus, it allows you to add informations about the relationship itself.

Yes indeed, it's an example of many to many relationship.

Here users is an entity; Team is another entity. Both the entity participate in Membership relationship.

So while converting this to relational table; you will have to create 3 tables.

One fore users , another for team and another for membership (you can have any meaningful name for the relationship) relationship which will have foreign key from both tables users and team like userid and teamid.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM