第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,欢迎关注!)

InnoDBBufferPool reduces disk I/O by caching data and indexing pages, improving database performance. Its working principle includes: 1. Data reading: Read data from BufferPool; 2. Data writing: After modifying the data, write to BufferPool and refresh it to disk regularly; 3. Cache management: Use the LRU algorithm to manage cache pages; 4. Reading mechanism: Load adjacent data pages in advance. By sizing the BufferPool and using multiple instances, database performance can be optimized.

Compared with other programming languages, MySQL is mainly used to store and manage data, while other languages such as Python, Java, and C are used for logical processing and application development. MySQL is known for its high performance, scalability and cross-platform support, suitable for data management needs, while other languages have advantages in their respective fields such as data analytics, enterprise applications, and system programming.

MySQL is worth learning because it is a powerful open source database management system suitable for data storage, management and analysis. 1) MySQL is a relational database that uses SQL to operate data and is suitable for structured data management. 2) The SQL language is the key to interacting with MySQL and supports CRUD operations. 3) The working principle of MySQL includes client/server architecture, storage engine and query optimizer. 4) Basic usage includes creating databases and tables, and advanced usage involves joining tables using JOIN. 5) Common errors include syntax errors and permission issues, and debugging skills include checking syntax and using EXPLAIN commands. 6) Performance optimization involves the use of indexes, optimization of SQL statements and regular maintenance of databases.

MySQL is suitable for beginners to learn database skills. 1. Install MySQL server and client tools. 2. Understand basic SQL queries, such as SELECT. 3. Master data operations: create tables, insert, update, and delete data. 4. Learn advanced skills: subquery and window functions. 5. Debugging and optimization: Check syntax, use indexes, avoid SELECT*, and use LIMIT.

MySQL efficiently manages structured data through table structure and SQL query, and implements inter-table relationships through foreign keys. 1. Define the data format and type when creating a table. 2. Use foreign keys to establish relationships between tables. 3. Improve performance through indexing and query optimization. 4. Regularly backup and monitor databases to ensure data security and performance optimization.

MySQL is an open source relational database management system that is widely used in Web development. Its key features include: 1. Supports multiple storage engines, such as InnoDB and MyISAM, suitable for different scenarios; 2. Provides master-slave replication functions to facilitate load balancing and data backup; 3. Improve query efficiency through query optimization and index use.

SQL is used to interact with MySQL database to realize data addition, deletion, modification, inspection and database design. 1) SQL performs data operations through SELECT, INSERT, UPDATE, DELETE statements; 2) Use CREATE, ALTER, DROP statements for database design and management; 3) Complex queries and data analysis are implemented through SQL to improve business decision-making efficiency.

The basic operations of MySQL include creating databases, tables, and using SQL to perform CRUD operations on data. 1. Create a database: CREATEDATABASEmy_first_db; 2. Create a table: CREATETABLEbooks(idINTAUTO_INCREMENTPRIMARYKEY, titleVARCHAR(100)NOTNULL, authorVARCHAR(100)NOTNULL, published_yearINT); 3. Insert data: INSERTINTObooks(title, author, published_year)VA


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

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.

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Dreamweaver CS6
Visual web development tools