School of Computing and Management Sciences © Sheffield Hallam University SQL is non-procedural –designed to be relatively approachable to non- programmers.

Slides:



Advertisements
Similar presentations
PL/SQL.
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.
SQL*PLUS, PLSQL and SQLLDR Ali Obaidi. SQL Advantages High level – Builds on relational algebra and calculus – Powerful operations – Enables automatic.
Creating Triggers.
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 Lecture 0 – Self Study Akhtar Ali.
Bordoloi and Bock PROCEDURES, FUNCTIONS & TRIGGERS.
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.
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.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer Computer Science, IUPUI
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.
Program with PL/SQL. Interacting with the Oracle Server.
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 A BRIEF OVERVIEW DAVID WILSON. PL/SQL User’s Guide and Reference PL/SQL User’s Guide and Reference.
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.
In Oracle.  A PL/SQL block stored in the database and fired in response to a specified event ◦ DML statements : insert, update, delete ◦ DDL statements.
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.
1 Copyright © 2004, Oracle. All rights reserved. Introduction to PL/SQL.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
PL/SQL Block Structure DECLARE - Optional Variables, cursors, user-defined exceptions BEGIN - Mandatory SQL Statements PL/SQL Statements EXCEPTIONS - Optional.
1 ISYS Triggers. 2 Agenda Triggers Review Correlation identifiers (pseudo records) Restrictions on triggers Trigger usage Mutating tables Enabling.
Trigger Oracle PL/SQL. Triggers Associated with a particular table Associated with a particular table Automatically executed when a particular event occurs.
Chapter 15 Introduction to PL/SQL. Chapter Objectives  Explain the benefits of using PL/SQL blocks versus several SQL statements  Identify the sections.
Database Management COP4540, SCS, FIU Oracle PL/SQL (Ch 10.5)
Database Technology Jing Shen.
Copyright  Oracle Corporation, All rights reserved. 18 Interacting with the Oracle Server.
Objectives Database triggers and syntax
PL/SQLPL/SQL Oracle10g Developer: PL/SQL Programming Chapter 9 Database Triggers.
PL/SQLPL/SQL Oracle11g: PL/SQL Programming Chapter 4 Cursors and Exception Handling.
ITEC 224 Database Programming PL/SQL Lab Cursors.
PL/SQLPL/SQL Oracle11g: PL/SQL Programming Chapter 9 Database Triggers.
PL/SQLPL/SQL Oracle10g Developer: PL/SQL Programming Chapter 9 Database Triggers.
implicit and an explicit cursor
Creating Functions. V 12 NE - Oracle 2006 Overview of Stored Functions A function is a named PL/SQL block that returns a value A function can be stored.
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.
A database trigger is a stored PL/SQL program unit associated with a specific database table. ORACLE executes (fires) a database trigger automatically.
Oracle10g Developer: PL/SQL Programming1 Objectives Named program units How to identify parameters The CREATE PROCEDURE statement Creating a procedure.
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.
What Are Subprograms? Subprograms are named PL/SQL blocks that can take parameters and be invoked. Subprograms allow decomposition of a program into logical.
Oracle9i Developer: PL/SQL Programming Chapter 6 PL/SQL Packages.
1 Copyright © 2004, Oracle. All rights reserved. PL/SQL Programming Concepts: Review.
ISYS Triggers.
CS322: Database Systems PL/ SQL PL/SQL by Ivan Bayross.
Oracle11g: PL/SQL Programming Chapter 5 Procedures.
Creating Database Triggers
Interacting with the Oracle Server
Interacting with the Oracle Server
Active Database Concepts
Interacting with the Oracle Server
PL/SQL.
Oracle11g: PL/SQL Programming Chapter 4 Cursors and Exception Handling.
Agenda Triggers Review Correlation identifiers (pseudo records)
Interacting with the Oracle Server
PL/SQL Scripting in Oracle:
ISYS Triggers.
الأزنده أ.موضي المحرج.
PL/SQL Programing : Triggers
Oracle9i Developer: PL/SQL Programming Chapter 8 Database Triggers.
Chapter 8 Advanced SQL.
PL/SQL Declaring Variables.
Triggers.
Prof. Arfaoui. COM390 Chapter 9
TRIGGERS.
Presentation transcript:

School of Computing and Management Sciences © Sheffield Hallam University SQL is non-procedural –designed to be relatively approachable to non- programmers PL/SQL is a procedural extension to SQL –many Oracle products are themselves written in pl/sql –but it IS proprietary What is PL/SQL?

