search
HomeDatabaseMysql Tutorial让你提前认识软件开发(26):数据库脚本的注释

第2部分 数据库SQL语言 数据库脚本的注释 1. 概述 注释在程序语言的编写中占有非常重要的地位。优美的、得当的注释不仅有助于研发人员理解程序,还能够提高编程效率(进而提高办事效率)。 但是,可能是由于工作比较忙的缘故,许多开发人员不重视注释的书写,

第2部分 数据库SQL语言

数据库脚本的注释

 

1. 概述

注释在程序语言的编写中占有非常重要的地位。优美的、得当的注释不仅有助于研发人员理解程序,还能够提高编程效率(进而提高办事效率)。

但是,可能是由于工作比较忙的缘故,许多开发人员不重视注释的书写,这也导致了项目交接的时候,其他开发人员理解程序困难,甚至不知道程序到底要做什么事情。因此,良好注释的书写是对一个开发人员的基本要求,大家一定要重视。

对于脚本的注释,建议大家一律采用英文,这样可以体现出国际化、专业性与规范性。

2. 数据库脚本文件头部的注释

很多脚本文件都没有头部的注释,大家认为它不重要。但作者认为一定要把这部分内容加上,这样为以后追踪版本信息提供了方便。

在文件头部的注释中,要包括版权、数据库类型、创建日期、作者、修改记录等信息,可以采用以下的样式:

--*********************************************************************

-- copy right (C)2014, company name.

-- DB Type: XXX

-- Content: XXX

-- Created: YYYY.MM.DD

-- Modify1: The name of the author

-- Date1: YYYY.MM.DD

-- version1: The original version of the product

-- Modify2: The name of who modified the file

-- Date2: YYYY.MM.DD

-- version2: The updated version of the product

--**********************************************************************

3. 数据库脚本文件摘要信息的注释

在头部注释之后,不要马上就开始创建表及存储过程,而应该有一个摘要。如果是建表脚本,摘要就是该文件中包括的表的名称和用途;如果是创建存储过程的脚本,摘要就是该文件中包括的存储过程的名称和用途。这个摘要可以起到索引的作用,帮助开发人员了解脚本文件的主要内容。

摘要信息的注释可以采用以下的样式:

--********* XXX(Version)DataBase Table Creating*********

--* 1 table1 : description1

--* 2 table2 : description2

--* 3 table3 : description3

. . . . . .

--***************************************************

4. 表或存储过程开头处的注释

在表或存储过程的开头处添加注释,可以起到方便定位、易于查阅的作用。可以采用以下的样式:

-- XXX(The name of the table or procedure, and what it is used for)

The definition of the table or procedure

5. 表的各字段之后的注释

在定义了一个表的各字段之后,需要对每个字段进行注释,以方便研发人员了解其作用,避免猜测和错误理解。这样,使用起来也会得心应手。

表的定义及字段注释可以采用以下的样式:

create table tb_XXX

