建模更複雜的關係

SQL 關聯式資料庫入門

Timo Grossenbacher

Data Journalist

目前的資料庫模型

  • 1:N 關係以單一外鍵實作
SQL 關聯式資料庫入門

最終的資料庫模型

SQL 關聯式資料庫入門

如何實作 N:M 關係

  • 建立一個資料表
  • 為每個相連資料表加入外鍵
  • 加上額外屬性
CREATE TABLE affiliations (
 professor_id integer REFERENCES professors (id),
 organization_id varchar(256) REFERENCES organizations (id),
 function varchar(256)
);
  • 沒有主鍵!
  • 可能的 PK = {professor_id, organization_id, function}
SQL 關聯式資料庫入門

開始動手實作!

SQL 關聯式資料庫入門

Preparing Video For Download...