3.3. 外部索引鍵

回想一下在第 2 章中的表格 weather 及 cities,思考下列問題:
你想要保證沒有另一個人可以新增在 cities 中沒有的城市資料到 weather 中。這就是所謂資料關連性的管理。在簡單的資料庫系統當中,可能會這樣實作:先檢查 cities 中是否已有對應的資料,然後再決定資料表 weather 中新增或拒絕新的天氣資料。這個辦法還有很多問題,而且很不方便,所以 PostgreSQL 可以幫助你解決這個需求。

新的資料表宣告如下所示:

  1. CREATE TABLE cities (
  2. city varchar(80) primary key,
  3. location point
  4. );
  5. CREATE TABLE weather (
  6. city varchar(80) references cities(city),
  7. temp_lo int,
  8. temp_hi int,
  9. prcp real,
  10. date date
  11. );

現在嘗試新增一筆不合理的資料:

  1. INSERT INTO weather VALUES ('Berkeley', 45, 53, 0.0, '1994-11-28');
  1. ERROR: insert or update on table "weather" violates foreign key constraint "weather_city_fkey"
  2. DETAIL: Key (city)=(Berkeley) is not present in table "cities".

外部索引鍵或簡稱外部鍵(foreign key)的行為可以讓你的應用程式變得容易調整。我們在這個導覽中不會再深入這個簡單的例子了,但你可以在第 5 章取得進一步的資訊。正確地使用外部索引鍵,可以改善資料庫應用程式的品質,所以強烈建議一定要好好學習它。