2nd part Database SQL language
Layout of database script files
"No rules, inadequate surrounding area", the code layout of the canonical database script file has the following significance:
(1) can display the code good logical structure, improves the code accuracy, the continuity, the readability and the maintainability.
(2) It is helpful to improve the quality and efficiency of product development and reduce the development cost.
(3) for developers, developing a good scripting habit can improve the level of individual database scripting and improve the efficiency of script programming.
Visible, unified, good script code layout and style is not only an individual subjective aesthetic or formal problems, but also affect the quality of the product, and related to the improvement of their own script programming ability.
1. Overview of script layout order
In the actual software development project, the Uniform Specification script Layout order can facilitate the reading of the Code, facilitate the subsequent maintenance and test modification. There are two ways to lay out the contents of a script file:
Way One:
File header
Initialization
Establishment of user and access rights
Creation of data tables
Creation of stored procedures
Creation of database Tasks
End
Way two:
File header
Initialization
Creation of data tables
Creation of stored procedures
Creation of database Tasks
Establishment of user and access rights
End
Attention:
(1) an ordinary script file contains the 7 parts of mode one and mode two. To make it easier to distinguish and read, each section is separated by a comment block ( the annotation uses "-").
(2) "The establishment of user and access rights" can be placed in the third part, or in the sixth part.
(3) each part of the uniform use of the annotated description, the relevant content must be written in the corresponding section, if the relevant content is empty, you must also retain the part of the labeling instructions.
2. Script Layout Example
As a follow-up example, the script code implementation based on the Sybase database is as follows:
--*********************************************************************
-- Copyright (C), Zhou Zhaoxiong.
-- database version: Sybase ASE Enterprise 15.0
-- Content Summary: script file Layout example
-- Creator: Zhou zhaoxiong
-- completion date: 20140616
-- change record 1:
-- Date Modified:
-- version number:
-- modified by:
-- Modify the content:
--**********************************************************************/
--**************************************************
--Initialization initialization
--**************************************************
Use master
Go
use xxx -- The database to be used
Go
Checkpoint
Go
dump tran xxx with no_log-- log truncation processing
Go
--************************************************************
-- establishment of user and right creation users and permissions
--************************************************************
exec sp_addalias xxx, dbo
Go
--***********************************************************************
-- creation of table creation datasheet
--***********************************************************************
-- sample table tb_example
if exists (select 1 from sysobjects where id = object_id (' tb_example '))
Begin
drop table Tb_example
End
Go
CREATE TABLE Tb_example
(
name varchar (+) NOT NULL,-- name
Age Int. NOT NULL-- ages
)
Go
-- Create an index
Create INDEX Idx_tb_example1 on tb_example (name)
Go
--************************************************************
-- creation of procedure creation stored procedures
--************************************************************
-- information query stored procedure pr_selectinfo
-- input parameters:@v_name name,@v_age age
-- output parameter: None
if exists (select 1 from sysobjects where id = object_id (' Pr_selectinfo '))
Begin
drop procedure Pr_selectinfo
End
Go
CREATE PROCEDURE Pr_selectinfo
@v_name varchar (in),-- name
@v_age INT-- Age
As
Begin
......
End
Go
print ' CREATE PROCEDURE pr_selectinfo OK '
Go
--**************************************************
--Task creation creation of database tasks
--**************************************************
......
--**************************************************
--Finalization end
--**************************************************
......
In the actual software project, the layout of the script file has its strict rules, developers need to follow the programming specifications to write script code. This not only facilitates the reading and modification of the code, but also facilitates the technical exchange and sharing of the team.
(I Weibo: Http://weibo.com/zhouzxi?topnav=1&wvr=5, No.: 245924426, welcome attention!) )