Database 1 4 - 1 An introduction to using Oracle PL/SQL An introduction to using Oracle PL/SQL An introduction to using Oracle PL/SQL Definition: PL/SQL.

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.
SQL*PLUS, PLSQL and SQLLDR Ali Obaidi. SQL Advantages High level – Builds on relational algebra and calculus – Powerful operations – Enables automatic.
PL/SQL.
Murali Mani Persistent Stored Modules (Stored Procedures) : PSM.
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
Using Oracle PL/SQL PL/SQL stands for Procedural Language/SQL. PL/SQL extends SQL by adding constructs found in procedural languages, resulting in a structural.
PL/SQL Agenda: Basic PL/SQL block structure
Introduction to PL/SQL
Introduction to PL/SQL Lecture 0 – Self Study Akhtar Ali.
Objectives Why PL-SQL ? Language features
Phonegap Bridge – File System CIS 136 Building Mobile Apps 1.
Cursors in Pl/SQL Database 1. Practice. Sample Database The schema of the sample database is the following: Drinkers (name, occupation, birthday, salary)
Introduction to PL/SQL. Procedural Language extension for SQL Oracle Proprietary 3GL Capabilities Integration of SQL Portable within Oracle data bases.
PL / SQL P rocedural L anguage / S tructured Q uery L anguage Chapter 7 in Lab Reference.
SQL Training Procedures & Functions. Confidential & Proprietary Copyright © 2009 Cardinal Directions, Inc. DB Procedures & Functions Procedures and Functions.
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.
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.
PL/SQL CSE2132 Database Systems Week 9 Lecture PL/SQL and Programming in Oracle - 2.
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.
1 Theory, Practice & Methodology of Relational Database Design and Programming Copyright © Ellis Cohen Cursors These slides are licensed under.
PL/SQL A BRIEF OVERVIEW DAVID WILSON. PL/SQL User’s Guide and Reference PL/SQL User’s Guide and Reference.
PL/SQL Cursors Session - II. Attributes Attributes %TYPE %ROWTYPE % Found % NotFound % RowCount % IsOPen %TYPE %ROWTYPE % Found % NotFound % RowCount.
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.
Advanced SQL: Cursors & Stored Procedures
LECTURE 1 INTRODUCTION TO PL/SQL Tasneem Ghnaimat.
CS178 Database Management PL/SQL session 8 References: ORACLE 9i PROGRAMMING A Primer Rajshekhar Sunderraman.
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 CursorsCursors. 2 SQL Cursor A cursor is a private SQL work area. A cursor is a private SQL work area. There are two types of cursors: There are two.
Oracle PL/SQL. PL/SQL Originally modeled after ADA Originally modeled after ADA Created for Dept. of DefenseCreated for Dept. of Defense Allows expanded.
PL/SQL Block Structure DECLARE - Optional Variables, cursors, user-defined exceptions BEGIN - Mandatory SQL Statements PL/SQL Statements EXCEPTIONS - Optional.
PL/SQL Declaring Variables PL/SQL Block Structure DECLARE (Optional) Variables, cursors, user-defined exceptions BEGIN (Mandatory) - SQL statements -
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.
PL/SQL. Introduction to PL/SQL block Declare declarations Begin executable statement Exception exception handlers End;
Database Application Development using PL/SQL Programming.
Database Management COP4540, SCS, FIU Oracle PL/SQL (Ch 10.5)
SQL Fundamentals  SQL: Structured Query Language is a simple and powerful language used to create, access, and manipulate data and structure in the database.
School of Computing and Management Sciences © Sheffield Hallam University SQL is non-procedural –designed to be relatively approachable to non- programmers.
1 PL/SQLPL/SQL Declaring Variables Declaring Variables Declaring Variables Declaring Variables Writing Executable Statements Writing Executable Statements.
Copyright  Oracle Corporation, All rights reserved. 16 Declaring Variables.
Chapter 16 Cursors and Exceptions. Chapter Objectives  Determine when an explicit cursor is required  Declare, open, and close an explicit cursor 
implicit and an explicit cursor
Academic Year 2015 Autumn. MODULE CC2006NI: Data Modelling and Database Systems Academic Year 2015 Autumn.
Declaring PL/SQL Variables
PL/SQL INTRODUCTION.
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 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.
PLSQL Cont…. Most Common Oracle Data Types VARCHAR2 –Stores variable-length character data. –Takes a required parameter that specifies a maximum length.
C LANGUAGE MULITPLE CHOICE QUESTION SET-2
Introduction To Oracle
PL/SQL.
Writing Control Structures
SQL PL/SQL Presented by: Dr. Samir Tartir
Working with Composite Datatypes
Chapter 2 Handling Data in PL/SQL Blocks Oracle9i Developer:
PL/SQL week10.
PL/SQL Declaring Variables.
Chapter 8 Advanced SQL Pearson Education © 2009.
Database Programming Using Oracle 11g
Database Programming Using Oracle 11g
Presentation transcript:

