Mysql 简明教程

MySQL - Select Database (USE Statement)

一旦连接到 MySQL 服务器,就需要选择一个数据库进行操作。这是因为 MySQL Server 可能有多个数据库可用。

Once you get connected with the MySQL server, it is required to select a database to work with. This is because there might be more than one database available with the MySQL Server.

MySQL USE Statement

要在 MySQL 中选择数据库,我们使用 SQL USE 语句。一旦选择特定的数据库,我们就可以执行不同的操作,例如创建表、添加数据、更新和删除信息。我们选择数据库后执行的每个操作都将存储在该特定数据库中。

To select a database in MySQL, we use the SQL USE statement. Once a specific database is selected, we can perform different operations such as creating tables, adding data, updating, and deleting information. Every operation we perform after selecting a database will be stored in that particular database.

Syntax

以下是 SQL 中 USE 语句的语法 −

Following is the syntax of the USE statement in SQL −

USE DatabaseName;

此处,“DatabaseName”是表示我们要使用的数据库名称的占位符。

Here, the "DatabaseName" is a placeholder representing the name of the database that we want to use.

数据库名称在 MySQL 或任何其他 RDBMS 中都必须始终唯一。

The database name must always be unique within the MySQL or any other RDBMS.

Example

让我们从使用以下 CREATE 查询创建名为 TUTORIALS 的数据库开始 −

Let us start by creating a database named TUTORIALS using the following CREATE query −

create database TUTORIALS;

现在,我们将使用以下查询获取 MySQL 服务器中存在的所有数据库 −

Now, we will fetch all the databases present in MySQL server using the below query −

Show databases;

以下是数据库列表

Following are the list of databases −

以下将选择/切换当前数据库至 TUTORIALS

The following will select/switch the current database to TUTORIALS

USE TUTORIALS;

Output

数据库已成功地选择/切换,无任何错误。

The database has been selected/switched successfully without any error.

Database changed

一旦我们完成切换至 TUTORIALS 数据库,我们便可执行各式各样的操作,例如创建表格,并将数据插入表格,如下所示

Once we finish switching to the database TUTORIALS, we can perform operations such as creating a table, and inserting data in that table as shown below −

CREATE TABLE CUSTOMERS (
   ID INT AUTO_INCREMENT,
   NAME VARCHAR(20) NOT NULL,
   AGE INT NOT NULL,
   ADDRESS CHAR (25),
   SALARY DECIMAL (18, 2),
   PRIMARY KEY (ID)
);

在表格创建后,执行以下查询以检索当前数据库 (TUTORIALS) 中存在的数据

Once the table is created, execute the following query to retrieve the data present in the current database (TUTORIALS) −

SHOW TABLES;

正如我们在下方的输出中可以看到,我们于选择 TUTORIALS 数据库后建立的 CUSTOMERS 表格已存储在其中。

As we can see in the output below, the CUSTOMERS table we have created after selecting the TUTORIALS database is stored in it.

Selecting a Non Existing MySQL Database

如果我们尝试在 MySQL 服务器中选择/切换至一个不存在的数据库,系统将产生一个错误,指出“未知数据库”。

If we try to select/switch a non-existent database in a MySQL server, it will result in an error stating that "Unkwown Database".

Example

此处,我们正试图选择/切换至一个不存在的数据库

Here, we are trying to select/swith to the database which doesn’t exist −

USE NonExistingDatabase;

以上查询的输出如下所示:

The output for the above query is produced as given below −

ERROR 1049 (42000): Unknown database 'nonexistingdatabase'

Selecting Database Using a Client Program

除了使用 MySQL 查询在 MySQL 服务器中选择/切换某一个数据库,我们还可以使用客户端程序执行 USE 操作。

Besides selecting/switching a database in a MySQL server using a MySQL query, we can also use a client program to perform the USE operation.

Syntax

以下是此操作在各种编程语言中的语法 −

Following are the syntaxes of this operation in various programming languages −

Example

以下是这些程序 −

Following are the programs −