如何設計n多對多關係以使sql查詢更容易 (How to design n many to many relationship in order make sql query easily)


問題描述

如何設計n多對多關係以使sql查詢更容易 (How to design n many to many relationship in order make sql query easily)

我在 3 個表之間有 3 個多對多關係,遵循此規則:

  • 表 A 和表 B 之間存在多對多關係。

    li>
  • 表A和表C之間存在多對多關係。

  • 表B和表C之間存在多對多關係。

我會在它們之間使用關聯表,但我會尋找最好的方法來處理它。在我看來,我認為創建映射表,其中將包含 idA、idB、idC 作為外鍵,並且每個表與映射表之間具有一對多的關係。


參考解法

方法 1:

All relationships are represented in tables as associations between columns/values representing entity identifiers. Relationships are not between tables ‑ that's a confusion between conceptual modelling (where relationships exist between entities) and physical modelling (where tables can be joined based matching values).

You propose a single ternary relationship table instead of properly implementing 3 separate binary relationship tables. I assume all the columns will be nullable? That's going to prevent you from implementing a primary key, and depending on your DBMS, unique constraints may be problematic too. Not to mention figuring out what it means when all 3 fields in a row are populated.

Do it the right way, and create 3 separate relationship tables in which you at least have a unique constraint on the two entity columns.

方法 2:

Usually when we have many‑to‑many relationship between two tables, a new table is created for the "many‑to‑many" relationship, where primary keys of both tables are added as foreign keys, you can study it in detail, i'm in the entry level, on the basis of my knowledge i answered.

(by Mamadou BerthéreaanbZia Ullah)

參考文件

  1. How to design n many to many relationship in order make sql query easily (CC BY‑SA 2.5/3.0/4.0)

#SQL #Database #data-modeling #erd #many-to-many






相關問題

如何組合表和視圖? (How do combine tables and views?)

Sql中的WHERE,結合兩個快速條件會成倍增加成本 (WHERE in Sql, combining two fast conditions multiplies costs many times)

Oracle : Выкарыстанне ўкладзенага запыту супраць выкарыстання адлюстравання (Oracle : Using nested query vs using mapping)

SQL在友誼表中插入值基於 (SQL insert value in friendship table based on)

SQL 查詢沒有返回任何值 (SQL query did not return any values)

PL/SQL 塊和循環練習 (PL/SQL block and LOOP exercise)

查找與日曆相比缺失的日期 (Find missing date as compare to calendar)

在 C# 中使用數據庫需要一些幫助 (Need some help working with databases in C#)

如何設計n多對多關係以使sql查詢更容易 (How to design n many to many relationship in order make sql query easily)

在 SQL 中從 3 個視圖創建一個視圖 (Creating a view from 3 views in SQL)

java while (resultset.next()) 不返回同一列中的所有數據 (java while (resultset.next()) does not return all data in the same column)

從訪問表單的文本字段傳遞開始和結束日期參數 (Pass start and end date parameter from text field of access form)







留言討論