Make sure that you are connected to the server, as discussed in the previous section. 确保已连接到服务器,如前一节所述。Doing so does not in itself select any database to work with, but that is okay. 这样做本身不会选择任何要使用的数据库,但这没关系。At this point, it is more important to find out a little about how to issue queries than to jump right in creating tables, loading data into them, and retrieving data from them. 此时,了解一点如何发出查询比直接创建表、将数据加载到表中以及从表中检索数据更重要。This section describes the basic principles of entering queries, using several queries you can try out to familiarize yourself with how mysql works.本节介绍输入查询的基本原则,使用几个查询您可以尝试熟悉mysql的工作方式。
Here is a simple query that asks the server to tell you its version number and the current date. 下面是一个简单的查询,要求服务器告诉您其版本号和当前日期。Type it in as shown here following the 按如下所示输入mysql>
prompt and press Enter:mysql>
提示并按Enter键:
mysql> SELECT VERSION(), CURRENT_DATE;
+-----------+--------------+
| VERSION() | CURRENT_DATE |
+-----------+--------------+
| 5.8.0-m17 | 2015-12-21 |
+-----------+--------------+
1 row in set (0.02 sec)
mysql>
This query illustrates several things about mysql:此查询说明了mysql的几个方面:
A query normally consists of an SQL statement followed by a semicolon. 查询通常由SQL语句后跟分号组成。(There are some exceptions where a semicolon may be omitted. (有些例外情况下可以省略分号。前面提到的QUIT
, mentioned earlier, is one of them. QUIT
就是其中之一。We'll get to others later.)我们稍后再联系其他人。)
When you issue a query, mysql sends it to the server for execution and displays the results, then prints another 当您发出查询时,mysql将其发送到服务器执行并显示结果,然后打印另一个mysql>
prompt to indicate that it is ready for another query.mysql>
提示以指示它已准备好进行另一个查询。
mysql displays query output in tabular form (rows and columns). mysql以表格形式(行和列)显示查询输出。The first row contains labels for the columns. 第一行包含列的标签。The rows following are the query results. 下面的行是查询结果。Normally, column labels are the names of the columns you fetch from database tables. 通常,列标签是从数据库表中获取的列的名称。If you're retrieving the value of an expression rather than a table column (as in the example just shown), mysql labels the column using the expression itself.如果检索的是表达式的值,而不是表列的值(如刚才所示),mysql将使用表达式本身标记该列。
mysql shows how many rows were returned and how long the query took to execute, which gives you a rough idea of server performance. mysql显示返回了多少行以及执行查询所用的时间,这让您大致了解服务器性能。These values are imprecise because they represent wall clock time (not CPU or machine time), and because they are affected by factors such as server load and network latency. 这些值不精确,因为它们表示挂钟时间(而不是CPU或机器时间),并且受服务器负载和网络延迟等因素的影响。(For brevity, the “rows in set” line is sometimes not shown in the remaining examples in this chapter.)(为简洁起见,“集合中的行”行有时不在本章剩余示例中显示。)
Keywords may be entered in any lettercase. The following queries are equivalent:关键字可以用任何字母输入。以下查询是等效的:
mysql>SELECT VERSION(), CURRENT_DATE;
mysql>select version(), current_date;
mysql>SeLeCt vErSiOn(), current_DATE;
Here is another query. 这是另一个问题。It demonstrates that you can use mysql as a simple calculator:它演示了您可以将mysql用作一个简单的计算器:
mysql> SELECT SIN(PI()/4), (4+1)*5;
+------------------+---------+
| SIN(PI()/4) | (4+1)*5 |
+------------------+---------+
| 0.70710678118655 | 25 |
+------------------+---------+
1 row in set (0.02 sec)
The queries shown thus far have been relatively short, single-line statements. 迄今为止显示的查询都是相对较短的单行语句。You can even enter multiple statements on a single line. Just end each one with a semicolon:您甚至可以在一行中输入多条语句。只需以分号结尾:
mysql> SELECT VERSION(); SELECT NOW();
+-----------+
| VERSION() |
+-----------+
| 8.0.13 |
+-----------+
1 row in set (0.00 sec)
+---------------------+
| NOW() |
+---------------------+
| 2018-08-24 00:56:40 |
+---------------------+
1 row in set (0.00 sec)
A query need not be given all on a single line, so lengthy queries that require several lines are not a problem. 一个查询不需要在一行中给出全部内容,因此需要几行的冗长查询不是问题。mysql determines where your statement ends by looking for the terminating semicolon, not by looking for the end of the input line. mysql通过查找终止分号而不是输入行的结尾来确定语句的结尾。(In other words, mysql accepts free-format input: it collects input lines but does not execute them until it sees the semicolon.)(换句话说,mysql接受自由格式输入:它收集输入行,但在看到分号之前不会执行它们。)
Here is a simple multiple-line statement:下面是一个简单的多行语句:
mysql>SELECT
->USER()
->,
->CURRENT_DATE;
+---------------+--------------+ | USER() | CURRENT_DATE | +---------------+--------------+ | jon@localhost | 2018-08-24 | +---------------+--------------+
In this example, notice how the prompt changes from 在本例中,请注意输入多行查询的第一行后,提示是如何从mysql>
to ->
after you enter the first line of a multiple-line query. mysql>
更改至->
。This is how mysql indicates that it has not yet seen a complete statement and is waiting for the rest. mysql就是这样表示它还没有看到完整的语句,正在等待其余的语句。The prompt is your friend, because it provides valuable feedback. 提示是您的朋友,因为它提供了有价值的反馈。If you use that feedback, you can always be aware of what mysql is waiting for.如果您使用这些反馈,您可以随时知道mysql在等待什么。
If you decide you do not want to execute a query that you are in the process of entering, cancel it by typing 如果您决定不执行正在输入的查询,请键入\c
:\c
:
mysql>SELECT
->USER()
->\c
mysql>
Here, too, notice the prompt. 这里也请注意提示。It switches back to 键入mysql>
after you type \c
, providing feedback to indicate that mysql is ready for a new query.\c
后它切换回mysql>
,提供反馈以指示mysql已准备好进行新查询。
The following table shows each of the prompts you may see and summarizes what they mean about the state that mysql is in.下表显示了您可能看到的每个提示,并总结了它们对mysql所处状态的含义。
mysql> | |
-> | |
'> | ' )' )号开始的字符串完成 |
"> | " )" )开头的字符串完成 |
`> | ` )` )开头的标识符完成 |
/*> | /* /* 开始的注释完成 |
Multiple-line statements commonly occur by accident when you intend to issue a query on a single line, but forget the terminating semicolon. 当您打算在一行上发出查询,但忘记了终止分号时,通常会意外地出现多行语句。In this case, mysql waits for more input:在这种情况下,mysql将等待更多输入:
mysql> SELECT USER()
->
If this happens to you (you think you've entered a statement but the only response is a 如果这种情况发生在您身上(您认为您输入了一条语句,但唯一的回答是->
prompt), most likely mysql is waiting for the semicolon. ->
提示符),很可能mysql正在等待分号。If you don't notice what the prompt is telling you, you might sit there for a while before realizing what you need to do. 如果您没有注意到提示告诉您的内容,您可能会在意识到需要做什么之前在那里坐一会儿。Enter a semicolon to complete the statement, and mysql executes it:输入分号以完成语句,mysql将执行该语句:
mysql>SELECT USER()
->;
+---------------+ | USER() | +---------------+ | jon@localhost | +---------------+
The '>
and ">
prompts occur during string collection (another way of saying that MySQL is waiting for completion of a string). '>
及">
在字符串收集过程中会出现提示(表示MySQL正在等待字符串完成的另一种方式)。In MySQL, you can write strings surrounded by either 在MySQL中,您可以编写由'
or "
characters (for example, 'hello'
or "goodbye"
), and mysql lets you enter strings that span multiple lines. '
或"
字符包围的字符串(例如,'hello'
或"goodbye"
),mysql允许您输入跨多行的字符串。When you see a 当您看到一个'>
or ">
prompt, it means that you have entered a line containing a string that begins with a '
or "
quote character, but have not yet entered the matching quote that terminates the string. '>
或">
提示时,表示您已输入一行,其中包含以'
或"
引号字符开头的字符串,但尚未输入终止该字符串的匹配引号。This often indicates that you have inadvertently left out a quote character. 这通常表示您无意中遗漏了引号字符。For example:例如:
mysql> SELECT * FROM my_table WHERE name = 'Smith AND age < 30;
'>
If you enter this 如果输入此SELECT
statement, then press Enter and wait for the result, nothing happens. SELECT
语句,然后按Enter键并等待结果,则不会发生任何事情。Instead of wondering why this query takes so long, notice the clue provided by the 请注意'>
prompt. '>
提示提供的线索,而不是疑惑此查询为何需要如此长的时间。It tells you that mysql expects to see the rest of an unterminated string. 它告诉您mysql希望看到未终止字符串的其余部分。(Do you see the error in the statement? The string (你看到语句中的错误了吗?字符串'Smith
is missing the second single quotation mark.)'Smith
缺少第二个单引号。)
At this point, what do you do? 在这一点上,你会怎么做?The simplest thing is to cancel the query. 最简单的方法是取消查询。However, you cannot just type 但是,在本例中不能只键入\c
in this case, because mysql interprets it as part of the string that it is collecting. \c
,因为mysql将其解释为它正在收集的字符串的一部分。Instead, enter the closing quote character (so mysql knows you've finished the string), then type 相反,请输入结束引号字符(以便mysql知道您已经完成了字符串),然后键入\c
:\c
:
mysql>SELECT * FROM my_table WHERE name = 'Smith AND age < 30;
'>'\c
mysql>
The prompt changes back to 提示变回mysql>
, indicating that mysql is ready for a new query.mysql>
,表示mysql已准备好进行新查询。
The `>
prompt is similar to the '>
and ">
prompts, but indicates that you have begun but not completed a backtick-quoted identifier.`>
提示类似于'>
及">
提示,但表示您已开始但未完成反勾引号标识符。
It is important to know what the 了解'>
, ">
, and `>
prompts signify, because if you mistakenly enter an unterminated string, any further lines you type appear to be ignored by mysql—including a line containing QUIT
. '>
、">
和`>
提示意味着什么很重要,因为如果您错误地输入了一个未终止的字符串,mysql会忽略您输入的任何其他行,包括包含QUIT
的行。This can be quite confusing, especially if you do not know that you need to supply the terminating quote before you can cancel the current query.这可能会非常混乱,尤其是如果您不知道在取消当前查询之前需要提供终止报价。
Multiline statements from this point on are written without the secondary (从这一点开始,多行语句在编写时不使用辅助(->
or other) prompts, to make it easier to copy and paste the statements to try for yourself.->
或其他)提示,以便更轻松地复制和粘贴语句,以便自己尝试。