Sql 简明教程
SQL - Comments
注释是编程语言中使用的一段文本,用于提供附加信息。编译器会忽略这些注释,而且它们不会影响程序的功能。它们在代码执行后在输出中不可见。它们的目的是使人类更容易更清楚地理解源代码。
A comment is a piece of text that is used in programming languages to provide additional information. These comments are ignored by the compiler and do not affect the program’s functionality. They are not visible in the output after the execution of code. Their purpose is to make the source code easier for human to understand more clearly.
SQL Comments
在 SQL 中,注释可用于解释查询中某个特定部分,或者用于跳过对语句的执行。因此,每当代码行在程序中标记为注释时,就不会对其执行。
In SQL, comments can be used to explain a particular section of a query; or to skip the execution of statement. So, whenever a line of code is marked as a comment in a program, it is not executed.
MySQL 数据库中使用两种类型的注释,如下所示:-
There are two types of comments used in MySQL database, they are as follows −
-
Single-line comments
-
Multi-line comments
Single Line Comments
SQL 单行注释以两个连续连字符 (即 --) 开头,并且一直延续到该行的末尾。连字符之后的文本不会被执行。
The SQL single line comments starts with two consecutive hyphens (i.e. --) and extends to the end of the line. The text after the hyphens will not be executed.
Syntax
下面是 SQL 单行注释的语法:-
Following is the syntax of SQL single line comment −
-- This is a single-line comment
Example
在以下查询中,我们使用单行注释来编写文本:-
In the following query, we are using a single line comment to write a text −
-- Will fetch all the table records
SELECT * from table;
Example
在这里,我们有一个从名为 CUSTOMERS 的表中检索数据的 SELECT 语句。虽然我们在这个语句中有 ORDER BY 子句,但是由于对那部分进行了注释,因此此查询只是检索 CUSTOMERS 表中的记录,而不会对结果进行排序:-
Here, we have a SELECT statement that retrieves data from a table named CUSTOMERS. Though we have an ORDER BY clause in this statement, since we have commented that part, this query just retrieves the records in the CUSTOMERS table without sorting the result −
SELECT * FROM CUSTOMERS -- ORDER BY NAME ASC;
Multi-Line Comments
SQL 多行注释用于注释掉多行或一段 SQL 代码。它以 / 开头,以 / 结尾。这些定界符 ( … /) 之间的全部文本将被忽略,并被视为注释。
The SQL multi line comments are used to comment out multiple lines or a block of SQL code. It starts with / and ends with /. Entire text between these delimiters (/…/) will be ignored and considered as a comment.
Syntax
下面是 SQL 多行注释的语法:-
Following is the syntax of SQL multi line comments −
/* This is a
multi-line
comment */
Example
以下示例使用多行注释作为查询的说明:-
The following example uses multi-line comment as an explanation of the query −
/*following query
will fetch all the
table records./*
SELECT * from CUSTOMERS;