Oracle PL/SQL programming specification Guide

Source: Internet
Author: User

I. PL/SQL programming specification in case

Just like in SQL, PL/SQL is case-insensitive. The general principles are as follows:

Keywords (BEGIN, EXCEPTION, END, if then else, LOOP, end loop), data type (VARCHAR2, NUMBER), internal function (LEAST, SUBSTR) and user-defined subroutines (procedures, functions, packages), in upper case.

Variable name, column name and table name in SQL, in lower case.

Ii. Gaps in PL/SQL programming specifications

Blank blank lines and spaces) are equally important in PL/SQL as they are in SQL, because they are an important factor in improving code readability. In other words, you can use indentation in the Code to reflect the logical structure of the program. The following are some suggestions:

Leave a space at the left and right of the equal sign or comparison operator;

DECLARE, BEGIN, EXCEPTION, END, IF and end if, LOOP and end loop) are arranged on the left. In addition, the nested structure in the structure should be indented with three spaces using the space key instead of the Tab key );

The main code segments are separated by blank lines;

Separate different logical parts of the same structure into independent rows, even if the structure is short. For example, IF and THEN are placed in the same row, while ELSE and end if are placed in independent rows.

Iii. Naming Conventions for PL/SQL programming specifications

Using the following prefix is helpful to avoid conflicts with keywords and table names:

V _ variable name

Con _ constant name

I _ input parameter name, o _ output parameter name, io _ input and output parameter name

C _ cursor name or cursor name_cur

Rc _ Ref Cursor name

R_Record name or Record name_rec

FOR r_stud IN c_stud LOOP...

FOR stud_rec IN stud_cur LOOP

Type _ name, name_type (user-defined type)

T _ table name, table name_tab PL/SQL table)

Rec_Record name, Record name_rec Record variable)

E _ User-defined exception)

The package name should describe the main functions of the stored procedures and functions in the package.

The name of the stored procedure should describe the action performed by the stored procedure.

The function name should describe the returned variable

For example:

PACKAGE student_admin

-The admin suffix may be used to indicate the management function.

PROCEDURE remove_student (I _student_id IN student. studid % TYPE );

FUNCTION student_enroll_count (I _student_id student. studid % TYPE)

Return integer;

Iv. Notes on PL/SQL programming specifications

Annotations in PL/SQL are as important as those in SQL. They should explain the main parts of the program and all the key logical steps.

Use single line comment (-) instead of multi-line comment (/*). Even if PL/SQL processes these comments as well, it is easier to debug the code after the code is completed, because you cannot embed multi-line comments in multi-line comments. In other words, you can partially cancel comments in a single line comment code, but not in multiple lines of comments code.

5. Other suggestions

For SQL statements embedded in PL/SQL, use the same formatting guide to determine how these statements should appear in the code block.

A header comment is provided to describe the purpose of the code block and list the creation date and author name. Each revision has a line of comment, including the author name, date, and revision description.

For example, the following example shows the above suggestions. Please note that this example also uses the same width font Courier New), because each font occupies the same width can make formatting easier. Proportional space fonts hide spaces, making it difficult to align between lines. Most text and program Editors Use an equal-width font by default.

 
 
  1. REM *************************************** *****************
  2. REM * file name: coursediscount01. SQL
  3. REM * version: 1
  4. REM * purpose: discounts are given for at least a portion of courses registered with more than 10 students
  5. REM * parameter: None
  6. REM *
  7. REM * Author: s. tashi time: 2000.1.1
  8. REM * modifier: y. sonam time: 2000.2.1
  9. REM * Description: modifies the cursor and adds indentation and comments.
  10. REM *************************************** *****************
  11. DECLARE 
  12. -- C_DISCOUNT_COURSE: Find the courses with at least a part of more than 10 student registrations 
  13. CURSORC_discount_courseIS 
  14. SELECT DISTINCTCourse_no
  15. FROM SectionSect
  16. WHERE10 <= (SELECT COUNT(*)
  17. FROMEnrollment enr
  18. WHEREEnr. section_id = sect. section_id
  19. );
  20. -- Discount rate of courses with fees exceeding $2000.00 
  21. Con_discount_2000 constant number: =. 90;
  22. -- Discount rate of the course between $1001.00 and $2000.00 
  23. Con_discount_other constant number: =. 95;
  24. V_current_course_cost course. cost % TYPE;
  25. V_discount_all NUMBER;
  26. E_update_is_problematic EXCEPTION;
  27. BEGIN 
  28. -- Determine the current fee and new fee for the courses to be discounted 
  29. FORR_discount_courseInC_discount_course LOOP
  30. SELECTCost
  31. INTOV_current_course_cost
  32. FROMCourse
  33. WHERECourse_no = r_discount_course.course_no;
  34. IF v_current_course_cost> 2000THEN 
  35. V_discount_all: = con_discount_2000;
  36. ELSE 
  37. IF v_current_course_cost> 1000THEN 
  38. V_discount_all: = con_discount_other;
  39. ELSE 
  40. V_discount_all: = 1;
  41. ENDIF;
  42. ENDIF;
  43. BEGIN 
  44. UPDATECourse
  45. SETCost = cost * v_discount_all
  46. WHERECourse_no = r_discount_course.course_no;
  47. EXCEPTION
  48. WHENOTHERSTHEN 
  49. RAISE e_update_is_problematic;
  50. END;-- The subcode block of the update Record ends. 
  51. ENDLOOP;-- The main cycle ends. 
  52. COMMIT;
  53. EXCEPTION
  54. WHENE_update_is_problematicTHEN 
  55. -- Transaction rollback 
  56. ROLLBACK;
  57. DBMS_OUTPUT.PUT_LINE
  58. ('There was a problem updating a course cost .');
  59. WHENOTHERSTHEN 
  60. NULL;
  61. END;
  62. /
  1. Connection Methods and common usage of Oracle database tables
  2. Application Analysis of Oracle recovery Manager
  3. Five restrictions on using Oracle External tables
  4. MySQL left connection, right connection, and internal connection
  5. In-depth exploration of database connection Performance

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.