Database An introduction to using Oracle PL/SQL An introduction to using Oracle PL/SQL An introduction to using Oracle PL/SQL Definition: PL/SQL is an Oracle’s procedural extension of SQL, it is an advanced fourth-generation programming language (4GL)..

Database Oracle PL/SQL Objective: PL/SQL PL/SQL offers modern features such as data encapsulation, overloading, collection types, exception handling, and information hiding. offers modern features such as data encapsulation, overloading, collection types, exception handling, and information hiding. improve database performance, make your applications simpler and easier to maintain for an Oracle developer or DBA. improve database performance, make your applications simpler and easier to maintain for an Oracle developer or DBA.

Database Oracle PL/SQL Advantages of Oracle PLSQL PL/SQL has over other programming languages its tight coupling with the Oracle database. PL/SQL has over other programming languages its tight coupling with the Oracle database. This makes PL/SQL programs easier to write and This makes PL/SQL programs easier to write and very efficient at accessing the database. very efficient at accessing the database. Reduced Network Traffic Reduced Network Traffic

Database Oracle PL/SQL Advantages of Oracle PLSQL Advantages of Oracle PLSQL PLSQL is also fully portable. You can use PLSQL is also fully portable. You can use PL/SQL on any platform which runs Oracle PL/SQL on any platform which runs Oracle and you can transfer PLSQL programs from and you can transfer PLSQL programs from one platform to another without having to one platform to another without having to recompile them. recompile them.

