CIS4368: Advanced DatabaseSlide # 1 PL/SQL Dr. Peeter KirsSpring, 2003 PL/SQL.

Slides:



Advertisements
Similar presentations
PL/SQL.
Advertisements

BD05/06 PL/SQL  Introduction  Structure of a block  Variables and types  Accessing the database  Control flow  Cursors  Exceptions  Procedures.
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.
SQL*PLUS, PLSQL and SQLLDR Ali Obaidi. SQL Advantages High level – Builds on relational algebra and calculus – Powerful operations – Enables automatic.
INTRODUCTION TO ORACLE Lynnwood Brown System Managers LLC Introduction to PL/SQL – Lecture 6.
Chapter 8 Embedded SQL.
PL/SQL (Procedural Language extensions to SQL) Prepared by: Manoj Kathpalia Edited by: M V Ramakrishna.
Chapter 4B: More Advanced PL/SQL Programming
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.
A Guide to SQL, Seventh Edition. Objectives Embed SQL commands in PL/SQL programs Retrieve single rows using embedded SQL Update a table using embedded.
Introduction to PL/SQL
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.
SQL Within PL / SQL Chapter 4. 2 SQL Within PL / SQL SQL Statements DML in PL / SQL Pseudocolums Transaction Control.
Session Title: Using SQL and PL/SQL for Queries and Reporting Presented By: Stephen Frederic Institution: IHL September 16, 2013.
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.
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:
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.
Oracle10g Developer: PL/SQL Programming1 Objectives Programming fundamentals The PL/SQL block Define and declare variables Initialize variables The NOT.
PL/SQLPL/SQL Oracle10g Developer: PL/SQL Programming Chapter 2 Basic PL/SQL Block Structures.
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.
University of Sunderland COM 220 Lecture Six Slide 1 Building Interactive Forms Applications using Oracle.
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.
Session Title: Using SQL and PL/SQL for Queries and Reporting Presented By: Stephen Frederic Institution: IHL September 16, 2014.
LECTURE 1 INTRODUCTION TO PL/SQL Tasneem Ghnaimat.
CS178 Database Management PL/SQL session 8 References: ORACLE 9i PROGRAMMING A Primer Rajshekhar Sunderraman.
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.
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.
Manipulating Data in PL/SQL. 2 home back first prev next last What Will I Learn? Construct and execute PL/SQL statements that manipulate data with DML.
Chapter 15 Introduction to PL/SQL. Chapter Objectives  Explain the benefits of using PL/SQL blocks versus several SQL statements  Identify the sections.
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)
1 PL\SQL Dev Templates. 2 TEMPLATE DEFINITION Whenever you create a new program unit, its initial contents are based upon a template which contains pre-defined.
PL/SQLPL/SQL Oracle10g Developer: PL/SQL Programming Chapter 9 Database Triggers.
Using SQL in PL/SQL ITEC 224 Database Programming.
© 2007 by Prentice Hall7-1 Introduction to Oracle 10g Chapter 7 Using PL/SQL to Your Advantage James Perry and Gerald Post.
PL/SQLPL/SQL Oracle11g: PL/SQL Programming Chapter 4 Cursors and Exception Handling.
Introduction to Explicit Cursors. 2 home back first prev next last What Will I Learn? Distinguish between an implicit and an explicit cursor Describe.
A Guide to SQL, Eighth Edition Chapter Eight SQL Functions and Procedures.
implicit and an explicit cursor
Handling Exceptions. Objectives What is exception Types of exceptions How to handle exceptions Trapping pre defined oracle errors.
Text TCS INTERNAL Oracle PL/SQL – Introduction. TCS INTERNAL PL SQL Introduction PLSQL means Procedural Language extension of SQL. PLSQL is a database.
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.
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 Stored Procedures and Triggers Chengyu Sun California State University, Los Angeles.
ITEC 224 Database Programming
Pl/SQL LANGUAGE MULITPLE CHOICE QUESTION SET-3
A Guide to SQL, Seventh Edition
PL/SQL.
(With a little more on Database Design)
Oracle11g: PL/SQL Programming Chapter 2 Basic PL/SQL Block Structures.
Handling Exceptions.
PL/SQL Scripting in Oracle:
Handling Exceptions.
Chapter 4: Introduction to PL/SQL
Chapter 2 Handling Data in PL/SQL Blocks Oracle9i Developer:
PL/SQL week10.
Chapter 8 Advanced SQL.
PL/SQL Declaring Variables.
COP 2700 – Data Structures (SQL)
Presentation transcript:

