* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Download plsql_ch1_2
Relational algebra wikipedia , lookup
Oracle Database wikipedia , lookup
Microsoft Access wikipedia , lookup
Microsoft Jet Database Engine wikipedia , lookup
Tandem Computers wikipedia , lookup
Clusterpoint wikipedia , lookup
Database model wikipedia , lookup
Relational model wikipedia , lookup
Microsoft SQL Server wikipedia , lookup
Database Application Development using PL/SQL Programming PL/SQL Programming Chapter 1. Introduction PL/SQL Programming Introduction to PL/SQL • 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. PL/SQL Programming PL/SQL Environment PL/SQL Engine PL/SQL block PL/SQL block Procedural Statement executor SQL statement executor Oracle server PL/SQL Programming PL/SQL and Oracle-based Applications Oracle applications are designed for client-server architecture. PL/SQL Programming Programming in Oracle Applications • 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) PL/SQL Programming Benefits: Integration Improved performance Modularised Program Development Portability Control Structures Error Handling PL/SQL Programming Chapter 2. Declaring Variables PL/SQL Programming Objectives 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 PL/SQL Programming PL/SQL Block Structure DECLARE (Optional) constants variables cursors user-defined exceptions BEGIN (Mandatory) SQL statements PL/SQL statements EXCEPTION (Optional) Exception handling END; (Mandatory) 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. <Block Header> DECLARE (Optional) constants variables cursors user-defined exceptions BEGIN (Mandatory) SQL statements PL/SQL statements EXCEPTION (Optional) Exception handling END; (Mandatory) PL/SQL Programming Executing Statements and PL/SQL Blocks DECLARE v_variable VARCHAR2(5); BEGIN SELECT column_name INTO v_variable FROM table_name; EXCEPTION WHEN exception_name THEN ... END; / PL/SQL Programming Block Types Anonymous Function Procedure [DECLARE] PROCEDURE name IS BEGIN -- statements BEGIN -- statements [EXCEPTION] [EXCEPTION] FUNCTION name RETURN datatype IS BEGIN --statements RETURN value; [EXCEPTION] END; END; END; PL/SQL Programming Program Constructs Anonymous blocks Stored procedures or functions Application procedures or functions Packages (Application or Stored) Database triggers (triggered by DML statements) Application triggers PL/SQL Programming Use of variables Variables can be used for: Temporary storage of data Manipulation of stored values Reusability Ease of maintenance PL/SQL Programming Handling variables in PL/SQL 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 PL/SQL Programming Types of variables PL/SQL variables Scalar Composite Reference LOB (Large objects) Non-PL/SQL variables Bind and host variables PL/SQL Programming Examples TRUE 30-MAY-78 photo text of a speech 8523.25 Movie “Dhaka” represents a Boolean value. represents a DATE. represents a LOB. represents a LONG. represents a NUMBER. represents a BFILE. represents a VARCHAR2. PL/SQL Programming Declaring PL/SQL Variables Syntax: identifier [CONSTANT] datatype [NOT NULL] [:= | DEFAULT expr]; Examples: v_hire_date v_department_id v_city c_commission_pct DATE; NUMBER(4) NOT NULL := 10; VARCHAR2(30) := ‘Sylhet’; CONSTANT NUMBER := 1400; PL/SQL Programming Guidelines for Declaring PL/SQL Variables • 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; PL/SQL Programming Naming Rules • • • 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. PL/SQL Programming Naming Rules DECLARE employee_id NUMBER(6); BEGIN SELECT employee_id Better INTO employee_id FROM employees WHERE last_name = ‘Smith’; END; to avoid PL/SQL Programming Variable Initialization and Keywords • Assignment operator (:=) • DEFAULT keyword • NOT NULL constraint Syntax: identifier := expr; Examples: v_hiredate := ’31-DEC-98’ ; v_mgr NUMBER(6) DEFAULT 100; v_name := ‘King’; PL/SQL Programming Scalar Data Types • Hold a single value • Have no internal components • Classified into four categories (they may have sub-types) -> number -> character -> date -> boolean PL/SQL Programming Base Scalar Data Types • • • • • • • • CHAR [(maximum_length)] VARCHAR2 [(maximum_length)] LONG LONG RAW NUMBER [(precision, scale)] BINARY_INTEGER PLS_INTEGER BOOLEAN PL/SQL Programming Base Scalar Data Types • • • • • • DATE TIMESTAMP TIMESTAMP WITH TIME ZONE TIMESTAMP WITH LOCAL TIME ZONE INTERVAL YEAR TO MONTH INTERVAL DAY TO SECOND PL/SQL Programming Scalar Variable Declarations DECLARE v_job v_count v_total_sal v_orderdate c_tax_rate v_valid … VARCHAR2 (9); BINARY_INTEGER := 0; NUMBER (9,2) := 0; DATE := SYSDATE + 7; CONSTANT NUMBER (3,2) := 8.25; BOOLEAN NOT NULL := TRUE; PL/SQL Programming The %TYPE Attribute • 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 PL/SQL Programming Declaring Variables with the %TYPE Attribute Syntax: identifier Table.column_name%TYPE; Examples: … v_name employees.last_name%TYPE; v_balance NUMBER(7,2); v_min_balance v_balance%TYPE := 10; … PL/SQL Programming Declaring Boolean Variables • • • • 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. v_sal1 := 50000; v_sal2 := 60000; v_sal1 < v_sal2 DECLARE v_flag BOOLEAN := FALSE; BEGIN v_flag := TRUE; END; PL/SQL Programming Composite Datatypes • PL/SQL TABLES • PL/SQL RECORDS PL/SQL Programming LOB Data Type Variables 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 PL/SQL Programming Bind Variables • A variable declared in a host environment. • Used to pass run-time values (character or number) How to create? In SQL*Plus environment VARIABLE VARIABLE return_code NUMBER 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 PL/SQL Programming Using Bind Variables Example: In SQL*Plus prompt, declareVARIABLE g_salary NUMBER BEGIN SELECT salary INTO :g_salary FROM employees WHERE employee_id = 178; END; PL/SQL Programming DBMS_OUTPUT.PUT_LINE • Oracle-supplied packaged procedure • An alternative for displaying data from a PL/SQL block • Needs to be enabled with SET SERVEROUTPUT ON 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; PL/SQL Programming Time for Practice 2