用限制提升資料品質

SQL 關聯式資料庫入門

Timo Grossenbacher

Data Journalist

完整性限制

  1. 屬性限制,例如欄位的資料型別(第 2 章)
  2. 鍵值限制,例如主鍵(第 3 章)
  3. 參照完整性限制,透過外鍵強制(第 4 章)
SQL 關聯式資料庫入門

為何需要限制?

  • 限制賦予資料結構
  • 限制有助一致性,進而提升資料品質
  • 資料品質是商業優勢/資料科學前提
  • 強制執行不易,但 PostgreSQL 能幫上忙
SQL 關聯式資料庫入門

以資料型別作為屬性限制

引自 PostgreSQL 文件

SQL 關聯式資料庫入門

處理資料型別(轉型)

CREATE TABLE weather (
 temperature integer,
 wind_speed text);

SELECT temperature * wind_speed AS wind_chill FROM weather;
operator does not exist: integer * text
HINT: No operator matches the given name and argument type(s). 
You might need to add explicit type casts.
SELECT temperature * CAST(wind_speed AS integer) AS wind_chill
FROM weather;
SQL 關聯式資料庫入門

一起來練習吧!

SQL 關聯式資料庫入門

Preparing Video For Download...