結構變更時更新資料庫

SQL 關聯式資料庫入門

Timo Grossenbacher

Data Journalist

目前的資料庫模型

SQL 關聯式資料庫入門

目前的資料庫模型

SQL 關聯式資料庫入門

在新資料表只儲存 DISTINCT 資料

SELECT COUNT(*)
FROM university_professors;
 count
 -----
 1377
SELECT COUNT(DISTINCT organization) 
FROM university_professors;
 count
 -----
 1287
SQL 關聯式資料庫入門

將 DISTINCT 記錄 INSERT INTO 新資料表

INSERT INTO organizations 
SELECT DISTINCT organization, 
    organization_sector
FROM university_professors;
Output: INSERT 0 1287
INSERT INTO organizations 
SELECT organization, 
    organization_sector
FROM university_professors;
Output: INSERT 0 1377
SQL 關聯式資料庫入門

INSERT INTO 陳述式

INSERT INTO table_name (column_a, column_b)
VALUES ("value_a", "value_b");
SQL 關聯式資料庫入門

在 affiliations 中重新命名欄位(RENAME COLUMN)

CREATE TABLE affiliations (
 firstname text,
 lastname text,
 university_shortname text,
 function text,
 organisation text
);
ALTER TABLE table_name
RENAME COLUMN old_name TO new_name;
SQL 關聯式資料庫入門

在 affiliations 中刪除欄位(DROP COLUMN)

CREATE TABLE affiliations (
 firstname text,
 lastname text,
 university_shortname text,
 function text,
 organization text
);
ALTER TABLE table_name
DROP COLUMN column_name;
SQL 關聯式資料庫入門
SELECT DISTINCT firstname, lastname, 
    university_shortname 
FROM university_professors
ORDER BY lastname;
-[ RECORD 1 ]--------+-------------
firstname            | Karl
lastname             | Aberer
university_shortname | EPF
-[ RECORD 2 ]--------+-------------
firstname            | Reza Shokrollah
lastname             | Abhari
university_shortname | ETH
-[ RECORD 3 ]--------+-------------
firstname            | Georges
lastname             | Abou Jaoudé
university_shortname | EPF
(truncated)

(551 records)
SELECT DISTINCT firstname, lastname 
FROM university_professors
ORDER BY lastname;
-[ RECORD 1 ]----------------------
firstname | Karl
lastname  | Aberer
-[ RECORD 2 ]----------------------
firstname | Reza Shokrollah
lastname  | Abhari
-[ RECORD 3 ]----------------------
firstname | Georges
lastname  | Abou Jaoudé
(truncated)

(551 records)
SQL 關聯式資料庫入門

教授可由 firstname、lastname 唯一識別

SQL 關聯式資料庫入門

開始動手吧!

SQL 關聯式資料庫入門

Preparing Video For Download...