确保你已经连接到服务器,如前一节所述。仅仅连接服务器本身并不会选择任何要使用的数据库,但这没关系。目前,比起立即创建表、向表中加载数据以及从表中检索数据,了解如何发出查询更为重要。本节将介绍输入查询的基本原理,并使用几个查询示例让你熟悉mysql的工作方式。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> SELECT VERSION(), CURRENT_DATE;
+-----------+--------------+
| VERSION() | CURRENT_DATE |
+-----------+--------------+
| 8.4.0-tr | 2024-01-25 |
+-----------+--------------+
1 row in set (0.00 sec)
mysql>
这个查询展示了关于 mysql 的几件事:This query illustrates several things about mysql:
一个查询通常由一个 SQL 语句后跟一个分号组成。(有一些例外情况可以省略分号。之前提到的
QUIT就是其中之一。其他情况我们稍后会讲到。)A query normally consists of an SQL statement followed by a semicolon. (There are some exceptions where a semicolon may be omitted.QUIT, mentioned earlier, is one of them. We'll get to others later.)当你发出一个查询时,mysql 会将其发送到服务器进行执行并显示结果,然后打印另一个
mysql>提示符,以表明它已准备好接收另一个查询。When you issue a query, mysql sends it to the server for execution and displays the results, then prints anothermysql>prompt to indicate that it is ready for another query.mysql 以表格形式(行和列)显示查询输出。第一行包含列标签。之后的行是查询结果。通常,列标签是从表中获取的列的名称。如果你获取的是一个表达式的值而不是表的列(如刚才显示的示例中),mysql 将使用该表达式本身作为列标签。mysql displays query output in tabular form (rows and columns). 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 显示返回了多少行以及查询执行所用的时间,这可以让你大致了解服务器的性能。这些值不精确,因为它们表示的是实际时间(而非 CPU 或机器时间),并且会受到服务器负载和网络延迟等因素的影响。(为简洁起见,本章后续示例中有时不会显示 “rows in set” 这一行。)mysql shows how many rows were returned and how long the query took to execute, which gives you a rough idea of server performance. 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. (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;
这里是另一个查询。它演示了你可以使用 mysql 作为一个简单的计算器:Here is another query. It demonstrates that you can use mysql as a simple calculator:
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.4.0-tr |
+-----------+
1 row in set (0.00 sec)
+---------------------+
| NOW() |
+---------------------+
| 2024-01-25 18:33:04 |
+---------------------+
1 row in set (0.00 sec)
查询不必全部写在单行上,因此需要多行的长查询也没有问题。mysql通过查找结束分号来确定语句的结束,而不是通过查找输入行的结束。(换句话说,mysql接受自由格式输入:它会收集输入行,但直到看到分号才执行它们。)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. (In other words, mysql accepts free-format input: it collects input lines but does not execute them until it sees the semicolon.)
这里是一个简单的多行语句:Here is a simple multiple-line statement:
mysql>SELECT->USER()->,->CURRENT_DATE;+---------------+--------------+ | USER() | CURRENT_DATE | +---------------+--------------+ | jon@localhost | 2018-08-24 | +---------------+--------------+
在这个示例中,请注意,当你输入多行查询的第一行后,提示符如何从 mysql> 变为 ->。这就是 mysql 表示尚未看到完整语句并正在等待其余部分的方式。提示符是你的朋友,因为它提供了有价值的反馈。如果你利用这些反馈,你总能知道 mysql 正在等待什么。In this example, notice how the prompt changes from
mysql> to -> after you
enter the first line of a multiple-line query. This is how
mysql indicates that it has not yet seen a
complete statement and is waiting for the rest. 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.
如果你决定不想执行正在输入的查询,可以通过输入 \c 来取消它:If you decide you do not want to execute a query that you are in
the process of entering, cancel it by typing
\c:
mysql>SELECT->USER()->\cmysql>
这里也要注意提示符。在你输入 \c 之后,它会切换回 mysql>,提供反馈以表明 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.
下表显示了你可能看到的每一个提示符,并总结了它们所表示的 mysql 所处的状态。The following table shows each of the prompts you may see and summarizes what they mean about the state that mysql is in.
| 提示Prompt | 意义Meaning |
|---|---|
mysql> |
准备好新的查询Ready for new query |
-> |
等待多行查询的下一行Waiting for next line of multiple-line query |
'> |
等待下一行,等待以单引号 (') 开始的字符串完成Waiting for next line, waiting for completion of a string that began
with a single quote (') |
"> |
等待下一行,等待以双引号 (") 开始的字符串完成Waiting for next line, waiting for completion of a string that began
with a double quote (") |
`> |
等待下一行,等待以反引号 (`) 开始的标识符完成Waiting for next line, waiting for completion of an identifier that
began with a backtick (`) |
/*> |
等待下一行,等待以/*开始的注释完成Waiting for next line, waiting for completion of a comment that began
with /* |
多行语句通常是意外发生的,当你打算在单行中执行查询,但忘记终止分号时。在这种情况下,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> SELECT USER()
->
如果这种情况发生在你身上(你以为自己输入了一个语句,但唯一的响应是一个 -> 提示符),很可能是 mysql 正在等待分号。如果你没有注意到提示符在告诉你什么,你可能会坐在那里一段时间,才意识到你需要做什么。输入一个分号完成语句,然后 mysql 就会执行它: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. 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>SELECT USER()->;+---------------+ | USER() | +---------------+ | jon@localhost | +---------------+
'> 和 "> 提示出现在字符串收集期间(换句话说,MySQL 正在等待字符串完成)。在 MySQL 中,你可以使用 ' 或 " 字符来书写字符串(例如,'hello' 或 "goodbye"),并且 mysql 允许你输入跨多行的字符串。当你看到 '> 或 "> 提示时,这意味着你已经输入了一行以 ' 或 " 引号字符开始的字符串,但尚未输入结束该字符串的匹配引号。这通常表示你不小心遗漏了一个引号字符。例如:The '> and "> prompts
occur during string collection (another way of saying that MySQL
is waiting for completion of a string). In MySQL, you can write
strings surrounded by either ' or
" characters (for example,
'hello' or "goodbye"), and
mysql lets you enter strings that span multiple
lines. 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;
'>
如果你输入这个 SELECT 语句,然后按 Enter 并等待结果,什么也不会发生。与其困惑于为什么这个查询需要这么长时间,不如注意 '> 提示提供的线索。它告诉你 mysql 期望看到未终止字符串的其余部分。(你看到语句中的错误了吗?字符串 'Smith 缺少第二个单引号。)If you enter this SELECT statement,
then press Enter and wait for the result, nothing
happens. 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. (Do you see the error in the statement? The
string 'Smith is missing the second single
quotation mark.)
这时候,你该怎么办?最简单的方法是取消查询。然而,在这种情况下,你不能直接输入 \c,因为 mysql 会将其解释为它正在收集的字符串的一部分。相反,需要先输入闭合引号字符(这样 mysql 才能知道你已经完成了字符串),然后再输入 \c: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. Instead, enter the closing quote
character (so mysql knows you've finished the
string), then type \c:
mysql>SELECT * FROM my_table WHERE name = 'Smith AND age < 30;'>'\cmysql>
提示符会变回 mysql>,表示 mysql 已经准备好接收新的查询。The prompt changes back to mysql>,
indicating that mysql is ready for a new query.
`> 提示符类似于 '> 和 "> 提示符,但表示您已经开始但尚未完成一个反引号括起来的标识符。The `> prompt is similar to the
'> and "> prompts, but
indicates that you have begun but not completed a backtick-quoted
identifier.
了解 '>、"> 和 `> 提示符的含义非常重要,因为如果您错误地输入了未结束的字符串,您输入的任何后续行都将被 mysql 忽略——包括包含 QUIT 的行。这可能会令人相当困惑,尤其是如果您不知道需要先提供结束引号,才能取消当前查询。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. 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.