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

3.3.1 Creating and Selecting a Database创建和选择数据库
3.3.2 Creating a Table创建表
3.3.3 Loading Data into a Table将数据加载到表中
3.3.4 Retrieving Information from a Table从表中检索信息

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

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:本节介绍如何执行以下操作:

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. 可以从MySQL网站获得包含以下部分中使用的一些查询和示例数据的动物园分布。It is available in both compressed tar file and Zip formats at https://dev.mysql.com/doc/.它有压缩的tar文件和Zip格式,可从https://dev.mysql.com/doc/获得。

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

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

The mysql database describes user access privileges. mysql数据库描述用户访问权限。The test database often is available as a workspace for users to try things out.test数据库通常作为一个工作区供用户试用。

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. 语句显示的数据库列表在您的计算机上可能不同;如果您没有SHOW DATABASES权限,则SHOW DATABASES不会显示您没有权限的数据库。See Section 13.7.7.14, “SHOW DATABASES Statement”.请参阅第13.7.7.14节,“SHOW DATABASES语句”

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

mysql> USE test
Database changed

USE, like QUIT, does not require a semicolon. QUIT一样,USE不需要分号。(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.USE语句在另一方面也很特殊:它必须在一行中给出。

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. 您可以在下面的示例中使用test数据库(如果您有权访问它),但是您在该数据库中创建的任何内容都可以被任何有权访问它的人删除。For this reason, you should probably ask your MySQL administrator for permission to use a database of your own. 因此,您可能应该向MySQL管理员申请使用您自己的数据库的权限。Suppose that you want to call yours menagerie. 假设你想调用menagerieThe administrator needs to execute a statement like this:管理员需要执行如下语句:

mysql> GRANT ALL ON menagerie.* TO 'your_mysql_name'@'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.其中,your_mysql_name是分配给您的mysql用户名,your_client_host是连接到服务器的主机。

3.3.1 Creating and Selecting a Database
3.3.2 Creating a Table
3.3.3 Loading Data into a Table
3.3.4 Retrieving Information from a Table