In this series of articles, we are going to learn about the basics of the MySQL database server. In this article, I am going to explain how we can query the data from the MySQL server using the SELECT statement.
在本系列文章中,我们将学习MySQL数据库服务器的基础知识。 在本文中,我将解释如何使用SELECT语句从MySQL服务器查询数据。
For the demonstration, I have installed MySQL 8.0 and MySQL Workbench on my workstation and also restored a demo database named sakila. This article helps you to install the MySQL server on Windows 10. You can download the sakila database from here, and the installation steps are provided in this article.
为了演示,我在工作站上安装了MySQL 8.0和MySQL Workbench,还还原了一个名为sakila的演示数据库。 本文帮助您安装在Windows 10.您MySQL服务器可以从下载sakila数据库在这里 ,并在提供的安装步骤此文章。
Now to understand the schema architecture of the tables of the sakila, we will generate a schema diagram by reverse-engineering the database.
现在,要了解sakila表的架构,我们将通过对数据库进行反向工程来生成架构图。
To generate the schema diagram, Open MySQL workbench -> Connect to MySQL database engine -> From the menu bar, click on the database -> Select Reverse Engineer. See the following image:
要生成模式图,请打开MySQL工作台->连接到MySQL数据库引擎 ->从菜单栏中,单击数据库 ->选择反向工程师 。 见下图:
On the Reverse Engineer Database screen, choose the connection name from the Stored Connection drop-down box, choose the appropriate connection method (TCP/IP OR Named Pipe) from Connection Method drop-down box. Enter hostname or IP Address in the Hostname text box. Enter the appropriate port number in Port Number text box. And finally, enter the appropriate user name in the Username text box and click on Next. See the following image:
在逆向工程数据库屏幕上,从所存储的连接下拉框中连接的名称,然后从连接方法相应的连接方式(TCP / IP或命名管道 )下拉框。 在“主机名”文本框中输入主机名或IP地址。 在“端口号”文本框中输入适当的端口号。 最后,在用户名文本框中输入相应的用户名 ,然后单击下一步 。 见下图:
Now, MySQL database server tries to establish the connection between the reverse engineering wizard and server and fetch the information of the database hosted on the server. If it fails, then the error will be shown on Connect to DBMS and fetch the information screen. The connection is established successfully. See the following image. Click on Next.
现在,MySQL数据库服务器尝试在逆向工程向导和服务器之间建立连接,并获取服务器上托管的数据库的信息。 如果失败,则错误将显示在“ 连接到DBMS”并获取信息屏幕上。 连接成功建立。 参见下图。 单击下一步。
On the Select Schemas to Reverse Engineer screen, you can choose the database. The wizard will generate the schema diagram of the selected database. We want to generate the schema of the sakila database; hence, click on sakila and click on Next. See the following image:
在“ 选择要逆向工程的模式”屏幕上,可以选择数据库。 该向导将生成所选数据库的架构图。 我们要生成sakila数据库的模式; 因此,单击sakila ,然后单击Next。 见下图:
On retrieve and reverse engineer schema object screen, the wizard populates the schema object to the sakila database. If any occurs, which fetching the object, it displays on the screen. The schema is fetched successfully. Click on Next. See the following image:
在“检索和反向工程模式对象”屏幕上,向导将模式对象填充到sakila数据库。 如果发生任何事情(取回对象),它将显示在屏幕上。 架构已成功获取。 单击下一步。 见下图:
On Select objects to Reverse Engineer screen, you can choose the objects which you want to reverse engineer. We want to generate the diagram of the tables hence select Import MySQL table object and click on Execute. See the following image:
在选择要逆向工程的对象屏幕上,可以选择要逆向工程的对象。 我们要生成表图,因此选择Import MySQL table object并单击Execute 。 见下图:
The reverse engineering process on the database starts. If it encounters any error, it will be displayed on the Reverse engineering process screen. The process was completed successfully. Click on Next. See the following image:
数据库的逆向工程过程开始。 如果遇到任何错误,它将显示在“逆向工程过程”屏幕上。 该过程已成功完成。 单击下一步。 见下图:
On the Reverse Engineering Result screen, you can see the details of the reverse-engineered objects. Click on Finish to close the dialog box. See the following image:
在“ 反向工程结果”屏幕上,您可以看到反向工程对象的详细信息。 单击完成关闭对话框。 见下图:
Once the process completes, you can see the schema diagram of the sakila database that has been created. The diagram is in the EER tab of MySQL workbench. If you want to use it later, you can save this diagram. See the following image:
该过程完成后,您可以看到已创建的sakila数据库的架构图。 该图位于MySQL工作台的EER选项卡中。 如果要以后使用,可以保存此图表。 见下图:
Now, as the diagram has been prepared, we can use it to understand the schema structure of the database and use the tables to understand the concept of MySQL database programming. First, let me explain about the SELECT statement.
现在,在准备好图之后,我们可以使用它来了解数据库的架构结构,并使用表来了解MySQL数据库编程的概念。 首先,让我解释一下SELECT语句。
The SELECT statement is used to populate data from any table of the MySQL database server. It is neither a DML (Data modification language) or DDL (Data definition language). It is a limited form of DML statement that is used only to populate the data from the database. The basic syntax of the SELECT statement is following
SELECT语句用于从MySQL数据库服务器的任何表填充数据。 它既不是DML(数据修改语言)也不是DDL(数据定义语言)。 它是DML语句的一种受限形式,仅用于填充数据库中的数据。 SELECT语句的基本语法如下
Select
,
,..
From Table;
In the syntax, the keyword SELECT keyword is to instruct the database to retrieve the data. The second segment is the list of columns or the expressions which you want to retrieve in the result set. Here you can specify the list of columns or the expressions, or you can specify the star(*). Now, when we specify the star(*), then the query populates all the columns.
在语法中,关键字SELECT关键字用于指示数据库检索数据。 第二段是要在结果集中检索的列或表达式的列表。 您可以在此处指定列或表达式的列表,也可以指定star(*)。 现在,当我们指定star(*)时,查询将填充所有列。
Finally, in the FROM keyword, you can specify the name of the table or the view. If you want to filter the data or sort the data, the keywords will be placed after the name of the table or view.
最后,在FROM关键字中,您可以指定表或视图的名称。 如果要过滤数据或对数据进行排序,关键字将放在表或视图的名称之后。
In the syntax, the semicolon is used as a statement delimiter. The semicolon is considered as an end of the query. If you want to get the result of multiple queries, then you must specify semicolon at the end of an individual query. When we specify it at the end of multiple queries, MySQL executes them individually and generates separate resultsets for them.
在语法中,分号用作语句定界符。 分号被认为是查询的结尾。 如果要获取多个查询的结果,则必须在单个查询的末尾指定分号。 当我们在多个查询的末尾指定它时,MySQL会分别执行它们并为它们生成单独的结果集。
Note: Many users ask how we can run multiple SELECT statements in MySQL Workbench. When we want to use multiple SELECT statements in the query, you must specify semicolon at the end of the query. The result set of the queries will be displayed in multiple tabs of the output window.
注意:许多用户问我们如何在MySQL Workbench中运行多个SELECT语句。 当我们要在查询中使用多个SELECT语句时,必须在查询末尾指定分号。 查询的结果集将显示在输出窗口的多个选项卡中。
For example, I want to populate only the first_name, last_name, and email column of the customer table of the sakila database. The query should be written as follows:
例如,我只想填充sakila数据库的客户表的first_name,last_name和email列。 该查询应编写如下:
use sakila;
select first_name, last_name, email from customer;
The output is below
输出如下
For example, I want to populate all columns of the customer table of the sakila database. The query should be written as follows:
例如,我要填充sakila数据库的customer表的所有列。 该查询应编写如下:
use sakila;
select * from customer;
The output is shown below:
输出如下所示:
In this article, I have explained how we can generate the schema of an existing MySQL database using MySQL Workbench. Moreover, I have also explained the SELECT statement in the MySQL database server. In the next article, I am going to cover how we can filter and sort the data from the table. Stay tuned..!!
在本文中,我已经解释了如何使用MySQL Workbench生成现有MySQL数据库的架构。 此外,我还解释了MySQL数据库服务器中的SELECT语句。 在下一篇文章中,我将介绍如何对表中的数据进行过滤和排序。 敬请关注..!!
Learn MySQL: Querying data from MySQL server using the SELECT statement |
Learn MySQL: What is pagination |
Learn MySQL: Sorting and Filtering data in a table |
学习MySQL:使用SELECT语句从MySQL服务器查询数据 |
学习MySQL:什么是分页 |
学习MySQL:对表中的数据进行排序和过滤 |
翻译自: https://www.sqlshack.com/learn-mysql-querying-data-from-mysql-server-using-the-select-statement/