简体   繁体   中英

How to create default relationship for 2 MySQL tables with many-to-many relationships?

In MySQL, I have 2 tables, A and B , with many-to-many relationships. And I have a pivot table, A_B , for the relationships between table A and B.

In table A, there are some rows that I want to link to *ALL* (including current and future) rows in table B . For example, let's say currently there are 5 rows in table B . Row A2 in table A is linked to all 5 rows in table B . In the future, when new rows are added to table B , I want row A2 to be automatically linked to ALL those new rows in table B .

Currently I am thinking of using MySQL Trigger to add this function. But I am not sure whether this is the correct way to do this or not.

Or I should add a new column in table A , and use it as an indicator for the default relationship? Then in my programming codes, I can check the column value whether a row in table A has relationship to all rows in table B .

Pivot table

In Relational Database terms, that is an Associative Table.

What you have is a need, such that designated rows in Table_A are related to all rows in Table_B; and the rest re related via Table_A_B. The great Dr EF Codd asks us to think of data in terms of sets. There are two sets in Table_A. Let's call the first set All_B and the second set Specific_B, for which Table_A_B identifies the specific relations.

Therefore is not a "default" relationship, it is two separate relationships, based on the Subtype.

You don't need the "default" relationship you described. That would result in masses of rows that serve no purpose, because the rows in Table_B for All_B are known, they do not need to be stored.

You don't need triggers (which would have been necessary to keep inserting Table_A_B Rows whenever an All_B row is inserted). Ugly as sin.

The correct method is to use Exclusive Subtypes, named for the sets, where:

  • Specific_B is related to Table_B, via Table_A_B

  • Table_A itself, and All_B, are related to nothing.

Your code can determine which Subtype any Table_A row is, and join Table_B accordingly:

  • via Table_A_B for Specific_B

  • Cartesian Product Table_A::Table_B for All_B.

Picture

Typical XOR Gate • Table Relation Diagram

An Exclusive Subtype requires a Discriminator column in the Basetype.

If you don't want to bother with full Relational Integrity, then sure, an Indicator column in Table_A will suffice. That is the Discriminator column in Table_A, minus the Subtypes.

Response to Comment

This is the first time I learn about the concept of exclusive subtype. Now I know how to solve the problem

In that case, study this document on Subtypes . The links include code that you will need, such that the integrity is Declarative. If you would like a full discussion, and particularly to inoculate yourself from being seduced by the circus freaks who allege to be theoreticians, who peddle insane methods to enforce "integrity", read this Answer

I think using database triggers is the most appropriate way to do what you want because it conserve independence of the DB from program and DB is complete by itself.

But maybe you consider using Grouping concept as it is used in user accounts management DBs. you can create a group such as admins and relate all records of table B to it and only assign records of A to this group.

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