Database Oracle PL/SQL Disadvantages of Oracle PLSQL Proprietary to Oracle Proprietary to Oracle PL/SQL is proprietary to Oracle (means if you change database then you would have to re-write all your Oracle PL/SQL programs or write your applications in a database-neutral language like Java, VB or C and use the APIs to access the database.

Database Oracle PL/SQL Disadvantages of Oracle PLSQL Poor I/O Features Poor I/O Features Oracle PL/SQL has very little support for i/o either to read/write files or to read from or write to a user interface. PLSQL is designed for manipulating information from the database and not for processing files or communicating with users. Oracle PL/SQL has very little support for i/o either to read/write files or to read from or write to a user interface. PLSQL is designed for manipulating information from the database and not for processing files or communicating with users.

Database PL/SQL Data Types Numeric types BINARY_INTEGER BINARY_INTEGER DEC DEC DECIMAL DECIMAL FLOAT FLOAT INT INT INTEGER INTEGER NATURAL NATURAL NUMBER NUMBER NUMERIC NUMERIC PLS_INTEGER PLS_INTEGER POSITIVE POSITIVE REAL REAL Numeric types BINARY_INTEGER BINARY_INTEGER DEC DEC DECIMAL DECIMAL FLOAT FLOAT INT INT INTEGER INTEGER NATURAL NATURAL NUMBER NUMBER NUMERIC NUMERIC PLS_INTEGER PLS_INTEGER POSITIVE POSITIVE REAL REAL Character types CHAR CHAR CHARACTER CHARACTER LONG LONG LONG RAW LONG RAW NCHAR NCHAR NVARCHAR2 NVARCHAR2 RAW RAW ROWID ROWID STRING STRING UROWID UROWID VARCHAR VARCHAR VARCHAR2 VARCHAR2 Composite Types RECORD RECORD TABLE TABLE VARRAY VARRAY LOB Types BFILE BFILE BLOB BLOB CLOB CLOB NCLOB NCLOB Reference Types REF CURSOR REF CURSOR REF object_type REF object_type Composite Types RECORD RECORD TABLE TABLE VARRAY VARRAY LOB Types BFILE BFILE BLOB BLOB CLOB CLOB NCLOB NCLOB Reference Types REF CURSOR REF CURSOR REF object_type REF object_type

Database PL/SQL Data Types 1- Built-in Datatypes The built-in data types comprise integer, real (floating point), character, date/time and Boolean and support the manipulation of LOB (large object) data types. The built-in data types comprise integer, real (floating point), character, date/time and Boolean and support the manipulation of LOB (large object) data types. NUMBER(precision, scale) NUMBER(precision, scale) REAL REAL FLOAT(binary precision). FLOAT(binary precision).

Database PL/SQL Data Types Character types are available as:- fixed length (CHAR(length)) fixed length (CHAR(length)) variable length (VARCHAR2(max length)) variable length (VARCHAR2(max length)) 2- User-Defined Subtypes 2- User-Defined Subtypes The standard PL/SQL data types can be extended by defining your own types in the form of records and PLSQL collections. The standard PL/SQL data types can be extended by defining your own types in the form of records and PLSQL collections.

Database Oracle PL/SQL Language Elements PL/SQL the language elements consist of:- 1- loops numeric and cursor FOR loops (the latter act on the results of queries) numeric and cursor FOR loops (the latter act on the results of queries) WHILE LOOPs WHILE LOOPs Simple LOOPs Simple LOOPs

Database Oracle PL/SQL Language Elements 2-Conditional and sequential control statements IF THEN and IF THEN ELSE IF THEN and IF THEN ELSE GOTO (but the use of this is usually not necessary) GOTO (but the use of this is usually not necessary) simple and searched CASE statements simple and searched CASE statements 3- Anonymous blocks 4- Named blocks PROCEDURES PROCEDURES FUNCTIONS FUNCTIONS

Database Oracle PL/SQL block The general syntax is … The general syntax is …

Database Oracle PL/SQL block The general syntax is... The general syntax is... DECLARATIONS DECLARATIONS BEGIN /* start of anonymous block or procedure/function */ BEGIN /* start of anonymous block or procedure/function */ EXCEPTION EXCEPTION WHEN THEN ; WHEN THEN ; END ; /*end of anonymous block or procedure/function */ END ; /*end of anonymous block or procedure/function */

Database Variables and Constants Examples of variable and constant declarations: credit_limit CONSTANT NUMBER := 5000; invalid BOOLEAN := FALSE; acct_id INTEGER(4) NOT NULL DEFAULT 9999; pi CONSTANT REAL := ; postal_code VARCHAR2(20); last_name VARCHAR2(20 CHAR); my_ename emp.ename%TYPE;

Database Variables and Constants %TYPE This attribute provides the datatype of a previously declared collection, cursor variable, field, object, record, database column, or variable. Example: DECLARE my_empno emp.empno%TYPE;... BEGIN my_empno := NULL;

Database Variables and Constants %ROWTYPE This attribute provides a record type that represents a row in a database table or a row fetched from a previously declared cursor. Fields in the record and corresponding columns in the row have the same names and data types.

Database Variables and Constants %ROWTYPE Example: DECLARE emp_rec emp%ROWTYPE; CURSOR c1 IS SELECT deptno, dname, loc FROM dept; dept_rec c1%ROWTYPE; BEGIN SELECT * INTO emp_rec FROM emp WHERE...

Database PL/SQL USES TWO TYPES OF CURSORS 1- Implicit 2- Explicit. Implicitly for all SQL data manipulation statements, including queries that return only one row. Implicitly for all SQL data manipulation statements, including queries that return only one row. For queries that return more than one row, you must declare an explicit cursor, use a cursor FOR loop. 1- Implicit 2- Explicit. Implicitly for all SQL data manipulation statements, including queries that return only one row. Implicitly for all SQL data manipulation statements, including queries that return only one row. For queries that return more than one row, you must declare an explicit cursor, use a cursor FOR loop.

Database Cursor declarations Cursor variable is used a to access the information after executing a multi-row query (Oracle opens an unnamed work area that stores processing information).

Database Cursor declarations Examples of cursor declarations : CURSOR c1 IS SELECT empno, ename, job, sal FROM emp WHERE sal > 2000; CURSOR c2 RETURN dept%ROWTYPE IS SELECT * FROM dept WHERE deptno = 10;

Database CURSORCURSOR Every explicit cursor and cursor variable has four characteristics: %FOUND %FOUND %ISOPEN %ISOPEN %NOTFOUND %NOTFOUND %ROWCOUNT. %ROWCOUNT. Every explicit cursor and cursor variable has four characteristics: %FOUND %FOUND %ISOPEN %ISOPEN %NOTFOUND %NOTFOUND %ROWCOUNT. %ROWCOUNT.

Database %ISOPEN This is a cursor attribute that can be appended to the name of a cursor or cursor variable. If a cursor is open, cursor_name%ISOPEN yields TRUE; otherwise, it yields FALSE.

Database %NOTFOUND This is a cursor attribute that can be appended to the name of a cursor or cursor variable. Before the first fetch from an open cursor, cursor_name%NOTFOUND yields NULL. Thereafter, it yields FALSE if the last fetch returned a row, or TRUE if the last fetch failed to return a row.

Database %ROWCOUNT This is a cursor attribute that can be appended to the name of a cursor or cursor variable. When a cursor is opened, %ROWCOUNT is zeroed. Before the first fetch, cursor_name%ROWCOUNT yields 0. Thereafter, it yields the number of rows fetched so far.

Database declare CURSOR my_cursor IS SELECT sal *12 anual, ename FROM emp; my_rec my_cursor%ROWTYPE; BEGIN OPEN my_cursor; LOOP FETCH my_cursor INTO my_rec; EXIT WHEN my_cursor%NOTFOUND; IF my_rec.anual > 2000 THEN INSERT INTO temp VALUES (NULL, my_rec.anual, my_rec.ename); END IF; END LOOP; CLOSE my_cursor; END;declare CURSOR my_cursor IS SELECT sal *12 anual, ename FROM emp; my_rec my_cursor%ROWTYPE; BEGIN OPEN my_cursor; LOOP FETCH my_cursor INTO my_rec; EXIT WHEN my_cursor%NOTFOUND; IF my_rec.anual > 2000 THEN INSERT INTO temp VALUES (NULL, my_rec.anual, my_rec.ename); END IF; END LOOP; CLOSE my_cursor; END; Example

Database Conditional Statements in PL/SQL 1)IF THEN ELSE STATEMENT IF condition THEN statement 1; ELSE statement 2; END IF;

