SQL is a defacto language used to manage, query, select, filter data in a relational database. Similar to the programming languages SQL provides comments in order to make the SQL query or clause more readable and put some note.
SQL是一种事实上的语言,用于管理,查询,选择,过滤关系数据库中的数据。 与编程语言类似,SQL提供注释以使SQL查询或子句更具可读性并加一些注释。
(Single-Line Comments)
We will start with single-line comments. Double dash --
is used to create a single line comment. We can put the double dash at the start of the line which we want to comment on.
我们将从单行注释开始。 双破折号--
用于创建单行注释。 我们可以将双破折号放在要注释的行的开头。
-- This is single full line comment.
SELECT * FROM Users;
-- This is a comment too.
(Inline Comments)
We can also use the double dash in order to create half-line or inline comments. We will put a double dash where we want to comment to start. In this example after the SELECT query, we will put a double dash and the next part of the line will become a comment.
我们还可以使用双破折号来创建半行或内联注释。 我们将在要评论的地方加一个双破折号。 在此示例中,在执行SELECT查询之后,我们将放置一个双破折号,并且该行的下一部分将成为注释。
-- This is single full line comment.
SELECT * FROM Users; -- This is inline comment.
We can also use /*
and */
in order to create inline comments. /*
will specify the start and */
will specify the end of the inline comment.
我们还可以使用/*
和*/
来创建内联注释。 /*
将指定内联注释的开始,而*/
将指定内联注释的结束。
SELECT * FROM Users; /* This is inline comment. */
We can also comment on some SQL parts like below.
我们还可以对以下一些SQL部分进行注释。
SELECT Name, Surname /*, City */ From Users;
We can see that City
is commented and it will not returned y the SQL query.
我们可以看到City
已注释,并且不会在SQL查询中返回。
(Multi-Line Comments)
Another useful comment type is a multi-line comment where we will use /*
in order to set the start of the comment and */
for the end of the comment. Lines between /*
and */
will be assumed as a comment to like below.
另一种有用的注释类型是多行注释,其中我们将使用/*
设置注释的开头,并使用*/
设置注释的结尾。 /*
和*/
行将作为注释,如下所示。
/* This the the start and first line of the comment.
This line is comment too.
This line too.
This line is the end and last line of the comment.*/
SELECT * FROM Users;
Here is another example of the multi-line comment.
这是多行注释的另一个示例。
/*
This the the start and first line of the comment.
This line is comment too.
This line too.
This line is the end and last line of the comment.
*/
SELECT * FROM Users;
(Nested Comments)
Comments can be nested. Nesting comments can be useful when we try some different SQL sentences and enable or disable them by commenting and uncommenting.
注释可以嵌套。 当我们尝试一些不同SQL语句并通过注释和取消注释启用或禁用它们时,嵌套注释会很有用。
/*
This is a comment.
/* This is a nested comment.
*/
This is the end line of the comment.*/
(Execute Comment In MySQL)
MySQL database provides some extensions to the SQL language. We can use executable comments in order to run SQL for specific MySQL versions. e will use /*!
for the start and */
for the end of the executable comment. In the following example, we will sum the two values which will return 4.
MySQL数据库提供了对SQL语言的一些扩展。 我们可以使用可执行注释来为特定MySQL版本运行SQL。 e将使用/*!
开头和*/
表示可执行注释的结尾。 在下面的示例中,我们将对两个值求和,这些值将返回4。
SELECT 2 /*! +2 */
(Execute Comment For Specific MySQL Version)
We can also execute comments for specific MySQL version. We will specify the MySQL version only numbers without dots. and then provide the SQL like below. In the following example, we will run the comment the MySQL version 5.2.10 .
我们还可以为特定MySQL版本执行注释。 我们将只指定MySQL版本的不带点的数字。 然后提供如下所示SQL。 在以下示例中,我们将运行注释MySQL版本5.2.10。
SELECT * FROM Users;
/*!5210 KEY_BLOCK_SIZE=1024; */
LEARN MORE SQL "Select Into" Statement Usage Tutorial
了解更多SQL“选择入”语句用法教程
翻译自: https://www.poftut.com/sql-comments-tutorial-with-example/