(

AAA int not null, -- description1

BBB varchar(256) not null, -- description2

CCC int default(0) null, -- description3

DDD varchar(256) default('''') null, -- description4

. . . . . .

)

6. 存储过程的注释

一般说来,存储过程包括的SQL语句比较多,因此注释也会比较的复杂。即便是这样,在一些关键语句的地方,一定要有注释,否则其他开发人员阅读起来就会比较费劲。

存储过程的编写及注释可以采用以下的样式(以Sybase数据库中的语法为例):

create procedure pr_XXX

@AAA varchar(30), -- description1

@BBB int, -- description2

. . . . . .

as

begin

declare

@CCC int, -- description3

@DDD varchar(100), -- description4

. . . . . .

. . . . . .

-- YYY(name) add YYYYMMDD for ZZZ begin

. . . . . .

-- YYY(name) add YYYYMMDD for ZZZ end

. . . . . .

statement1 -- YYY add YYYYMMDD description5

. . . . . .

statement2 -- YYY modify YYYYMMDD description6

. . . . . .

statement3 -- YYY delete YYYYMMDD description7

. . . . . .

. . . . . .

statement4 -- description8(important statement)

. . . . . .

end

7. 有关注释的一些规则和建议

(1) 统一使用“--”进行注释(不要使用“/* */进行注释”)。

(3) 每段完成一定功能的脚本前(如创建数据表、存储过程、任务、插入缺省记录等),均应有注释说明。

(4) 创建数据表中每个字段后应有注释,说明字段含义,有些还需要说明取值范围等。

(5) 创建存储过程和函数中每个输入输出参数后应有注释,说明参数含义,有些还需要说明取值范围等。

(6) 对分支语句(包括条件分支)、循环语句等要编写注释。

(7) 保证代码和注释的一致性。修改代码同时修改相应的注释,不再有用的注释要删除。

(8) 注释应与其描述的代码相近,对代码的注释应放在其上方或右方(对数据表中字段和存储过程参数的注释)相邻位置,不可放在下面,如放于上方则需与其上面的代码用空行隔开。

(9) 注释与所描述代码进行同样的缩排。

(10) 中文版本的注释统一使用中文描述,海外版本的注释统一使用英文描述。

(11) 通过对函数或过程、变量等正确的命名以及合理地组织代码结构,使代码成为自注释的。

(12) 尽量避免在注释中使用缩写,特别是不常用缩写。

8. 总结

注释的作用是锦上添花,不恰当的注释不但不能够起到应有的作用,反而有可能让人产生误解。因此,我们在添加脚本文件注释的时候,一定要遵循简单、清晰、明了、通俗易懂的原则。

(本系列文章每周更新两篇,敬请期待!本人微博:http://weibo.com/zhouzxi?topnav=1&wvr=5,微信号:245924426,欢迎关注!)

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
MySQL: BLOB and other no-sql storage, what are the differences?MySQL: BLOB and other no-sql storage, what are the differences?May 13, 2025 am 12:14 AM

MySQL'sBLOBissuitableforstoringbinarydatawithinarelationaldatabase,whileNoSQLoptionslikeMongoDB,Redis,andCassandraofferflexible,scalablesolutionsforunstructureddata.BLOBissimplerbutcanslowdownperformancewithlargedata;NoSQLprovidesbetterscalabilityand

MySQL Add User: Syntax, Options, and Security Best PracticesMySQL Add User: Syntax, Options, and Security Best PracticesMay 13, 2025 am 12:12 AM

ToaddauserinMySQL,use:CREATEUSER'username'@'host'IDENTIFIEDBY'password';Here'showtodoitsecurely:1)Choosethehostcarefullytocontrolaccess.2)SetresourcelimitswithoptionslikeMAX_QUERIES_PER_HOUR.3)Usestrong,uniquepasswords.4)EnforceSSL/TLSconnectionswith

MySQL: How to avoid String Data Types common mistakes?MySQL: How to avoid String Data Types common mistakes?May 13, 2025 am 12:09 AM

ToavoidcommonmistakeswithstringdatatypesinMySQL,understandstringtypenuances,choosetherighttype,andmanageencodingandcollationsettingseffectively.1)UseCHARforfixed-lengthstrings,VARCHARforvariable-length,andTEXT/BLOBforlargerdata.2)Setcorrectcharacters

MySQL: String Data Types and ENUMs?MySQL: String Data Types and ENUMs?May 13, 2025 am 12:05 AM

MySQloffersechar, Varchar, text, Anddenumforstringdata.usecharforfixed-Lengthstrings, VarcharerForvariable-Length, text forlarger text, AndenumforenforcingdataAntegritywithaetofvalues.

MySQL BLOB: how to optimize BLOBs requestsMySQL BLOB: how to optimize BLOBs requestsMay 13, 2025 am 12:03 AM

Optimizing MySQLBLOB requests can be done through the following strategies: 1. Reduce the frequency of BLOB query, use independent requests or delay loading; 2. Select the appropriate BLOB type (such as TINYBLOB); 3. Separate the BLOB data into separate tables; 4. Compress the BLOB data at the application layer; 5. Index the BLOB metadata. These methods can effectively improve performance by combining monitoring, caching and data sharding in actual applications.

Adding Users to MySQL: The Complete TutorialAdding Users to MySQL: The Complete TutorialMay 12, 2025 am 12:14 AM

Mastering the method of adding MySQL users is crucial for database administrators and developers because it ensures the security and access control of the database. 1) Create a new user using the CREATEUSER command, 2) Assign permissions through the GRANT command, 3) Use FLUSHPRIVILEGES to ensure permissions take effect, 4) Regularly audit and clean user accounts to maintain performance and security.

Mastering MySQL String Data Types: VARCHAR vs. TEXT vs. CHARMastering MySQL String Data Types: VARCHAR vs. TEXT vs. CHARMay 12, 2025 am 12:12 AM

ChooseCHARforfixed-lengthdata,VARCHARforvariable-lengthdata,andTEXTforlargetextfields.1)CHARisefficientforconsistent-lengthdatalikecodes.2)VARCHARsuitsvariable-lengthdatalikenames,balancingflexibilityandperformance.3)TEXTisidealforlargetextslikeartic

MySQL: String Data Types and Indexing: Best PracticesMySQL: String Data Types and Indexing: Best PracticesMay 12, 2025 am 12:11 AM

Best practices for handling string data types and indexes in MySQL include: 1) Selecting the appropriate string type, such as CHAR for fixed length, VARCHAR for variable length, and TEXT for large text; 2) Be cautious in indexing, avoid over-indexing, and create indexes for common queries; 3) Use prefix indexes and full-text indexes to optimize long string searches; 4) Regularly monitor and optimize indexes to keep indexes small and efficient. Through these methods, we can balance read and write performance and improve database efficiency.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment