Oracle PL/SQL Language. CIS 331: Introduction to Database Systems



Similar documents
An Introduction to PL/SQL. Mehdi Azarmi

ORACLE 9I / 10G / 11G / PL/SQL COURSE CONTENT

Triggers & Packages. {INSERT [OR] UPDATE [OR] DELETE}: This specifies the DML operation.

Darshan Institute of Engineering & Technology PL_SQL

STUDY OF PL/SQL BLOCK AIM: To Study about PL/SQL block.

Creating PL/SQL Blocks. Copyright 2007, Oracle. All rights reserved.

SQL/PSM. Outline. Database Application Development Oracle PL/SQL. Why Stored Procedures? Stored Procedures PL/SQL. Embedded SQL Dynamic SQL

Persistent Stored Modules (Stored Procedures) : PSM

COMS20700 DATABASES 13 PL/SQL. COMS20700 Databases Dr. Essam Ghadafi

1 Stored Procedures in PL/SQL 2 PL/SQL. 2.1 Variables. 2.2 PL/SQL Program Blocks

PL/SQL MOCK TEST PL/SQL MOCK TEST I

Oracle For Beginners Page : 1

Handling Exceptions. Copyright 2006, Oracle. All rights reserved. Oracle Database 10g: PL/SQL Fundamentals 8-1

Why programming extensions? to program complex update transactions. where referential integrity is not addressed by data definition

Review your answers, feedback, and question scores below. An asterisk (*) indicates a correct answer.

When an exception occur a message which explains its cause is received. PL/SQL Exception message consists of three parts.

Training Guide. PL/SQL for Beginners. Workbook

CSC 443 Database Management Systems. The SQL Programming Language

Answers to the Try It Yourself Sections

PL/SQL Overview. Basic Structure and Syntax of PL/SQL

Recognizing PL/SQL Lexical Units. Copyright 2007, Oracle. All rights reserved.

Oracle 11g PL/SQL training

Oracle Database: SQL and PL/SQL Fundamentals

PL/SQL (Cont d) Let s start with the mail_order database, shown here:

SQL Programming. CS145 Lecture Notes #10. Motivation. Oracle PL/SQL. Basics. Example schema:

Handling Exceptions. Copyright 2008, Oracle. All rights reserved.

Oracle Database: SQL and PL/SQL Fundamentals NEW

1 Triggers. 2 PL/SQL Triggers

Writing Control Structures

DECLARATION SECTION. BODY STATEMENTS... Required

Handling Exceptions. Schedule: Timing Topic 45 minutes Lecture 20 minutes Practice 65 minutes Total

Database programming 20/08/2015. DBprog news. Outline. Motivation for DB programming. Using SQL queries in a program. Using SQL queries in a program

Oracle PL/SQL Best Practices

What is the value of SQL%ISOPEN immediately after the SELECT statement is executed? Error. That attribute does not apply for implicit cursors.

Oracle 10g PL/SQL Training

Handling Exceptions. Schedule: Timing Topic. 45 minutes Lecture 20 minutes Practice 65 minutes Total

Oracle Internal & Oracle Academy

Handling PL/SQL Errors

2. Which of the following declarations is invalid? Mark for Review (1) Points

A basic create statement for a simple student table would look like the following.

Data Masking. Procedure

Introduction to PL/SQL Programming

GENERAL PROGRAMMING LANGUAGE FUNDAMENTALS

Oracle Database: SQL and PL/SQL Fundamentals

PL/SQL TUTORIAL. Simply Easy Learning by tutorialspoint.com. tutorialspoint.com

Oracle For Beginners Page : 1

Chapter 8: Introduction to PL/SQL 1

Oracle PL/SQL Injection

Oracle Database: Develop PL/SQL Program Units

Database Programming with PL/SQL: Learning Objectives

Review your answers, feedback, and question scores below. An asterisk (*) indicates a correct answer.

Oracle Database: Program with PL/SQL

Structured Query Language. Telemark University College Department of Electrical Engineering, Information Technology and Cybernetics

Handling PL/SQL Errors

The Sins of SQL Programming that send the DB to Upgrade Purgatory Abel Macias. 1 Copyright 2011, Oracle and/or its affiliates. All rights reserved.

SQL. Short introduction

Some Naming and Coding Standards

Oracle Database 10g: Program with PL/SQL


Physical Design. Meeting the needs of the users is the gold standard against which we measure our success in creating a database.

Topics Advanced PL/SQL, Integration with PROIV SuperLayer and use within Glovia

