Presentation is loading. Please wait.

Presentation is loading. Please wait.

Database Application Development using PL/SQL Programming.

Similar presentations


Presentation on theme: "Database Application Development using PL/SQL Programming."— Presentation transcript:

1 Database Application Development using PL/SQL Programming

2 Chapter 1. Introduction

3 PL/SQL Programming PL/SQL is the procedural extension to SQL with design features of programming languages. Data manipulation and query statements of SQL are included within procedural units of code. Introduction to PL/SQL

4 PL/SQL Programming PL/SQL block PL/SQL Environment PL/SQL block Procedural Statement executor PL/SQL Engine SQL statement executor Oracle server

5 PL/SQL Programming PL/SQL and Oracle-based Applications Oracle applications are designed for client-server architecture.

6 PL/SQL Programming Design and creation of database; Creation of database triggers; Building complex business rules at the database level; Building screens, reports for user interface ( Objects in these applications may require PL/SQL codes) Programming in Oracle Applications

7 PL/SQL Programming Benefits:  Integration  Improved performance  Modularised Program Development  Portability  Control Structures  Error Handling

8 PL/SQL Programming Chapter 2. Declaring Variables

9 PL/SQL Programming  Recognise the basic PL/SQL block and its sections  Describe the significance of variables in PL/SQL  Distinguish between PL/SQL and non-PL/SQL variables  Declare PL/SQL variables  Execute PL/SQL blocks Objectives

10 PL/SQL Programming DECLARE (Optional) constants variables cursors user-defined exceptions BEGIN (Mandatory) SQL statements PL/SQL statements EXCEPTION (Optional) Exception handling END; (Mandatory) PL/SQL Block Structure

11 PL/SQL Programming PL/SQL Block Structure PL/SQL Block can have a header that goes on top of the block; The header specifies whether the block is a procedure, a function or a package; If the header is absent, the block is said to be an anonymous block. DECLARE (Optional) constants variables cursors user-defined exceptions BEGIN (Mandatory) SQL statements PL/SQL statements EXCEPTION (Optional) Exception handling END; (Mandatory)

12 PL/SQL Programming DECLARE v_variable VARCHAR2(5); BEGIN SELECTcolumn_name INTO v_variable FROM table_name; EXCEPTION WHEN exception_name THEN... END; / Executing Statements and PL/SQL Blocks

13 PL/SQL Programming [DECLARE] BEGIN -- statements [EXCEPTION] END; Block Types PROCEDURE name IS BEGIN -- statements [EXCEPTION] END; FUNCTION name RETURN datatype IS BEGIN --statements RETURN value; [EXCEPTION] END; AnonymousProcedure Function

14 PL/SQL Programming  Anonymous blocks  Stored procedures or functions  Application procedures or functions  Packages (Application or Stored)  Database triggers (triggered by DML statements)  Application triggers Program Constructs

15 PL/SQL Programming Variables can be used for:  Temporary storage of data  Manipulation of stored values  Reusability  Ease of maintenance Use of variables

16 PL/SQL Programming  Declare and initialise variables within the declarative section  Assign new values to variables within the executable section  Pass values into PL/SQL blocks through parameters  View results through output variables Handling variables in PL/SQL

17 PL/SQL Programming PL/SQL variables  Scalar  Composite  Reference  LOB (Large objects) Non-PL/SQL variables  Bind and host variables Types of variables

18 PL/SQL Programming  TRUE represents a Boolean value.  30-MAY-78represents a DATE.  photo represents a LOB.  text of a speech represents a LONG.  8523.25 represents a NUMBER.  Movie represents a BFILE.  “Dhaka” represents a VARCHAR2. Examples

19 PL/SQL Programming Syntax: identifier [CONSTANT] datatype [NOT NULL] [:= | DEFAULT expr]; Examples: v_hire_dateDATE; v_department_idNUMBER(4) NOT NULL := 10; v_cityVARCHAR2(30) := ‘Sylhet’; c_commission_pctCONSTANT NUMBER := 1400; Declaring PL/SQL Variables

20 PL/SQL Programming Follow a naming convention. Initialise variables designated as NOT NULL and CONSTANT. Declare one identifier per line. Initialise identifiers by using the assignment operator (:=) or the DEFAULT reserved word. identifier := expr; Guidelines for Declaring PL/SQL Variables

