# 注释

在本教程中，您将学习如何使用MySQL注释来记录MySQL中的SQL语句或代码块。

## 注释

注释可用于记录SQL语句的目的或存储过程中代码块的逻辑。解析SQL代码时，MySQL会忽略注释部分。 它只执行除了可执行注释之外的SQL部分，我们将在下一节讨论。

MySQL支持三种注释样式(方式)：

**样式一：** 从`--`到行尾

双重冲突注释样式至少需要在第二个破折号之后的空格或控制字符(空格，制表符，换行符等)。

```sql
SELECT * FROM users; -- This is a comment
```

请注意，标准SQL在第二个破折号后不需要空格。 MySQL使用空白来避免某些SQL构造的问题，如：

```sql
SELECT 10--1;
```

该语句返回`11`。

**样式二：** 从`--`到行尾

参考如下查询语句-

```sql
SELECT 
    lastName, firstName
FROM
    employees
WHERE
    reportsTo = 1002; # get subordinates of Diane
```

**样式三：**

C语言风格的注释`/* */`可以跨越多行。您使用此注释样式记录一个SQL代码块。

```sql
/*
    Get sales rep employees
    that reports to Anthony
*/

SELECT 
    lastName, firstName
FROM
    employees
WHERE
    reportsTo = 1143
        AND jobTitle = 'Sales Rep';
```

> 请注意，MySQL不支持嵌套注释。

## 可执行的注释

MySQL提供可执行注释来支持不同数据库之间的可移植性。这些注释允许嵌入仅能在MySQL中执行，但不能在其他数据库执行SQL代码。

下面说明了可执行注释语法：

```sql
/*! MySQL-specific code */
```

例如，以下语句使用可执行注释：

```sql
SELECT 1 /*! +1 */ ;
```

该语句返回`2`而不是1。但是，如果在其他数据库系统中执行，则返回1。

字符串`"#####"`表示可以执行注释的最小版本的MySQL。 第一个`＃`表示主要版本，例如`5`或`8`，第二个`2`个数字(`##`)是次要版本。 最后`2`个(`##`)表示补丁级别。

例如，以下注释只能在MySQL 5.1.10或更高版本中执行：

```sql
CREATE TABLE t1 (
    k INT AUTO_INCREMENT,
    KEY (k)
)  /*!50110 KEY_BLOCK_SIZE=1024; */
```

在本教程中，您已经学会了如何使用MySQL注释来记录MySQL中的SQL代码。


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://hezhiqiang-book.gitbook.io/mysql/di-er-zhang/zhu-shi.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