PL/SQL Programming Concepts: Review. Copyright 2004, Oracle. All rights reserved.

5.1 Database Schema Schema Generation in SQL

Programming Database lectures for mathema

Course Objectives. Database Applications. External applications. Course Objectives Interfacing. Mixing two worlds. Two approaches

MONASH UNIVERSITY. Faculty of Information Technology

PL / SQL Basics. Chapter 3

Oracle For Beginners Page : 1

PL/SQL: Introduction. Overview. Anonymous Blocks and Basic Language Features

Oracle Database 11g Express Edition PL/SQL and Database Administration Concepts -II

Oracle PL/SQL Programming

Duration Vendor Audience 5 Days Oracle Developers, Technical Consultants, Database Administrators and System Analysts

CS 632 Advanced Database Systems Object-Relational Database (ORDB) Dr. H. Assadipour

PROCEDURES, FUNCTIONS AND PACKAGES

PL/SQL. Database Procedural Programming PL/SQL and Embedded SQL. Procedures and Functions

Intro to Embedded SQL Programming for ILE RPG Developers

14 Triggers / Embedded SQL

Oracle Database: Program with PL/SQL

ISYS PL/SQL Basics. Week 03

Introduction to Oracle PL/SQL Programming V2.1 - Lessons 11-End

9 Using Triggers. Using Triggers 9-1

ORACLE DATABASE 11G: COMPLETE

Developing SQL and PL/SQL with JDeveloper

History of SQL. Relational Database Languages. Tuple relational calculus ALPHA (Codd, 1970s) QUEL (based on ALPHA) Datalog (rule-based, like PROLOG)

VARRAY AND NESTED TABLE

Oracle Database: Program with PL/SQL

Oracle Database: Program with PL/SQL

Oracle10g PL/SQL Programming V1.0

A Brief Introduction to MySQL

Oracle Database 11g: Program with PL/SQL

Table Backup and Recovery using SQL*Plus

Oracle Database 12c: Introduction to SQL Ed 1.1

OPP ODTUG Kaleidoscope. An ODTUG SP* Oracle PL/SQL Programming Conference. WOW-Wide Open World, Wide Open Web!

Duration Vendor Audience 5 Days Oracle End Users, Developers, Technical Consultants and Support Staff

Introduction to Database. Systems HANS- PETTER HALVORSEN,

Oracle Database: Program with PL/SQL

How To Create A Table In Sql (Ahem)

Oracle Database 10g: Introduction to SQL

Tutorial on Operations on Database using JDeveloper

Transcription:

Oracle PL/SQL Language CIS 331: Introduction to Database Systems

Topics: Structure of a PL/SQL program Exceptions 3-valued logic Loops (unconditional, while, for) Cursors Procedures Functions Triggers Vladimir Vacic, Temple University 2

Structure of a PL/SQL program PL/SQL code follows this scheme: -- variables -- types -- local subprograms -- procedural statements -- SQL statements EXCEPTION -- exception handling statements Vladimir Vacic, Temple University 3

A simple PL/SQL program SET SERVEROUTPUT ON -- so your printouts will show up on the screen A NUMBER; B REAL; D VARCHAR2(16); A := 13.6; B := 12; DBMS_OUTPUT.PUT_LINE('A / B = ' A/B); D := I am a happy camper'; DBMS_OUTPUT.PUT_LINE(D); Vladimir Vacic, Temple University 4

Exception handling An exception would break the execution of the program. PL/SQL provides means to handle this: A NUMBER; B REAL := 12; C NUMBER := 0; MY_EXCEPTION EXCEPTION; A := 13.6; DBMS_OUTPUT.PUT_LINE('A / B = ' A/B); -- C := 12; DBMS_OUTPUT.PUT_LINE('A / C = ' A/C); RAISE MY_EXCEPTION; EXCEPTION WHEN ZERO_DIVIDE THEN DBMS_OUTPUT.PUT_LINE('Ooooups. Division by zero!'); WHEN OTHERS THEN DBMS_OUTPUT.PUT_LINE('Ooooups. Something went wrong!'); Vladimir Vacic, Temple University 5

3-valued logic PL/SQL's boolean variables support 3-valued logic. This means that a boolean variable can take values TRUE, FALSE or NULL. D BOOLEAN := null; IF D THEN DBMS_OUTPUT.PUT_LINE('D is true.'); ELSE DBMS_OUTPUT.PUT_LINE('D is false.'); END IF; IF NOT D THEN DBMS_OUTPUT.PUT_LINE('D is false.'); ELSE DBMS_OUTPUT.PUT_LINE('D is true.'); END IF; Vladimir Vacic, Temple University 6

