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

Slides:



Advertisements
Similar presentations
BD05/06 PL/SQL  Introduction  Structure of a block  Variables and types  Accessing the database  Control flow  Cursors  Exceptions  Procedures.
Advertisements

Chapter 4B: More Advanced PL/SQL Programming
PL/SQL Agenda: Basic PL/SQL block structure
Exception types In Oracle PL/SQL. Types of exceptions Named system exceptions –Raised as a result of an error in PL/SQL or RDBMS processing. Named programmer-defined.
Exception Handling in PL/SQL. POINTS TO DISCUSS What is Exception Handling Structure of Exception Handling Section Types of Exceptions.
PL/SQL block has the following structure: DECLARE Declaration statements BEGIN Executable statements EXCEPTION Exception-handling statements END ;
Introduction to PL/SQL. Procedural Language extension for SQL Oracle Proprietary 3GL Capabilities Integration of SQL Portable within Oracle data bases.
Distributed Database Applications COSC 5050 Week Three.
PL / SQL P rocedural L anguage / S tructured Q uery L anguage Chapter 7 in Lab Reference.
Bordoloi and Bock EXCEPTIONS. Bordoloi and Bock Errors Two types of errors can be found in a program: compilation errors and runtime errors. There is.
Cursor and Exception Handling By Nidhi Bhatnagar.
Oracle10g Developer: PL/SQL Programming1 Objectives Manipulating data with cursors Managing errors with exception handlers Addressing exception-handling.
Chapter 4 Cursors and Exception Handling Oracle10g Developer:
Exceptions Oracle Database PL/SQL 10g Programming Chapter 7.
EE Copyright س Oracle Corporation, All rights reserved. ® Review of PL/SQL.
Lecture 4 PL/SQL language. PL/SQL – procedural SQL Allows combining procedural and SQL code PL/SQL code is compiled, including SQL commands PL/SQL code.
INTRODUCTION TO PL/SQL. Class Agenda Introduction Introduction to PL/SQL Declaring PL/SQL Variable Creating the Executable Section Interacting with the.
1 Copyright © 2004, Oracle. All rights reserved. Introduction to PL/SQL.
Overview · What is PL/SQL · Advantages of PL/SQL · Basic Structure of a PL/SQL Block · Procedure · Function · Anonymous Block · Types of Block · Declaring.
Distributed Database Applications COSC 5050 Week One.
PL/SQL A BRIEF OVERVIEW DAVID WILSON. PL/SQL User’s Guide and Reference PL/SQL User’s Guide and Reference.
L/O/G/O Working with Composite Data Types. Objectives After completing this lesson, you should be able to do the following: –Create user-defined PL/SQL.
CS178 Database Management PL/SQL session 8 References: ORACLE 9i PROGRAMMING A Primer Rajshekhar Sunderraman.
CSIT 313 DB PROGRAMMING EXCEPTION HANDLING. In PL/SQL, an error condition is called an exception. An exception can be either –internally defined (by the.
PL/SQL Oracle's Database Programming Language. Remember: Set serveroutput on With serveroutput off (default) executing procedure: With serveroutput on:
Oracle PL/SQL. PL/SQL Originally modeled after ADA Originally modeled after ADA Created for Dept. of DefenseCreated for Dept. of Defense Allows expanded.
1 Copyright © 2004, Oracle. All rights reserved. Introduction to PL/SQL.
Trapping Oracle Server Exceptions. 2 home back first prev next last What Will I Learn? Describe and provide an example of an error defined by the Oracle.
PL/SQL Block Structure DECLARE - Optional Variables, cursors, user-defined exceptions BEGIN - Mandatory SQL Statements PL/SQL Statements EXCEPTIONS - Optional.
Distributed Database Applications COSC 5050 Week Five.
Database Application Development using PL/SQL Programming.
Copyright  Oracle Corporation, All rights reserved. 16 Declaring Variables.
Oracle 8i Exception Handling. General Syntax DECLARE --- BEGIN --- EXCEPTION WHEN exception_name1 THEN -Error handling statements WHEN exception_name2.
Using SQL in PL/SQL ITEC 224 Database Programming.
PL/SQLPL/SQL Oracle11g: PL/SQL Programming Chapter 4 Cursors and Exception Handling.
ITEC 224 Database Programming PL/SQL Lab Cursors.
1 Handling Exceptions Part F. 2 Handling Exceptions with PL/SQL What is an exception? Identifier in PL/SQL that is raised during execution What is an.
Chapter 16 Cursors and Exceptions. Chapter Objectives  Determine when an explicit cursor is required  Declare, open, and close an explicit cursor 
Introduction to Explicit Cursors. 2 home back first prev next last What Will I Learn? Distinguish between an implicit and an explicit cursor Describe.
Chapter 18: Exception Handling1 Chapter Eighteen Exception Handling Objective: – Define exceptions – List types of exception handlers – Trap errors – Exception.
Handling Exceptions. Objectives What is exception Types of exceptions How to handle exceptions Trapping pre defined oracle errors.
Declaring PL/SQL Variables
Copyright  Oracle Corporation, All rights reserved. 23 Handling Exceptions.
Program with PL/SQL Lesson 3. Interacting with the Oracle Server.
Kingdom of Saudi Arabia Ministry of Higher Education Al-Imam Muhammad Ibn Saud Islamic University College of Computer and Information Sciences Overview.
Introduction to PL/SQL N. Dimililer. About PL/SQL –PL/SQL is an extension to SQL with design features of programming languages. –Data manipulation and.
CS422 Principles of Database Systems Oracle PL/SQL Chengyu Sun California State University, Los Angeles.
DB Programming Course Lecture 4. Errors Two types of errors can be found in a program: compilation errors and runtime errors. There is a special section.
1 Copyright © 2004, Oracle. All rights reserved. PL/SQL Programming Concepts: Review.
PLSQL Cont…. Most Common Oracle Data Types VARCHAR2 –Stores variable-length character data. –Takes a required parameter that specifies a maximum length.
ITEC 224 Database Programming
CS322: Database Systems PL/ SQL PL/SQL by Ivan Bayross.
CHAPTER 5 EXCEPTION HANDLING
Interacting with the Oracle Server
Interacting with the Oracle Server
Handling Exceptions.
SQL Stored Triggers Presented by: Dr. Samir Tartir
Oracle11g: PL/SQL Programming Chapter 4 Cursors and Exception Handling.
Cursors ITEC 224 Database Programming PL/SQL Lab.
Oracle9i Developer: PL/SQL Programming Chapter 3 PL/SQL Processing.
Handling Exceptions.
SQL PL/SQL Presented by: Dr. Samir Tartir
Database Management Systems 2
Agenda Summary of last class Cursors Loops Records Simple Loops
Handling Exceptions.
Handling Exceptions.
Database Management Systems 2
Handling Exceptions.
PL/SQL week10.
Database Programming Using Oracle 11g
Presentation transcript:

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

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

DBA 与 Oracle DBA

Database, Data and System Administration  Metadata  Data Model

DA vs. DBA vs. SA

Web 应用需求挖掘

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

PL/SQL 中的异常处理

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

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

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;

异常名与错误代码  异常名: 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

预定义系统异常  例子: 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...

系统异常举例  CURSOR_ALREADY_OPEN ORA-6511 SQLCODE = – 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 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.

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

发送异常示例 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;

发送异常示例 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;

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;

异常处理 (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;

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

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

异常处理方式

 考虑以下问题 :  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;

异常处理方式

异常处理方式 --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;

异常处理方式 — 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.

异常处理方式 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;

异常处理方 -- 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;

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

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

PL/SQL 中的内存数据

 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.

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

内存数据的声明与命名  特殊用法 : 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

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