Mysql 简明教程

MySQL - Comments

The MySQL Comments

MySQL 注释是添加到代码片段中的文本说明,旨在提供关于代码的更多信息。注释不适合作为代码的一部分执行。它充当注释,让读者(包括开发人员)了解代码的用途、功能或任何其他相关详细信息。

The MySQL Comment is a textual explanation added to a piece of code to provide additional information about the code. Comments are not meant to be executed as part of the code. It serve as notes for readers, including developers, to understand the purpose of the code, functionality, or any other relevant details.

MySQL 中有两种类型的注释: Single-line commentsMulti-line comments

There are two types of comments in MySQL: Single-line comments and Multi-line comments

The MySQL Single Line Comments

单行注释用于在单行中进行简要说明。要在 MySQL 中创建单行注释,请使用两个连字符 (--),后跟您的注释文本。

Single-line comments are used for brief explanations on a single line. To create a single-line comment in MySQL, use two hyphens (--) followed by your comment text.

Example

在以下查询中,我们使用单行注释来编写文本。

In the following query, we are using a single line comment to write a text.

SELECT * FROM customers; -- This is a comment

The MySQL Multi-line Comments

MySQL 中的多行注释用于进行较长的说明或注释掉多行代码。这些注释以 / 开始,以 / 结束。它们之间的所有内容都被视为注释。

Multi-line comments in MySQL are used for longer explanations or to comment out multiple lines of code. These comments start with / and end with /. Everything between them is considered a comment.

Example

以下示例使用多行注释作为查询的说明:-

The following example uses multi-line comment as an explanation of the query −

/*
This is a multi-line comment.
You can use it to explain complex queries or comment out multiple lines of code.

SELECT *
FROM products
WHERE price > 50;
*/

Where to Place Comments

您几乎可以在您的 SQL 代码中的任何位置放置注释。常见位置包括 −

You can place comments almost anywhere in your SQL code. Common places include −

  1. Before or after a SQL statement.

  2. Within a SQL statement to explain a specific part of it.

  3. At the beginning of a script or stored procedure to describe its purpose.

-- This is a comment before a query
SELECT * FROM orders;

SELECT /* This is an inline comment */ customer_name
FROM customers;

/* This is a comment block at the beginning of a script */
DELIMITER //
CREATE PROCEDURE CalculateDiscount(IN product_id INT)
BEGIN
    -- Calculate discount logic here
END //
DELIMITER ;

Comments Using a Client Program

我们还可以使用客户端程序注释任何值。

We can also comment any value using the client program.

Syntax

Example

以下是这些程序 −

Following are the programs −