mysql 注释
在本文中,我们将介绍 mysql 注释。我们还将了解应该在哪里使用什么类型的注释。
注释写的都是描述代码,方便理解。我们还使用注释在解析 sql 查询时忽略代码的某一部分(不要让那段代码被执行)。
它还可以帮助其他程序员了解代码中发生的事情。我们将看到如何在 mysql 中使用单行和多行注释。我们还将了解可执行注释及其用途。
有三种不同的方式来编写 mysql 注释。我们可以使用 #
、-
或/*
和*/
符号来注释。
mysql 单行注释
我们可以通过两种方式使用 mysql 单行注释,使用 #
或 -
。
让我们创建一个名为 tb_teachers
的表来练习 mysql 注释。我们使用 #
在下面给出的 mysql 查询中编写单行注释。
此注释用在 sql
查询的末尾,并且后面必须有一个换行符。如果你在注释后不使用换行符,则同一行中的所有内容都将被注释,直到遇到换行符。不需要在 #
之后放置空格。但是在 #
后面留一个空格来增加可读性是一个好方法。
使用 #
的 mysql 示例代码:
#followingsqlquerywillcreateatablenamed'tb_teachers'createtable`practice_mysql_comments`.`tb_teachers`(teacher_idintegernotnull,first_namevarchar(30)notnull,last_namevarchar(30)notnull,gendervarchar(30)notnull,cityvarchar(64)notnull,emailvarchar(64)notnull,joining_yearintegernotnull,primarykey(teacher_id));
请参阅以下代码示例,其中我们在使用单行注释后不放置换行符。你可以看到 create
命令也被部分注释。
#followingsqlquerywillcreateatablenamed'tb_teachers'createtable`practice_mysql_comments`.`tb_teachers`(teacher_idintegernotnull,first_namevarchar(30)notnull,last_namevarchar(30)notnull,gendervarchar(30)notnull,cityvarchar(64)notnull,emailvarchar(64)notnull,joining_yearintegernotnull,primarykey(teacher_id));
我们也可以在 sql 查询中使用这种类型的注释。请参阅以下代码;你可以观察到我们可以在 sql 查询中使用 #
进行注释。
#followingsqlquerywillcreateatablenamed'tb_teachers'createtable`practice_mysql_comments`.`tb_teachers`(teacher_idintegernotnull,#teacher's id
first_name varchar(30) not null, # teacher'sfirstnamelast_namevarchar(30)notnull,#teacher's last name
gender varchar(30) not null, # teacher'sgendercityvarchar(64)notnull,#teacher's home town
email varchar(64) not null, # teacher'semailjoining_yearintegernotnull,#teacher's appointment year
primary key (teacher_id) # primay key of the teacher'stable);
让我们探索使用 -
符号的另一种单行注释方式。下面的 sql 代码表明,即使在 sql 查询中,我们也可以使用双 -
(破折号)符号进行注释。
这与我们使用 #
符号进行注释相同,只有一个区别。我们必须在第二个破折号之后放置至少一个空格;否则,它不会作为注释。
-- following sql query will create a table named 'tb_students'
createtable`practice_mysql_comments`.`tb_students`(student_idintegernotnull,-- student's id
first_namevarchar(30)notnull,-- student's first name
last_namevarchar(30)notnull,-- student;s last name
gendervarchar(30)notnull,-- student's gender
cityvarchar(64)notnull,-- student's home town
emailvarchar(64)notnull,-- student's email
registration_yearintegernotnull,-- student's registration year
primarykey(student_id)-- primay key of the student's table
);
mysql 多行注释
如果我们需要详细解释 sql 查询以使代码易于理解,我们使用多行注释。 /* */
中的所有内容都将被忽略。是否在此类注释的末尾放置换行符都没关系,但最好有一个换行符来编写干净且有条理的代码。
/*
following sql query will create a table named 'tb_students'
having the basic information about the students. this information includes
full name, gender, city, email and registration year.
*/createtable`practice_mysql_comments`.`tb_students`(student_idintegernotnull,first_namevarchar(30)notnull,last_namevarchar(30)notnull,gendervarchar(30)notnull,cityvarchar(64)notnull,emailvarchar(64)notnull,registration_yearintegernotnull,primarykey(student_id));
虽然我们可以把多行注释当成单行来处理,但是为什么我们有单行注释还要增加工作量呢。当你不想执行多于一行时,最好使用多行注释。请参阅以下示例。
/*
following sql query will create a table named 'tb_students'
having the basic information about the students. this information includes
full name, gender, city, email and registration year.
*/createtable`practice_mysql_comments`.`tb_students`(student_idintegernotnull,first_namevarchar(30)notnull,last_namevarchar(30)notnull,gendervarchar(30)notnull,cityvarchar(64)notnull,/* email varchar(64) not null,
registration_year integer not null, */primarykey(student_id));
mysql 可执行文件注释
mysql 还支持可执行注释。这种类型的注释为你提供了在各种数据库之间的可移植性。这些注释中编写的代码只会在 mysql 中执行。如果你在 !
之后指定版本字符,它只适用于那个特定的 mysql 版本 或更高版本。
可执行注释的示例代码:
select3/*! *2 */asmultiplication;
结论
我们已经得出结论,mysql 注释在编程时起着至关重要的作用。它是什么类型的编程并不重要。如果你必须忽略多行,使用多行注释是一个不错的选择;否则,单行注释就可以了。如果你希望仅在 mysql 服务器上执行某段代码,则可以使用 mysql 可执行注释。
转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处
本文地址:
相关文章
如何在 mysql 中声明和使用变量
发布时间:2024/03/26 浏览次数:115 分类:mysql
-
当你需要在 mysql 中的脚本中存储单个值时,最好的方法是使用变量。变量有不同的种类,有必要知道何时以及如何使用每种类型。
发布时间:2024/03/26 浏览次数:176 分类:mysql
-
本教程演示了如何在 mysql 中重置自动增量。
在 mysql 中使用 mysqladmin 刷新主机解除阻塞
发布时间:2024/03/26 浏览次数:82 分类:mysql
-
你将了解阻止主机的原因。此外,通过使用 phpmyadmin 和命令提示符刷新主机缓存来解除阻塞的不同方法和效果。
发布时间:2024/03/26 浏览次数:199 分类:mysql
-
本教程演示如何在 mysql 中转换为整数。