School of Computing and Management Sciences © Sheffield Hallam University Standard DML commands SQL functions SQL transaction control creation and control of “cursors” flow control - IF, WHEN, LOOP…. Error Handling But not DDL – dynamic sql What can be done with PL/SQL?

School of Computing and Management Sciences © Sheffield Hallam University Anonymous Block Stored Procedure or Function Application Procedure or Function Database Trigger Table trigger Application Trigger Packages Where does PL/SQL live?

School of Computing and Management Sciences © Sheffield Hallam University Modular Blocks may be nested, but each block has the following general structure: –Declaration –Executable (Begin….End) –Exception Handling PL/SQL Structure

School of Computing and Management Sciences © Sheffield Hallam University Variable declaration within DECLARE block: –part_no NUMBER(4); Two approaches to assigning values: –either: part_no := 999 ; tax := price * tax_rate ; –OR SELECT sal * 0.10 INTO bonus FROM emp WHERE empno = emp_id ; PL/SQL Variables and Constants

School of Computing and Management Sciences © Sheffield Hallam University Declaring a constant is like declaring a variable except that you must add the keyword CONSTANT and immediately assign a value to the constant. credit_limit CONSTANT REAL := ; PL/SQL Variables and Constants

School of Computing and Management Sciences © Sheffield Hallam University Cursors –pointer to tables or views –rows can be FETCHED into variables and then manipulated –They need Opening and Closing Attributes –%Type, %ROWCOUNT, %NOTFOUND Important PL/SQL concepts

Block Types Anonymous [DECLARE] BEGIN --statements [EXCEPTION] END; [DECLARE] BEGIN --statements [EXCEPTION] END; Procedure PROCEDURE name IS | AS BEGIN --statements [EXCEPTION] END; PROCEDURE name IS | AS BEGIN --statements [EXCEPTION] END; Function FUNCTION name RETURN datatype IS BEGIN --statements RETURN value; [EXCEPTION] END; FUNCTION name RETURN datatype IS BEGIN --statements RETURN value; [EXCEPTION] END;

Syntax for Creating Procedures Syntax CREATE [OR REPLACE] PROCEDURE procedure_name (parameter1 [mode1] datatype1, parameter2 [mode2] datatype2,...) IS | AS BEGIN... END; Values of variables in the parameter list are populated by the calling environment OR by the procedure OR both, depending on the mode. Mode can be IN (default), OUT or IN OUT

Triggers A trigger is a PL/SQL block that executes implicitly whenever a particular event takes place Application INSERT INTO emp....; EMPNOENAMEJOBSAL KING BLAKE SMITH SCOTT PRESIDENT MANAGER CLERK ANALYST CHECK_SAL Trigger

Creating Triggers Trigger timing −For table:BEFORE, AFTER Triggering event:INSERT, UPDATE or DELETE Trigger Type:Row or Statement When clause: Restricting condition Trigger body: PL/SQL block

Creating Statement Triggers Syntax CREATE [OR REPLACE] TRIGGER trigger_name timing event1 [OR event2 OR event3] ON table_name BEGIN trigger_body END; timingBEFORE or AFTER eventINSERT, UPDATE or DELETE trigger bodyPL/SQL block (DECLARE – BEGIN – END)

Creating Statement Triggers Example CREATE OR REPLACE TRIGGER secure_emp BEFORE INSERT ON emp BEGIN IF (TO_CHAR(sysdate, 'DY') IN ('SAT', 'SUN') ) OR (TO_CHAR(sysdate, 'HH24') NOT BETWEEN '08' AND '18') THEN RAISE_APPLIATION_ERROR (-20500, 'You may only insert into EMP during normal hours.'); END IF; END; /

CREATE OR REPLACE TRIGGER derive_commission_pct BEFORE INSERT OR UPDATE OF sal ON emp FOR EACH ROW BEGIN IF NOT (:new.job IN ('MANAGER', 'PRESIDENT') ) THEN RAISE_APPLICATION_ERROR (-20202, 'Employee cannot earn this amount'); END IF; END; / Creating Row Triggers Example Within a ROW Trigger only :new.columnreferences the new value of the column :old.columnreferences the old value of the column (The : prefix is NOT required in the WHEN restricting condition)

School of Computing and Management Sciences © Sheffield Hallam University PL/SQL building a Procedure