Nulls This can be rectified by explicitly checking for nulls: D BOOLEAN := null; IF D THEN DBMS_OUTPUT.PUT_LINE('D is true.'); ELSIF D IS NULL THEN DBMS_OUTPUT.PUT_LINE('D is null.'); ELSE DBMS_OUTPUT.PUT_LINE('D is false.'); END IF; Vladimir Vacic, Temple University 7

Loops, unconditional loop An example of an unconditional loop: i NUMBER := 1; LOOP EXIT WHEN i>10; DBMS_OUTPUT.PUT_LINE(i); i := i+1; END LOOP; Vladimir Vacic, Temple University 8

While loop An example of a while loop: i INTEGER := 1; WHILE i < 10 LOOP DBMS_OUTPUT.PUT_LINE(i); i := i+1; END LOOP; Vladimir Vacic, Temple University 9

For loop An example of a for loop: FOR k IN 1..10 LOOP -- note that k was not declared DBMS_OUTPUT.PUT_LINE(k); END LOOP; Vladimir Vacic, Temple University 10

Cursors So far PL/SQL looked like a regular (although primitive) programming language. Let us introduce some special data types: i INTEGER; j i%type; name1 VARCHAR2(32); name2 students.last_name%type; student_rec students%rowtype; -- sort of like struct in C CURSOR s1 IS -- and now the plot thickens: SELECT first_name, last_name FROM students; student_name s1%rowtype; OPEN s1; LOOP FETCH s1 INTO student_name; EXIT WHEN s1%notfound; DBMS_OUTPUT.PUT_LINE(student_name.first_name ' ' student_name.last_name); END LOOP; CLOSE s1; Vladimir Vacic, Temple University 11

More cursors CURSOR p1 IS SELECT first_name, last_name FROM professors; professor_name p1%rowtype; FOR professor_name IN p1 LOOP DBMS_OUTPUT.PUT_LINE(professor_name.first_name ' ' professor_name.last_name); END LOOP; Vladimir Vacic, Temple University 12

Cursors with parameters CURSOR class_cursor (class_name VARCHAR2) IS SELECT c.code, c.name FROM classes c, (select code from departments) b WHERE name like '%' class_name '%' AND c.department = b.code ; class_row class_cursor%rowtype; OPEN class_cursor ('Comp'); LOOP FETCH class_cursor INTO class_row; EXIT WHEN class_cursor%notfound; DBMS_OUTPUT.PUT_LINE('Code: ' class_row.code ' Name: ' class_row.name); END LOOP; CLOSE class_cursor; Vladimir Vacic, Temple University 13

Program units Typical PL/SQL program units are: procedures functions database triggers packages Vladimir Vacic, Temple University 14

Procedures A sample procedure print a name for a given SSN: SET SERVEROUTPUT ON CREATE OR REPLACE PROCEDURE names (ssnum IN VARCHAR) IS CURSOR p1 IS SELECT first_name, last_name FROM professors WHERE ssn = ssnum; professor_name p1%rowtype; FOR professor_name IN p1 LOOP DBMS_OUTPUT.PUT_LINE(professor_name.first_name ' ' professor_name.last_name); END LOOP; Vladimir Vacic, Temple University 15

Calls and errors Which can then be called by EXECUTE names('2345789078'); SHO ERR (abbrev. from show errors) - useful for debugging Vladimir Vacic, Temple University 16

Functions A sample function return someone s age in years given his or her birthday: CREATE OR REPLACE FUNCTION age (dob IN DATE) RETURN NUMBER IS current_age NUMBER; current_age := TRUNC((SYSDATE - dob) / 365.25); RETURN current_age; Vladimir Vacic, Temple University 17

Functions Which can then be used in SQL*Plus client as any other function: SELECT sysdate() ; FROM dual SELECT age(to_date('08-12-1978', 'mm-dd-yyyy')) AS age FROM dual ; Vladimir Vacic, Temple University 18

Triggers Syntax of Oracle triggers (taken from the Oracle PL/SQL manual): CREATE OR REPLACE TRIGGER trigger_name [BEFORE AFTER] [INSERT UPDATE DELETE] ON table_name [FOR EACH ROW] [WHEN condition] -- -- trigger body -- Vladimir Vacic, Temple University 19