Database IF THEN ELSIF ELSE STATEMENT 2)IF condition 1 THEN 2)IF condition 1 THEN statement 1; statement 1; statement 2; statement 2; ELSIF condtion2 THEN statement 3; statement 3;ELSE statement 4; statement 4; END IF 2)IF condition 1 THEN 2)IF condition 1 THEN statement 1; statement 1; statement 2; statement 2; ELSIF condtion2 THEN statement 3; statement 3;ELSE statement 4; statement 4; END IF

Database CASE statements CASE CASE WHEN x = 1 THEN sequence_of_statements_1; WHEN x = 1 THEN sequence_of_statements_1; WHEN x = 2 THEN sequence_of_statements_2; WHEN x = 2 THEN sequence_of_statements_2; WHEN x = 3 THEN sequence_of_statements_3; WHEN x = 3 THEN sequence_of_statements_3; WHEN x = 4 THEN sequence_of_statements_4; WHEN x = 4 THEN sequence_of_statements_4; WHEN x = 5 THEN sequence_of_statements_5; WHEN x = 5 THEN sequence_of_statements_5; ELSE sequence_of_statements_N; ELSE sequence_of_statements_N; END CASE; END CASE; CASE CASE WHEN x = 1 THEN sequence_of_statements_1; WHEN x = 1 THEN sequence_of_statements_1; WHEN x = 2 THEN sequence_of_statements_2; WHEN x = 2 THEN sequence_of_statements_2; WHEN x = 3 THEN sequence_of_statements_3; WHEN x = 3 THEN sequence_of_statements_3; WHEN x = 4 THEN sequence_of_statements_4; WHEN x = 4 THEN sequence_of_statements_4; WHEN x = 5 THEN sequence_of_statements_5; WHEN x = 5 THEN sequence_of_statements_5; ELSE sequence_of_statements_N; ELSE sequence_of_statements_N; END CASE; END CASE;

