Dr. James Dullea, CSC8490 Introduction to PL/SQLSlide 1 of 36 7From Prof. Dullea CSC8490 Introduction to PL/SQL Module 01-9 Revised: June 12, 2005 Dr.

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

AN INTRODUCTION TO PL/SQL Mehdi Azarmi 1. Introduction PL/SQL is Oracle's procedural language extension to SQL, the non-procedural relational database.
PL/SQL. Introduction to PL/SQL PL/SQL is the procedure extension to Oracle SQL. It is used to access an Oracle database from various environments (e.g.
PL/SQL.
PL/SQL (Procedural Language extensions to SQL) Prepared by: Manoj Kathpalia Edited by: M V Ramakrishna.
Lecture-5 Though SQL is the natural language of the DBA, it suffers from various inherent disadvantages, when used as a conventional programming language.
Chapter 4B: More Advanced PL/SQL Programming
Advanced Package Concepts. 2 home back first prev next last What Will I Learn? Write packages that use the overloading feature Write packages that use.
CS 104 Introduction to Computer Science and Graphics Problems Introduction to Database (2) Basic SQL 12/05/2008 Yang Song.
A Guide to Oracle9i1 Advanced SQL And PL/SQL Topics Chapter 9.
Oracle PL/SQL Eyad Husni Elshami. Why PL/SQL Block Structures: – PL/SQL consists of blocks of code, which can be nested within each other. Each block.
Introduction to PL/SQL
Objectives Why PL-SQL ? Language features
Bordoloi and Bock PROCEDURES, FUNCTIONS & TRIGGERS.
Introduction to PL/SQL. Procedural Language extension for SQL Oracle Proprietary 3GL Capabilities Integration of SQL Portable within Oracle data bases.
Bordoloi and Bock CURSORS. Bordoloi and Bock CURSOR MANIPULATION To process an SQL statement, ORACLE needs to create an area of memory known as the context.
Distributed Database Applications COSC 5050 Week Three.
PL / SQL P rocedural L anguage / S tructured Q uery L anguage Chapter 7 in Lab Reference.
Cursor and Exception Handling By Nidhi Bhatnagar.
1 Introduction to PL/SQL. 2  Procedural programming language  Uses detailed instructions  Processes statements sequentially  Combines SQL commands.
SQL enables us to create, organize, retrieve and maintain data stored in database it does not provide the features which a typical programming language.
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.
PL SQL Block Structures. What is PL SQL A good way to get acquainted with PL/SQL is to look at a sample program. PL/SQL combines the data manipulating.
LECTURE 1 INTRODUCTION TO PL/SQL Tasneem Ghnaimat.
PL / SQL By Mohammed Baihan. What is PL/SQL? PL/SQL stands for Procedural Language extension of SQL. PL/SQL is a combination of SQL along with the procedural.
PL/SQL Procedural Language / Structured Query Language.
1 Copyright © 2004, Oracle. All rights reserved. Introduction to PL/SQL.
CIS4368: Advanced DatabaseSlide # 1 PL/SQL Dr. Peeter KirsSpring, 2003 PL/SQL.
8 1 Chapter 8 Advanced SQL Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
BIS Database Systems School of Management, Business Information Systems, Assumption University A.Thanop Somprasong Chapter # 8 Advanced SQL.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
Chapter 15 Introduction to PL/SQL. Chapter Objectives  Explain the benefits of using PL/SQL blocks versus several SQL statements  Identify the sections.
Database Application Development using PL/SQL Programming.
Guide to Oracle 10g ITBIS373 Database Development Lecture 4a - Chapter 4: Using SQL Queries to Insert, Update, Delete, and View Data.
Database Management COP4540, SCS, FIU Oracle PL/SQL (Ch 10.5)
Objectives Database triggers and syntax
PL/SQLPL/SQL Oracle10g Developer: PL/SQL Programming Chapter 9 Database Triggers.
Chapter 8 Advanced SQL Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
PL/SQLPL/SQL Oracle11g: PL/SQL Programming Chapter 9 Database Triggers.
PL/SQLPL/SQL Oracle10g Developer: PL/SQL Programming Chapter 9 Database Triggers.
Database Programming Contents 1. The Impedance Mismatch 2. JDBC 1. From the Ricardo text 2. From the Elmasri/Navathe text 3. From the ASU scholars 3. PLSQL:
A procedure is a module performing one or more actions; it does not need to return any values. The syntax for creating a procedure is as follows: CREATE.
Database Programming: Overview Contents 1. The Impedance Mismatch 2. JDBC 1. From the Elmasri/Navathe text 2. From the ASU scholars 3. PLSQL: by Prof.
Text TCS INTERNAL Oracle PL/SQL – Introduction. TCS INTERNAL PL SQL Introduction PLSQL means Procedural Language extension of SQL. PLSQL is a database.
Introduction to PL/SQL Francis Thottungal. The outline The basic PL/SQL code structure is : DECLARE -- optional, which declares and define variables,
Lab 2 Writing PL/SQL Blocks CISB514 Advanced Database Systems.
Kingdom of Saudi Arabia Ministry of Higher Education Al-Imam Muhammad Ibn Saud Islamic University College of Computer and Information Sciences Overview.
SQL Triggers, Functions & Stored Procedures Programming Operations.
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.
CS422 Principles of Database Systems Stored Procedures and Triggers Chengyu Sun California State University, Los Angeles.
1 Copyright © 2004, Oracle. All rights reserved. PL/SQL Programming Concepts: Review.
Difference between Oracle PL/SQL and MySQL
Interacting with the Oracle Server
PL/SQL.
Oracle11g: PL/SQL Programming Chapter 2 Basic PL/SQL Block Structures.
UNIT - V STORED PROCEDURE.
SQL PL/SQL Presented by: Dr. Samir Tartir
PL/SQL Scripting in Oracle:
Joining Tables ضم الجداول وإستخراج مناظر views منها الهدف : 1- استخراج المعلومات من جدولين أو اكثر بالإستفادة من الرابط بينهما وبإستخدام SQL 2- شروط قواعد.
Chapter 2 Handling Data in PL/SQL Blocks Oracle9i Developer:
PL/SQL week10.
Oracle9i Developer: PL/SQL Programming Chapter 8 Database Triggers.
Chapter 8 Advanced SQL.
PL/SQL Declaring Variables.
Chapter 8 Advanced SQL Pearson Education © 2009.
Prof. Arfaoui. COM390 Chapter 9
Presentation transcript:

Dr. James Dullea, CSC8490 Introduction to PL/SQLSlide 1 of 36 7From Prof. Dullea CSC8490 Introduction to PL/SQL Module 01-9 Revised: June 12, 2005 Dr. James Dullea

Dr. James Dullea, CSC8490 Introduction to PL/SQL Slide 2 of 36 Introduction to PL/SQL What is PL/SQL Why PL/SQL Kinds of PL/SQL BLOCKS Anonymous or Named Blocks Named Blocks (Stored procedures, Funtions, Triggers) Exceptions Assignments (2)

Dr. James Dullea, CSC8490 Introduction to PL/SQL Slide 3 of 36 What is PL/SQL PL/SQL is a sophistical programming language used to access an Oracle database from a various environments. PL/SQL stands for Procedural Language/SQL. It extends SQL by adding constructs found in other procedural languages, such as: loops, conditional statements, declared variables, accessing individual records one at a time, and many others.

Dr. James Dullea, CSC8490 Introduction to PL/SQL Slide 4 of 36 Why use PL/SQL Compared to SQL, PL/SQL has the procedural constructs that are useful to express a desired process from start to end. One block of PL/SQL code can bundled several SQL statements together as a single unit. Making less network traffic and improving application performance. PL/SQL can be integrated with other languages, such as Java, to take advantage of the strongest features of both languages.

Dr. James Dullea, CSC8490 Introduction to PL/SQL Slide 5 of 36 Kinds of PL/SQL BLOCKS The basic unit in any PL/SQL PROGRAM is a BLOCK. All PL/SQL programs are composed of a single block or blocks that occur either sequentially or nested within another block. There are two kinds of blocks: Anonymous blocks are generally constructed dynamically and executed only once by the user. It is sort of a complex SQL statement. Named blocks are blocks that have a name associated with them, are stored in the database, and can be executed again and again, can take in parameters, and can modify and existing database.

Dr. James Dullea, CSC8490 Introduction to PL/SQL Slide 6 of 36 Structure of Anonymous Block DECLARE /* Declare section (optional). */ BEGIN /* Executable section (required). */ EXCEPTION /* Exception handling section (optional). */ END; -- end the block (do not forget the “ ; “ in the end.) /

Dr. James Dullea, CSC8490 Introduction to PL/SQL Slide 7 of 36 Example of Anonymous Block SET SERVEROUTPUT ON DECLARE v_last_name VARCHAR2(15); v_salary employee.salary%TYPE; CURSOR c_query IS SELECT lname, salary FROM employee; BEGIN OPEN c_query; LOOP FETCH c_query INTO v_last_name, v_salary; IF v_salary >= THEN DBMS_OUTPUT.PUT_LINE (v_last_name||' '||v_salary); END IF; EXIT WHEN c_query%NOTFOUND; END LOOP; CLOSE c_query; END; /

Dr. James Dullea, CSC8490 Introduction to PL/SQL Slide 8 of 36 PL/SQL Build-In Utility Packages DBMS_OUTPUT.PUT_LINE SET SERVEROUTPUT ON to allow output to be displayed to the screen DBMS_OUTPUT.PUT_LINE Usage: DBMS_OUTPUT.PUT_LINE ( Argument ) Argument tendS to resemble the concatenated arguments of the SELECT clause in an SQL query. If the argument is not initialized, then a NULL VALUE will be displayed.

Dr. James Dullea, CSC8490 Introduction to PL/SQL Slide 9 of 36 PL/SQL Build-In Utility Packages Example SET SERVEROUTPUT ON DECLARE v_name VARCHAR2(30); v_title VARCHAR2(20); BEGIN v_name := 'James Dullea'; v_title := 'Research Scientist'; DBMS_OUTPUT.PUT_LINE(v_name||' works as a '||v_title); END; /

Dr. James Dullea, CSC8490 Introduction to PL/SQL Slide 10 of 36 Variables and Types A variable is a storage location that can be read from or assigned to by the program. It is declared in the declarative section within a PL/SQL block. v_last_name VARCHAR2(15); Every variable has a specific data type associated with it. Variables can be associated with a table structure. v_salary employee.salary%TYPE;

Dr. James Dullea, CSC8490 Introduction to PL/SQL Slide 11 of 36 Data Types Scalar type Numeric: INT, DEC,NUMBER,BINARY_INTEGER … Character: CHAR, CHARACTER, STRING, VARCHAR, VARCHAR2… Boolean: TRUE, FALSE. Date: DATE Composite types RECORD, TABLE, VARRAY Reference types CURSORS, OBJECT TYPES Lob types BFILE, LOB, CLOB, NLOB

Dr. James Dullea, CSC8490 Introduction to PL/SQL Slide 12 of 36 CURSORS  A cursor is used to process a single row 'at a time' from multiple rows retrieved from the database.  Cursors are declared in the Declaration Section. CURSOR c_query IS SELECT lname, salary FROM employee;  The cursor can be declared for complex queries involving joins and conditions.  Cursors must be OPENed to be accessed and CLOSEd before ending the program. OPEN c_query; CLOSE c_query;

Dr. James Dullea, CSC8490 Introduction to PL/SQL Slide 13 of 36 CURSORS  The FETCH statement is used to retrieve the output of a single record from the CURSOR SELECT statement INTO associate variables. FETCH c_query INTO v_last_name, v_salary;  Cursors can be opened and closed more than once in a block and if the a WHERE statement exists, the values of the binding variables can be modified.  “Cursor FOR loop” is a special type of for loop which the SQL cursor operations are carried out implicitly.

Dr. James Dullea, CSC8490 Introduction to PL/SQL Slide 14 of 36 Conditional Statements Conditional Processing The specified conditions are evaluated by the system and the result determines which sequence of statements is to be carried out. IF THEN END IF; IF THEN ELSE END IF;

Dr. James Dullea, CSC8490 Introduction to PL/SQL Slide 15 of 36 Conditional Statements (ELSIF) IF THEN ELSIF THEN ELSIF THEN ELSIF THEN ELSE END IF;

Dr. James Dullea, CSC8490 Introduction to PL/SQL Slide 16 of 36 Loop Structures  Unconstrained loops  WHILE loop  FOR loop  GOTO >

Dr. James Dullea, CSC8490 Introduction to PL/SQL Slide 17 of 36 Unconstrained Loops LOOP EXIT WHEN END LOOP;

Dr. James Dullea, CSC8490 Introduction to PL/SQL Slide 18 of 36 WHILE LOOP WHILE LOOP END LOOP; Note: The loop will continue to process as long as the condition is TRUE or an EXIT (or EXIT WHEN) statement is encountered.

Dr. James Dullea, CSC8490 Introduction to PL/SQL Slide 19 of 36 FOR LOOP FOR IN [REVERSE].. LOOP END LOOP;

Dr. James Dullea, CSC8490 Introduction to PL/SQL Slide 20 of 36 GOTO statement GOTO label; The label is defined in the block by being enclosed in double angle brackets. Example: LOOP IF THEN GOTO get_out_of_loop; END LOOP; >

Dr. James Dullea, CSC8490 Introduction to PL/SQL Slide 21 of 36 NAMED BLOCKS The following are types of NAMED BLOCKS Stored Procedures Similar to an anonymous block except it can be stored in the database, can accept parameters, and can be executed over and over again (with different parameters) Functions Type of named blocks that is executed within a DML or SQL statement. It may take in one or more parameters and RETURNs only one value back to the calling application. Triggers A named block that executes only when an associated DML statement is executed, such as an INSERT, UPDATE, or DELETE statement.

Dr. James Dullea, CSC8490 Introduction to PL/SQL Slide 22 of 36 Block Structure for PL/SQL Subprograms (Procedures or Functions) Program Comments (optional) Header (mandatory) IS|AS (mandatory) Declaration Section BEGIN (mandatory) Executable Section EXCEPTION (optional) Exception Section END; (mandatory) /

Dr. James Dullea, CSC8490 Introduction to PL/SQL Slide 23 of 36 Block Structure for PL/SQL subprograms Completed procedure example CREATE OR REPLACE PROCEDURE AddDepartment /*Procedure to add rows In the COMPANY.department table */ (p_DepartmentName IN department.dname%TYPE, p_DepartmentNumber IN department.dnumber%TYPE, p_DepartmentManager IN department.mgrssn%TYPE, p_ManagerStartDate IN department.mgrstartdate%TYPE) AS BEGIN INSERT INTO DEPARTMENT(dname, dnumber, mgrssn, mgrstartdate) VALUES (p_DepartmentName, p_DepartmentNumber, p_DepartmentManager, p_ManagerStartDate); COMMIT; END AddDepartment; /

Dr. James Dullea, CSC8490 Introduction to PL/SQL Slide 24 of 36 Execution of a Stored Functions EXEC AddDepartment ('Info Center', 43, ' ', '28- JUN-2005');

Dr. James Dullea, CSC8490 Introduction to PL/SQL Slide 25 of 36 Syntax of a Stored Functions CREATE OR REPLACE FUNCTION function_name (parameters IN datatypes) RETURN datatype IS|AS Declaration Section BEGIN Executable Section RETURN variable_name EXCEPTION (optional) Exception Section END;

Dr. James Dullea, CSC8490 Introduction to PL/SQL Slide 26 of 36 Example of a Stored Functions Given the salary of an employee, calculate the state tax of 2.8% from the salary and return it.

Dr. James Dullea, CSC8490 Introduction to PL/SQL Slide 27 of 36 Example of a Stored Functions CREATE OR REPLACE FUNCTION CalcStateTax ( p_salary IN employee.salary%TYPE) RETURN NUMBER AS BEGIN RETURN (p_salary * 0.028); END CalcStateTax; /

Dr. James Dullea, CSC8490 Introduction to PL/SQL Slide 28 of 36 Execution of a Stored Functions SELECT fname||' '||lname AS "EMPLOYEE", CalcStateTax(salary) AS "STATE TAX" FROM employee;

Dr. James Dullea, CSC8490 Introduction to PL/SQL Slide 29 of 36 Execution of a Stored Functions EMPLOYEE STATE TAX James Borg 1540 Jennifer Wallace 1204 Franklin Wong 1120 John Smith 840 Alicia Zelaya 700 Ramesh Narayan 1064 Joyce English 700 Ahmad Jabbar rows selected.

Dr. James Dullea, CSC8490 Introduction to PL/SQL Slide 30 of 36 What is a Trigger Similar to stored procedures and functions. Contains a Declaration, Executable, and Exception sections Differences Triggers are not executed explicitly, they are implicitly execute when a triggering event occurs. (This is called firing the trigger) Triggers do not accept parameters Triggering events are fired by DML Statements ( INSERTs, UPDATEs, or DELETEs) against tables or views AND certain system events

Dr. James Dullea, CSC8490 Introduction to PL/SQL Slide 31 of 36 Why Use Triggers Complex integrity constraints are not always possible through declarative constraints enabled at table creation time, such as salary may not be lowered. Auditing information, such as who updated an employee's salary, may be required. Remember triggers happen at the basic DML level. Triggers can signal other application that action needs to take place when changes are made to a table. Example, update employee statistics contained in another table.

Dr. James Dullea, CSC8490 Introduction to PL/SQL Slide 32 of 36 Block Structure for a PL/SQL Trigger CREATE [OR REPLACE] TRIGGER trigger_name AFTER | BEFORE | INSTEAD OF a_trigger_event ON table_name (or view_name) [FOR EACH ROW[WHEN trigger_condition]] DECLARE (optional) BEGIN (mandatory) Executes only when trigger_condition is TRUE on a ROW LEVEL TRIGGER EXCEPTION (optional) Exception Section END; (mandatory) NOTE: a_trigger_event may be any combination of an INSERT, DELETE, and/or UPDATE on a table or view

Dr. James Dullea, CSC8490 Introduction to PL/SQL Slide 33 of 36 Errors and Error Handling Errors can be classified into two types: 1) Compile-Time errors and warnings. After compiling use the command SHOW ERRORS use SET ECHO ON to see statement numbers 2) Run-Time errors occur during execution and throw exception that can be handled by the program.

Dr. James Dullea, CSC8490 Introduction to PL/SQL Slide 34 of 36 Error Handling When errors occur during the execution, control will be branched to the exception handling section. A corresponding error handler will be found to deal with the error.

Dr. James Dullea, CSC8490 Introduction to PL/SQL Slide 35 of 36 Practice 1 Use the Company database schema, write a stored procedure to add an employee to the employee table, using parameters to input the data. Use your name and following information to test the procedure. FNAME use your first name MINIT use your middle init or a blank space LNAME use your last name SSN make up a 9 digit number BDATE use your birthday (be careful of the date format) STREET make up data CITY Villanova STATE PA ZIP use the Villanova zip code SEX M or F SALARY SUPERSSN DNO 5

Dr. James Dullea, CSC8490 Introduction to PL/SQL Slide 36 of 36 Practice 2 Write a function (called GetDay) that will take in a date as a parameter and return the actual name of the day for that date. Use the function to solve the following problem. Using the data in the employee table from Assignment 1, write an SQL statement or an anonymous block (containing the above function GetDay) that uses your first and last name in a where clause to access the record and returns the actual day of the week that you were born. Hint: GetDay(bdate)