Presentation is loading. Please wait.

Presentation is loading. Please wait.

Oracle 数据库应用 -- PL/SQL 进阶 (3) & Oracle DBA 2016/5/10 2016/5/10.

Similar presentations


Presentation on theme: "Oracle 数据库应用 -- PL/SQL 进阶 (3) & Oracle DBA 2016/5/10 2016/5/10."— Presentation transcript:

1 Oracle 数据库应用 -- PL/SQL 进阶 (3) & Oracle DBA 2016/5/10 2016/5/10

2 目录 1 DBA 与 Oracle DBA 2 PL/SQL 中的异常处理 3 PL/SQL 中的内存数据

3 DBA 与 Oracle DBA

4 Database, Data and System Administration  Metadata  Data Model

5 DA vs. DBA vs. SA

6 Web 应用需求挖掘

7 DBA 的任务  Database Design  Performance Monitoring and Tuning  Availability  Database Security and Authorization  Backup and Recovery  Data Integrity

8 PL/SQL 中的异常处理

9 Oracle 异常概述  PL/SQL 程序出错 (error) 时,抛出 (raise) 异常,进行处理 (Handler)  事件驱动模式,控制转移  系统错误 (System exception)  程序错误 (Programmer-defined exception)

10 相关概念  Exception section  Raising an exception  Handle (verb), handler (noun)  Scope  Propagation  Unhandled exception  Unnamed or anonymous exception  Named exception

11 Oracle 异常的定义方式  系统异常定义在 STANDART, DBMS_STANDARD package 中  异常的声明与使用 : exception_name EXCEPTION; EXIT WHEN condition; WHEN invalid_company_id THEN PROCEDURE calc_annual_sales (company_id_in IN company.company_id%TYPE) IS invalid_company_id EXCEPTION; negative_balance EXCEPTION; duplicate_company BOOLEAN; BEGIN... body of executable statements... EXCEPTION WHEN NO_DATA_FOUND -- system exception THEN... WHEN invalid_company_id THEN WHEN negative_balance THEN... END;

12 异常名与错误代码  异常名: NO_DATA_FOUND  错误代码: ORA-01843: not a valid month  EXCEPTION_INIT: PROCEDURE my_procedure IS invalid_month EXCEPTION; PRAGMA EXCEPTION_INIT (invalid_month, −1843); BEGIN... EXCEPTION WHEN invalid_month THEN

13 预定义系统异常  例子: NO_DATA_FOUND  定义在 STANDARD package  异常处理示例: WHEN NO_DATA_FOUND THEN WHEN STANDARD.NO_DATA_FOUND  系统异常定义示例: invalid_argval EXCEPTION; PRAGMA EXCEPTION_INIT(invalid_argval, −21560);  定义在 DBMS_LOB 包中,异常引用方式方式: WHEN DBMS_LOB.invalid_argval THEN...

14 系统异常举例  CURSOR_ALREADY_OPEN ORA-6511 SQLCODE = –6511 -- You tried to OPEN a cursor that was already open. You must CLOSE a cursor before you try to OPEN or re-OPEN it.  DUP_VAL_ON_INDEX ORA-00001 SQLCODE = −1 -- Your INSERT or UPDATE statement attempted to store duplicate values in a column or columns in a row that is restricted by a unique index.

15 发送 (Raising) 异常  在 PL/SQL 程序中发送异常的三种方式: 数据库发送 使用 RAISE 语句发送 使用内置函 RAISE_APPLICATION_ERROR 发送  RAISE 语句 RAISE exception_name; RAISE package_name.exception_name; RAISE;

16 发送异常示例 DECLARE invalid_id EXCEPTION; id_value VARCHAR2(30); BEGIN id_value := id_for ('SMITH'); IF SUBSTR (id_value, 1, 1) != 'X' THEN RAISE invalid_id; END IF;... END; BEGIN IF total_sales = 0 THEN RAISE ZERO_DIVIDE; -- Defined in STANDARD package ELSE RETURN (sales_percentage_calculation (my_sales, total_sales)); END IF; END;

17 发送异常示例 IF days_overdue (isbn_in, borrower_in) > 365 THEN RAISE overdue_pkg.book_is_lost; END IF; EXCEPTION WHEN NO_DATA_FOUND THEN -- Use common package to record all the "context" information, -- such as error code, program name, etc. errlog.putline (company_id_in); -- And now propagate NO_DATA_FOUND unhandled to the enclosing block. RAISE;

18 RAISE_APPLICATION_ERROR  DBMS_STANDARD package 中的定义: PROCEDURE RAISE_APPLICATION_ERROR ( num binary_integer, msg varchar2, keeperrorstack boolean default FALSE); PROCEDURE raise_by_language (code_in IN PLS_INTEGER) IS l_message error_table.error_string%TYPE; BEGIN SELECT error_string INTO l_message FROM error_table WHERE error_number = code_in AND string_language = USERENV ('LANG'); RAISE_APPLICATION_ERROR (code_in, l_message); END;

19 异常处理 (Handling)  处理流程: Exception raised  Stops regular excecution  Transfer control to exception section  Handle it or pass to the enclosing block DECLARE... declarations... BEGIN... executable statements... [ EXCEPTION] WHEN exception_name --... exception handlers... THEN executable statements WHEN OTHERS THEN executable statements END;

20 Oracle 的错误处理内置函数  SQLCODE :返回最近执行语句的错误代码  SQLERRM: 返回错误信息  DBMS_UTILITY.FORMAT_ERROR_STACK  DBMS_UTILITY.FORMAT_ERROR_BACKTRACE  SQLCODE :返回最近执行语句的错误代码  SQLERRM: 返回错误信息  DBMS_UTILITY.FORMAT_ERROR_STACK  DBMS_UTILITY.FORMAT_ERROR_BACKTRACE