21 PL/SQL Programming Two variables can have the same name, provided they are in different blocks. The variable name should not be the same as the name of columns of the table(s) used in the block. Adopt a naming convention for identifiers. For example: v_variable for a variable; c_constant for a constant; g_message for a global (or host) variable; An identifier must not be longer than 30 characters. Naming Rules

22 PL/SQL Programming DECLARE employee_id NUMBER(6); BEGIN FROM employees WHERE last_name = ‘Smith’; END; Naming Rules SELECT employee_id INTO employee_id Better to avoid

23 PL/SQL Programming Assignment operator (:=) DEFAULT keyword NOT NULL constraint Syntax: identifier := expr; Examples: v_hiredate := ’31-DEC-98’ ; v_mgrNUMBER(6) DEFAULT 100; v_name := ‘King’; Variable Initialization and Keywords

24 PL/SQL Programming Hold a single value Have no internal components Classified into four categories (they may have sub-types) -> number -> character -> date -> boolean Scalar Data Types

25 PL/SQL Programming CHAR [(maximum_length)] VARCHAR2 [(maximum_length)] LONG LONG RAW NUMBER [(precision, scale)] BINARY_INTEGER PLS_INTEGER BOOLEAN Base Scalar Data Types

26 PL/SQL Programming DATE TIMESTAMP TIMESTAMP WITH TIME ZONE TIMESTAMP WITH LOCAL TIME ZONE INTERVAL YEAR TO MONTH INTERVAL DAY TO SECOND Base Scalar Data Types

27 PL/SQL Programming DECLARE v_jobVARCHAR2 (9); v_countBINARY_INTEGER := 0; v_total_salNUMBER (9,2) := 0; v_orderdateDATE := SYSDATE + 7; c_tax_rateCONSTANT NUMBER (3,2) := 8.25; v_validBOOLEAN NOT NULL := TRUE; … Scalar Variable Declarations

28 PL/SQL Programming Declare a variable according to: A database column definition Another previously declared variable Prefix %TYPE with: The database table and column The previously declared variable name The %TYPE Attribute

29 PL/SQL Programming Syntax: identifier Table.column_name%TYPE; Examples: … v_nameemployees.last_name%TYPE; v_balanceNUMBER(7,2); v_min_balancev_balance%TYPE := 10; … Declaring Variables with the %TYPE Attribute

30 PL/SQL Programming Only the values TRUE, FALSE, and NULL can be assigned to a Boolean variable. The variables are compared by the logical operators AND, OR, and NOT. The variables always yield TRUE, FALSE, or NULL. Arithmetic, character, and date expressions can be used to return a Boolean value. Declaring Boolean Variables v_sal1 := 50000; v_sal2 := 60000; v_sal1 < v_sal2 DECLARE v_flag BOOLEAN := FALSE; BEGIN v_flag := TRUE; END;

31 PL/SQL Programming PL/SQL TABLES PL/SQL RECORDS Composite Datatypes

32 PL/SQL Programming CLOB (character large object) – stores large blocks of single-byte character data (Book) BLOB (binary large object) – stores large binary objects (Photo) BFILE (binary file) – stores large binary objects in operating system files (Movie) NCLOB (national language character large object) – stores unicode data LOB Data Type Variables

33 PL/SQL Programming A variable declared in a host environment. Used to pass run-time values (character or number) How to create? In SQL*Plus environment - VARIABLE return_code NUMBER VARIABLE return_message VARCHAR2(30) How to reference in PL/SQL Block? Needs to be preceded by a colon. : bind_variable How to display? PRINT bind_variable Bind Variables

34 PL/SQL Programming Example: In SQL*Plus prompt, declare- VARIABLE g_salary NUMBER Using Bind Variables BEGIN SELECT salary INTO :g_salary FROM employees WHERE employee_id = 178; END;

35 PL/SQL Programming Oracle-supplied packaged procedure An alternative for displaying data from a PL/SQL block Needs to be enabled with SET SERVEROUTPUT ON DBMS_OUTPUT.PUT_LINE SET SERVEROUTPUT ON DEFINE p_annual_sal = 60000; DECLARE v_sal NUMBER(9,2) := &p_annual_sal; BEGIN v_sal := v_sal/12; DBMS_OUTPUT.PUT_LINE (‘Monthly salary is ‘ || TO_CHAR(v_sal)); END;

36 PL/SQL Programming Time for Practice 2


Download ppt "Database Application Development using PL/SQL Programming."

Similar presentations


Ads by Google