CIS4368: Advanced DatabaseSlide # 1 PL/SQL Dr. Peeter KirsSpring, 2003 PL/SQL

CIS4368: Advanced DatabaseSlide # 2 PL/SQL Dr. Peeter KirsSpring, 2003 Procedural Language for SQL (PL/SQL) is an extension of Oracle SQL The basic intent of PL/SQL is  increase the expressiveness of SQL  process query results in a tuple-oriented way  develop modular database application programs  reuse program code, and  reduce the cost for maintaining and changing applications.  constants and variables can be declared, and variables can be used to store query results. Statements in a PL/SQL block include: The basic construct of PL/SQL is a block  SQL statements  Control structures (loops)  Condition statements (if-then-else)  Exception handling  Calls of other PL/SQL blocks.

CIS4368: Advanced DatabaseSlide # 3 PL/SQL Dr. Peeter KirsSpring, 2003 Each block builds a (named) program unit, and blocks can be nested. The structure of a PL/SQL looks as follows: (brackets [ ] enclose optional parts) [ ] [declare ] begin [exception ] end;

CIS4368: Advanced DatabaseSlide # 4 PL/SQL Dr. Peeter KirsSpring, 2003 Consider the following simple code: Needed to display output Our local variable of data type DATE Get today’s date and store it in our variable Display the contents of the variable Program Output

CIS4368: Advanced DatabaseSlide # 5 PL/SQL Dr. Peeter KirsSpring, 2003 Numeric operations in PL/SQL:

CIS4368: Advanced DatabaseSlide # 6 PL/SQL Dr. Peeter KirsSpring, 2003 Bind Variables: Host variables:  Declared in the SQL “Host Environment”  Remain active for the length of the SQL Session  Can be displayed with the SQL Print Command  Can only be accessed in the program when prefaced with a colon (:)

CIS4368: Advanced DatabaseSlide # 7 PL/SQL Dr. Peeter KirsSpring, 2003 Interactive Input:

CIS4368: Advanced DatabaseSlide # 8 PL/SQL Dr. Peeter KirsSpring, 2003 Control Structures: IF-THEN:

CIS4368: Advanced DatabaseSlide # 9 PL/SQL Dr. Peeter KirsSpring, 2003 Control Structures: IF-THEN-ELSE:

CIS4368: Advanced DatabaseSlide # 10 PL/SQL Dr. Peeter KirsSpring, 2003 Control Structures: IF-THEN-ELSIF: (Note Spelling)

CIS4368: Advanced DatabaseSlide # 11 PL/SQL Dr. Peeter KirsSpring, 2003 Control Structures: Basic Loop:

CIS4368: Advanced DatabaseSlide # 12 PL/SQL Dr. Peeter KirsSpring, 2003 Control Structures: While:

CIS4368: Advanced DatabaseSlide # 13 PL/SQL Dr. Peeter KirsSpring, 2003 Control Structures: For:

CIS4368: Advanced DatabaseSlide # 14 PL/SQL Dr. Peeter KirsSpring, 2003 SQL in PL: We must be careful about single record/field queries and multiple return queries

CIS4368: Advanced DatabaseSlide # 15 PL/SQL Dr. Peeter KirsSpring, 2003 Single Return Queries: The data type applied to field studentname is automatically applied

CIS4368: Advanced DatabaseSlide # 16 PL/SQL Dr. Peeter KirsSpring, 2003 Multiple Record Queries:

CIS4368: Advanced DatabaseSlide # 17 PL/SQL Dr. Peeter KirsSpring, 2003 Remember our problem about calculating a student grade? /* This program calculates a grade */ declare cursor studentgrade is select lastname, firstname, quiz1, quiz2, quiz3 from grades; studentlastname grades.lastname%type; studentfirstname grades.firstname%type; q1 grades.quiz1%type; q2 grades.quiz2%type; q3 grades.quiz3%type; average number; sgrade grades.grade%type; nblanks number; blanks char(5);  Variable Declarations:

