4 安裝

Sakila 範例資料庫可從 https://mysqldev.dev.org.tw/doc/index-other.html 取得。可下載的壓縮檔以壓縮 tar 檔案或 Zip 格式提供。該壓縮檔包含三個檔案:sakila-schema.sqlsakila-data.sqlsakila.mwb

注意

Sakila 包含 MySQL 版本特定的註解,因此 sakila 綱要和資料取決於您的 MySQL 伺服器版本。例如,MySQL 伺服器 5.7.5 新增了對 InnoDB 的空間資料索引支援,因此 address 資料表將包含適用於 MySQL 5.7.5 和更高版本的空間感知 location 資料行。

sakila-schema.sql 檔案包含建立 Sakila 資料庫結構所需的所有 CREATE 陳述式,包括資料表、檢視表、預存程序和觸發器。

sakila-data.sql 檔案包含 INSERT 陳述式,用於填入 sakila-schema.sql 檔案建立的結構,以及在初始資料載入後必須建立的觸發器定義。

sakila.mwb 檔案是一個 MySQL Workbench 資料模型,您可以在 MySQL Workbench 中開啟它來檢查資料庫結構。如需更多資訊,請參閱 MySQL Workbench

若要安裝 Sakila 範例資料庫,請按照下列步驟進行

  1. 將安裝壓縮檔解壓縮到暫存位置,例如 C:\temp\/tmp/。當您解開壓縮檔時,它會建立一個名為 sakila-db 的目錄,其中包含 sakila-schema.sqlsakila-data.sql 檔案。

  2. 使用 mysql 命令列用戶端連線到 MySQL 伺服器,並使用下列命令

    $> mysql -u root -p

    出現提示時輸入您的密碼。可以使用非 root 帳戶,前提是該帳戶具有建立新資料庫的權限。

  3. 執行 sakila-schema.sql 腳本以建立資料庫結構,並執行 sakila-data.sql 腳本以填入資料庫結構,方法是使用下列命令

    mysql> SOURCE C:/temp/sakila-db/sakila-schema.sql;
    mysql> SOURCE C:/temp/sakila-db/sakila-data.sql;

    sakila-schema.sqlsakila-data.sql 檔案的路徑取代為您系統上的實際路徑。

    注意

    在 Windows 上,執行 SOURCE 命令時請使用斜線而非反斜線。

  4. 確認範例資料庫已正確安裝。執行下列陳述式。您應該會看到類似於此處顯示的輸出。

    mysql> USE sakila;
    Database changed
    
    mysql> SHOW FULL TABLES;
    +----------------------------+------------+
    | Tables_in_sakila           | Table_type |
    +----------------------------+------------+
    | actor                      | BASE TABLE |
    | actor_info                 | VIEW       |
    | address                    | BASE TABLE |
    | category                   | BASE TABLE |
    | city                       | BASE TABLE |
    | country                    | BASE TABLE |
    | customer                   | BASE TABLE |
    | customer_list              | VIEW       |
    | film                       | BASE TABLE |
    | film_actor                 | BASE TABLE |
    | film_category              | BASE TABLE |
    | film_list                  | VIEW       |
    | film_text                  | BASE TABLE |
    | inventory                  | BASE TABLE |
    | language                   | BASE TABLE |
    | nicer_but_slower_film_list | VIEW       |
    | payment                    | BASE TABLE |
    | rental                     | BASE TABLE |
    | sales_by_film_category     | VIEW       |
    | sales_by_store             | VIEW       |
    | staff                      | BASE TABLE |
    | staff_list                 | VIEW       |
    | store                      | BASE TABLE |
    +----------------------------+------------+
    23 rows in set (0.01 sec)
    
    mysql> SELECT COUNT(*) FROM film;
    +----------+
    | COUNT(*) |
    +----------+
    |     1000 |
    +----------+
    1 row in set (0.00 sec)
    
    mysql> SELECT COUNT(*) FROM film_text;
    +----------+
    | COUNT(*) |
    +----------+
    |     1000 |
    +----------+
    1 row in set (0.00 sec)