Writing rules for Database Programming

Source: Internet
Author: User

Database ProgrammingIf there is an uglyWriting specificationsNot only is the readability poor, but it also gives people a sense of reverence, that is, you are a hero, and good writing standards give people a pleasant and artistic experience. This article introduces the database programming and writing specifications for your reference.

Case Sensitive
Rules

All Database keywords and reserved words are in uppercase; the case-sensitive style of fields and variables is described in 1.4.

Indent Style
Rules

The program block is strictly written in the indent style to ensure that the Code is clear and easy to read, the style is consistent, and the number of indentions is 2/4.

Space is required, and the TAB key is not allowed. The program layout is not neat because the number of spaces set by the TAB key is different when you use different editors to read programs.

Rules

When a statement occupies more than one row, other keywords in each row are right aligned with those in the first row.

IF flag = 1 THEN

SELECT username -- four spaces are indented from the same line.

INTO vuserinfo -- right alignment between INTO and SELECT

FROM userinfo -- FROM align right with SELECT

WHERE userid =: iuserid; -- right alignment between WHERE and SELECT

End if;

Space and line feed
Rules

Multiple statements cannot be written in one row, that is, only one statement can be written in one row.

Rules

To avoid writing complex SQL statements to the same row, we recommend that you use a line break between keywords and predicates.

Rules

Empty lines must be added between relatively independent program blocks.

In and END are independent rows

Rules

Long expressions should wrap lines at low-priority operators. Operators or keywords should be placed at the beginning of a new line. The new lines should be properly indented to make the layout neat and the statement readable.

We recommend that you use parentheses to isolate different types of operators to make the code clearer.

Rules

Reduce the number of checks for control statements, such as when... In ELSE control statements, if the most common conditions are met, try to be prefixed to be checked.

DECLARE

-- Define local variables

VFlag VARCHAR2 (10); -- judge the flag

...

BEGIN

IF (a = B AND a = c AND a = d) OR -- break the line at OR to make the logic clearer

(A = e AND e = f) THEN

-- Process something

IF vFlag = 1 THEN -- vFlag = 1 is a frequently occurring condition, which can effectively reduce the number of checks.

-- Process something

ELSIF vFlag = 2 THEN -- vFlag = 2

-- Process something

ELSE

-- Process something

End if;

Others
Rules

Do not use the SELECT * Statement. Do not use * instead of all fields. A field list should be provided to avoid unrecognized applications when the table structure changes.

Rules

The INSERT statement must provide a field list to avoid compilation errors when the table structure changes.

Rules

When multiple tables are involved in a PL/SQL or SQL statement, aliases are always used to specify the table name and field name, which makes it easier for others to read and avoid fuzzy reference, the table name and field name can be clearly identified in the alias.

Rules

Make sure that the type and length of the variables and parameters match the type and length of the table data column. Note: If the column width does not match the width of a table data column, an exception occurs when a wide or large data is imported.

DECLARE

-- Define related table field variables

VDeptNo salary. Deptno % type; -- not VARCHAR2 (10) to adapt to changes

VEmployeeNo salary. EmployeeNo % type; -- not VARCHAR2 (10) to adapt to changes

VSalary salary. Salary % type; -- not NUMBER to adapt to changes

BEGIN

-- Process something

END;

This article is suitable for beginners to learn and is the most basic knowledge of data writing. I hope the content described above will be helpful to you.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.