Database CASE statement can be used with predefined selector CASE x WHEN 1 THEN sequence_of_statements_1; WHEN 1 THEN sequence_of_statements_1; WHEN 2 THEN sequence_of_statements_2; WHEN 2 THEN sequence_of_statements_2; WHEN 3 THEN sequence_of_statements_3; WHEN 3 THEN sequence_of_statements_3; WHEN 4 THEN sequence_of_statements_4; WHEN 5 THEN sequence_of_statements_5; WHEN 5 THEN sequence_of_statements_5; ELSE sequence_of_statements_N; ELSE sequence_of_statements_N; END CASE; END CASE; CASE x WHEN 1 THEN sequence_of_statements_1; WHEN 1 THEN sequence_of_statements_1; WHEN 2 THEN sequence_of_statements_2; WHEN 2 THEN sequence_of_statements_2; WHEN 3 THEN sequence_of_statements_3; WHEN 3 THEN sequence_of_statements_3; WHEN 4 THEN sequence_of_statements_4; WHEN 5 THEN sequence_of_statements_5; WHEN 5 THEN sequence_of_statements_5; ELSE sequence_of_statements_N; ELSE sequence_of_statements_N; END CASE; END CASE;

Database examplesexamples CASE WHEN a < b THEN 'hello' WHEN d < e THEN 'goodbye‘ END CASE CASE WHEN supplier_name = 'IBM' and supplier_type = 'Hardware' THEN 'North office‘ WHEN supplier_name = 'IBM' and supplier_type = 'Hardware' THEN 'North office‘ WHEN supplier_name = 'IBM' and supplier_type = 'Software' THEN 'South office‘ WHEN supplier_name = 'IBM' and supplier_type = 'Software' THEN 'South office‘END CASE WHEN a < b THEN 'hello' WHEN d < e THEN 'goodbye‘ END CASE CASE WHEN supplier_name = 'IBM' and supplier_type = 'Hardware' THEN 'North office‘ WHEN supplier_name = 'IBM' and supplier_type = 'Hardware' THEN 'North office‘ WHEN supplier_name = 'IBM' and supplier_type = 'Software' THEN 'South office‘ WHEN supplier_name = 'IBM' and supplier_type = 'Software' THEN 'South office‘END

Database PL/SQ Simple LOOP The simplest form of LOOP statement is the basic (or infinite) loop, with the keywords LOOP and END LOOP, as follows: LOOP sequence_of_statements END LOOP;

Database EXIT The EXIT statement forces a loop to complete unconditionally. An example follows: LOOP IF credit_rating < 3 THEN EXIT; -- exit loop immediately END IF; END LOOP; -- control resumes here

Database EXIT WHEN LOOP FETCH c1 INTO... EXIT WHEN c1%NOTFOUND; -- exit loop if condition is true... END LOOP; CLOSE c1;

Database PL/SQL PL/SQL WHILE-LOOP

Database PL/SQL PL/SQL WHILE-LOOP An example WHILE total <= LOOP... SELECT sal INTO salary FROM emp WHERE... total := total + salary; END LOOP;

Database PL/SQL PL/SQL FOR-LOOP The number of iterations through a FOR loop is known before the loop is entered. FOR loops iterate over a specified range of integers. The syntax follows: FOR counter IN [REVERSE] lower_bound..higher_bound LOOP sequence_of_statements END LOOP;

Database Oracle PL/SQL for loop An example FOR i IN 1..3 LOOP -- assign the values 1,2,3 to i sequence_of_statements -- executes three times END LOOP; FOR i IN REVERSE 1..3 LOOP -- assign the values 3,2,1 to i sequence_of_statements -- executes three times END LOOP;

Database PL/SQ PL/SQ CURSOR FOR Loops PL/SQ CURSOR FOR Loops The general structure is as follows: FOR IN LOOP END LOOP;

Database Advantage Oracle automatically OPENs the cursor, the results are automatically FETCHed and when all rows have been returned, Oracle automatically CLOSEs the cursor for you. PL/SQ PL/SQ CURSOR FOR Loops

Database PL/SQ PL/SQ CURSOR FOR Loops disadvantages If the query returns no data, then the body of the loop is not executed which causes an error. Processing is exactly the same for each row returned, which means you can't initialize variables.