21 异常处理方式  同时处理多个异常: WHEN invalid_company_id OR negative_balance THEN WHEN balance_too_low OR ZERO_DIVIDE OR DBMS_LDAP.INVALID_SESSION THEN  异常传递:

22 异常处理方式

23  考虑以下问题 :  DELETE 出错导致后续 DML 语句无法执行 ) PROCEDURE change_data IS BEGIN DELETE FROM employees WHERE... ; UPDATE company SET... ; INSERT INTO company_history SELECT * FROM company WHERE... ; END;  解决方案: ? PROCEDURE change_data IS BEGIN DELETE FROM employees WHERE... ; EXCEPTION WHEN OTHERS THEN log_error; END; BEGIN UPDATE company SET... ; EXCEPTION WHEN OTHERS THEN log_error; END; BEGIN INSERT INTO company_history SELECT * FROM company WHERE... ; EXCEPTION WHEN OTHERS THEN log_error; END;

24 异常处理方式

25 异常处理方式 --WHEN OTHERS PROCEDURE add_company ( id_in IN company.ID%TYPE, name_in IN company.name%TYPE, type_id_in IN company.type_id%TYPE ) IS BEGIN INSERT INTO company (ID, name, type_id) VALUES (id_in, name_in, type_id_in); EXCEPTION WHEN OTHERS THEN DECLARE l_errcode PLS_INTEGER := SQLCODE; BEGIN CASE l_errcode WHEN −1 THEN DBMS_OUTPUT.put_line ( 'Company ID or name already in use. ID = ' || TO_CHAR (id_in) || ' name = ' || name_in ); RAISE; WHEN −2291 THEN DBMS_OUTPUT.put_line ( 'Invalid company type ID: ' || TO_CHAR (type_id_in)); RAISE; ELSE RAISE; END CASE; END; END add_company;

26 异常处理方式 — Programming Strategy  When an error occurs in your code, obtain as much information as possible about the context in which the error was raised.  Avoid hiding errors with handlers that look like WHEN error THEN NULL; (or, even worse, WHEN OTHERS THEN NULL;)  Rely on the default error mechanisms of PL/SQL whenever possible.

27 异常处理方式 PROCEDURE read_file_and_do_stuff ( dir_in IN VARCHAR2, file_in IN VARCHAR2 ) IS l_file UTL_FILE.file_type; l_line VARCHAR2 (32767); BEGIN l_file := UTL_FILE.fopen (dir_in, file_in, 'R', max_linesize => 32767); LOOP UTL_FILE.get_line (l_file, l_line); do_stuff; END LOOP; EXCEPTION WHEN NO_DATA_FOUND THEN UTL_FILE.fclose (l_file); more_stuff_here; END; LOOP BEGIN UTL_FILE.get_line (l_file, l_line); do_stuff; EXCEPTION WHEN NO_DATA_FOUND THEN EXIT; END; END LOOP; UTL_FILE.flcose (l_file); more_stuff_here;

28 异常处理方 -- NO_DATA_FOUND & TOO_MANY_ROWS FUNCTION fullname ( employee_id_in IN employees.employee_id%TYPE) RETURN VARCHAR2 IS retval VARCHAR2 (32767); BEGIN SELECT last_name || ',' || first_name INTO retval FROM employees WHERE employee_id = employee_id_in; RETURN retval; END fullname;

29 轶事 (anecdote)  兴趣爱好转化为事业的例子: Face++ 天羽航  如何看待热门技术与项目 -- 三维打印 /VR/ 在线教育  当代社会人的价值在哪里  论 “ 面包与爱情总会有的 ”  职业转换容易吗  专业和职业的关系

30 课堂练习  编写 PL/SQL 程序实现下述功能:  查询数据库中的书籍表 如果有 ”Oracle 数据库应用 ” ,将书籍取出 ( 删除该条 目 ) 如果没有该书籍,提出输出文字 “ 该书籍不存在 ”

31 PL/SQL 中的内存数据

32  PGA (Program Global Area) ------------------- 内存数据有哪些? ------------------  Variable or constant  Scalar or composite Scalars are made up of a single value, such as a number or a string. Composite data consists of multiple values, such as a record.  Containerized Containers may contain information obtained from the database.

33 内存数据的声明与命名  使用前需要声明 --------------------- 命名规则举例 ------------------  最长 30 字符  S 以英文字符开头  可使用引文字符、数字、 $,# , _ 等特殊符号.  大小写无关 l_total_count 1st_account first_12_years email_address@business_loc favorite_ice_cream_flavors_that_dont_contain_nuts total_#_of_trees salary_in_$

34 内存数据的声明与命名  特殊用法 : DECLARE "truly_lower_case" INTEGER; " " DATE; -- Yes, a name consisting of five spaces! "123_go!" VARCHAR2(10); BEGIN "123_go!" := 'Steven'; END; ------------------- 命名建议 ------------------  Ensure that each name accurately reflects its usage and is understandable at a glance  Establish consistent, sensible naming conventions

35 PL/SQL 中的数据类型  Character Data  Numbers  Dates, Timestamps, and Intervals  Booleans  Binary Data  ROWIDs  REF CURSORs  Internet Datatypes  “Any” Datatypes  User-Defined Datatypes


Download ppt "Oracle 数据库应用 -- PL/SQL 进阶 (3) & Oracle DBA 2016/5/10 2016/5/10."

Similar presentations


Ads by Google