CIS4368: Advanced DatabaseSlide # 18 PL/SQL Dr. Peeter KirsSpring, 2003 Remember our problem about calculating a student grade? begin dbms_output.put_line('Student Name Grade'); dbms_output.put_line(' '); open studentgrade; loop fetch studentgrade into studentlastname, studentfirstname, q1, q2, q3; exit when studentgrade%notfound; average := (q1 + q2 + q3)/3; nblanks := 20 - (length(trim(studentfirstname)) + length(trim(studentlastname))); if average >= 90 then sgrade := 'A'; elsif average >= 80 then sgrade := 'B'; elsif average >= 70 then sgrade := 'C'; elsif average >= 60 then sgrade := 'D'; else sgrade := 'F'; end if; dbms_output.put_line(trim(studentfirstname) || ' ' || trim(studentlastname) || lpad(' ',nblanks,' ') || sgrade); update grades set grade = sgrade; end loop; close studentgrade; end;

CIS4368: Advanced DatabaseSlide # 19 PL/SQL Dr. Peeter KirsSpring, 2003 Remember our problem about calculating a student grade?

CIS4368: Advanced DatabaseSlide # 20 PL/SQL Dr. Peeter KirsSpring, 2003 Exceptions: Act as error handling routines

CIS4368: Advanced DatabaseSlide # 21 PL/SQL Dr. Peeter KirsSpring, 2003 Types of Exceptions: Exception Name No_data_found Description Single row select returned no data Too_Many_rowsSingle row select returned multiple rows Zero_DivideAttempt to divide by zero Value_ErrorArithmetic, Conversion, Truncation error Storage_ErrorPL/SQL ran out of memory or memory corrupted Login_DeniedInvalid Username or password Program_ErrorRun Time error Access_Into_NullAttempt to assign values to uninitialized object Invalid_CursorIllegal cursor operation Rowtype_MismatchCursor variable involved in incompatible return types --- And Others ---

CIS4368: Advanced DatabaseSlide # 22 PL/SQL Dr. Peeter KirsSpring, 2003 Procedures: Remember our Grading program? We could have created it as stored Procedure:

CIS4368: Advanced DatabaseSlide # 23 PL/SQL Dr. Peeter KirsSpring, 2003 To run the program:

CIS4368: Advanced DatabaseSlide # 24 PL/SQL Dr. Peeter KirsSpring, 2003 Functions, which are called by procedures, can also be created and stored:

CIS4368: Advanced DatabaseSlide # 25 PL/SQL Dr. Peeter KirsSpring, 2003 To call the function:

CIS4368: Advanced DatabaseSlide # 26 PL/SQL Dr. Peeter KirsSpring, 2003 Triggers:  A stored block which is implicitly called when an event occurs INSERT  A triggering event is based on a Data Manipulation Language statement such as: UPDATE DELETE  Execution of the trigger is known as firing the trigger

CIS4368: Advanced DatabaseSlide # 27 PL/SQL Dr. Peeter KirsSpring, 2003 Recall our problem about determining whether or not a student had enrolled for two classes at the same time  Information about students in a class was found only in the table enrollment  Information about when a class met was found only in the table class If the information were in one table we could apply a constraint which would not allow a student to enroll in both classes:

CIS4368: Advanced DatabaseSlide # 28 PL/SQL Dr. Peeter KirsSpring, 2003 However, even if we had created this table, it still would not stop a student from enrolling in two classes that meet at the same time  Enrollment in a class is done by entering a record in table enrollment (not table temp_table) One way to stop dual enrollment is to set a trigger which tries to insert the record (from enrollment) into table temp_table (which contains the constraint)  If the record can be inserted into temp_table, it will then be inserted into table enrollment  If the record can NOT be inserted into temp_table, it will NOT be inserted into table enrollment

CIS4368: Advanced DatabaseSlide # 29 PL/SQL Dr. Peeter KirsSpring, 2003 The trigger might appear as: FOR EACH ROW is a row trigger which fires once for each row inserted: :NEW refers to the new record to be inserted

CIS4368: Advanced DatabaseSlide # 30 PL/SQL Dr. Peeter KirsSpring, 2003 Suppose we look up the Spring 2003 (semester = 102) schedule for Yao Ming (studentid = ): There is one other class that meets at the same time

CIS4368: Advanced DatabaseSlide # 31 PL/SQL Dr. Peeter KirsSpring, 2003 If we now try and enroll Yao for the other class:

CIS4368: Advanced DatabaseSlide # 32 PL/SQL Dr. Peeter KirsSpring, 2003