8.4 中文镜像

MySQL 8.4 参考手册 · 中文镜像

正文为中文,英文原文默认隐藏。点击任意中文段落即可展开 / 收起对应英文原文。

打开官方英文原文 ↗

译文由机器翻译生成,仅供理解参考;有疑问请以官方英文原文为准。

MySQL 8.4 参考手册 / 5 教程 / 5.3 创建和使用数据库
中文点击中文段落可显示 / 隐藏英文原文本页译文覆盖 100%

5.3 创建和使用数据库5.3 Creating and Using a Database

一旦你知道如何输入 SQL 语句,就可以访问数据库了。Once you know how to enter SQL statements, you are ready to access a database.

假设你家里(你的动物园)里有几只宠物,并且你想跟踪各种关于它们的信息。你可以通过创建表格来存储你的数据,并加载所需的信息来实现这一点。然后,你可以通过从表格中提取数据来回答关于你动物的不同类型的问题。本节会展示如何执行以下操作:Suppose that you have several pets in your home (your menagerie) and you would like to keep track of various types of information about them. You can do so by creating tables to hold your data and loading them with the desired information. Then you can answer different sorts of questions about your animals by retrieving data from the tables. This section shows you how to perform the following operations:

  • 创建一个数据库Create a database

  • 创建表格Create a table

  • 将数据加载到表格中Load data into the table

  • 以多种方式从表中检索数据Retrieve data from the table in various ways

  • 使用多个表格Use multiple tables

动物园数据库很简单(故意如此),但并不难想象现实世界中可能使用类似类型数据库的情况。例如,农民可以使用这样的数据库来跟踪牲畜,或者兽医可以用它来记录病人档案。包含以下各节中使用的一些查询和样本数据的动物园分发版本可以从 MySQL 网站获取。它提供压缩的 tar 文件和 Zip 格式,网址为 https://dev.mysql.com/doc/。The menagerie database is simple (deliberately), but it is not difficult to think of real-world situations in which a similar type of database might be used. For example, a database like this could be used by a farmer to keep track of livestock, or by a veterinarian to keep track of patient records. A menagerie distribution containing some of the queries and sample data used in the following sections can be obtained from the MySQL website. It is available in both compressed tar file and Zip formats at https://dev.mysql.com/doc/.

使用 SHOW 语句查看服务器上当前存在的数据库:Use the SHOW statement to find out what databases currently exist on the server:

mysql> SHOW DATABASES;
+----------+
| Database |
+----------+
| mysql    |
| test     |
| tmp      |
+----------+

mysql 数据库描述了用户访问权限。test 数据库通常作为用户尝试操作的工作空间。The mysql database describes user access privileges. The test database often is available as a workspace for users to try things out.

语句显示的数据库列表在你的机器上可能不同;SHOW DATABASES 不会显示你没有权限的数据库,如果你没有 SHOW DATABASES 权限。参见 第 15.7.7.15 节,“SHOW DATABASES 语句”。The list of databases displayed by the statement may be different on your machine; SHOW DATABASES does not show databases that you have no privileges for if you do not have the SHOW DATABASES privilege. See Section 15.7.7.15, “SHOW DATABASES Statement”.

如果 test 数据库存在,尝试访问它:If the test database exists, try to access it:

mysql> USE test
Database changed

USE,像 QUIT 一样,不需要分号。(如果你愿意,可以用分号终止这些语句;这样做无害。)USE 语句还有另一种特别之处:它必须写在一行内。USE, like QUIT, does not require a semicolon. (You can terminate such statements with a semicolon if you like; it does no harm.) The USE statement is special in another way, too: it must be given on a single line.

你可以使用 test 数据库(如果你有权限的话)来执行下面的示例,但在那个数据库中创建的任何内容都可能被其他有权限的人删除。因此,你可能应该向 MySQL 管理员请求使用你自己的数据库。假设你想将你的数据库命名为 menagerie。管理员需要执行如下语句:You can use the test database (if you have access to it) for the examples that follow, but anything you create in that database can be removed by anyone else with access to it. For this reason, you should probably ask your MySQL administrator for permission to use a database of your own. Suppose that you want to call yours menagerie. The administrator needs to execute a statement like this:

mysql> GRANT ALL ON menagerie.* TO 'your_mysql_name'@'your_client_host';

其中 your_mysql_name 是分配给你的 MySQL 用户名,your_client_host 是你连接到服务器的主机。where your_mysql_name is the MySQL user name assigned to you and your_client_host is the host from which you connect to the server.