> ## Documentation Index
> Fetch the complete documentation index at: https://villagesql.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# 加载 Sakila 示例数据库

> 下载 Sakila 示例数据库，用 2 条命令把它的架构和数据加载到您的 MySQL 服务器中，然后检查每个表都已到位。

> [card] [VillageSQL is a drop-in replacement for MySQL with extensions.](https://villagesql.com/docs/en/8.4/stable/install-server.md)
>
> All examples on this page work on VillageSQL. Install Now →

Sakila 以 1 个 zip 中的 2 个 SQL 文件的形式提供。第一个建表，第二个填充数据。您按这个顺序运行它们，目标是 [安装服务器](https://villagesql.com/docs/zh/tutorial/install-mysql.md) 中的那台服务器。

## 下载

在您喜欢的任何目录中运行这些命令。

```bash theme={null}
curl -O https://downloads.mysql.com/docs/sakila-db.zip
unzip sakila-db.zip
```

这会留下一个 `sakila-db` 目录。您需要的 2 个文件是其中的 `sakila-schema.sql` 和 `sakila-data.sql`。

## 加载

先运行架构文件，再运行数据文件。如果 `root` 没有密码，在每个密码提示处按回车。

```bash theme={null}
mysql -u root -p < sakila-db/sakila-schema.sql
mysql -u root -p < sakila-db/sakila-data.sql
```

成功时，两条命令在密码提示之后都不打印任何内容。数据文件需要更长时间，因为它插入数据库中的每一行。

> [!NOTE]
>
> 架构文件做的第一件事是 `DROP SCHEMA IF EXISTS sakila`。再次运行这 2 条命令会从头重建数据库，所以您总能回到一份干净的副本。您改动过的任何东西都会随之消失。

## 检查

问问服务器数据库现在包含什么。

```bash theme={null}
mysql -u root -p -e "USE sakila; SHOW TABLES;"
```

```text theme={null}
Enter password:
+----------------------------+
| Tables_in_sakila           |
+----------------------------+
| actor                      |
| actor_info                 |
| address                    |
| category                   |
| city                       |
| country                    |
| customer                   |
| customer_list              |
| film                       |
| film_actor                 |
| film_category              |
| film_list                  |
| film_text                  |
| inventory                  |
| language                   |
| nicer_but_slower_film_list |
| payment                    |
| rental                     |
| sales_by_film_category     |
| sales_by_store             |
| staff                      |
| staff_list                 |
| store                      |
+----------------------------+
```

23 个名称，因为 `SHOW TABLES` 把 16 个表和 7 个视图列在一起。

表可能到位了而数据没有，所以也找点东西数一数。

```bash theme={null}
mysql -u root -p -e "SELECT COUNT(*) FROM sakila.film;"
```

```text theme={null}
Enter password:
+----------+
| COUNT(*) |
+----------+
|     1000 |
+----------+
```

1,000 部影片表示您可以开始第一课了。

## 在数据库内部工作

每一课写查询时都把 `sakila` 当作当前数据库。打开客户端时指定它：

```bash theme={null}
mysql -u root -p sakila
```

或者进入之后切换到它：

```sql theme={null}
USE sakila;
```

两种做法都不用的话，提到 `film` 的查询会失败。

```bash theme={null}
mysql -u root -p -e "SELECT COUNT(*) FROM film;"
```

```text theme={null}
Enter password:
ERROR 1046 (3D000) at line 1: No database selected
```

## 另请参阅

* [Sakila 示例数据库](https://villagesql.com/docs/zh/tutorial/sakila-database.md) — 16 个表各保存什么
* [SELECT](https://villagesql.com/docs/zh/tutorial/select.md) — 第一课，它查询本课加载的数据


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.