SQL enables us to create, organize, retrieve and maintain data stored in database it does not provide the features which a typical programming language.

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.
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.
PL/SQL The Oracle Programming Language. Purpose SQL is a non-procedural language. Often we need to put structure on transactions, so we use the supplemental.
Introduction to PL/SQL Lecture 0 – Self Study Akhtar Ali.
Introduction to PL/SQL Chapter 9. Objectives Explain the need for PL/SQL Explain the benefits of PL/SQL Identify the different types of PL/SQL blocks.
Bordoloi and Bock PL/SQL : INTRODUCTION. Bordoloi and BockPL/SQL PL/SQL is Oracle's procedural language extension to SQL, the non-procedural relational.
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.
PL / SQL P rocedural L anguage / S tructured Q uery L anguage Chapter 7 in Lab Reference.
JavaScript, Fifth Edition Chapter 1 Introduction to JavaScript.
EE Copyright س Oracle Corporation, All rights reserved. ® Review of PL/SQL.
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.
6 Copyright © 2009, Oracle. All rights reserved. Working with Composite Data Types.
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.
11 Copyright س Oracle Corporation, All rights reserved. ® Overview of PL/SQL.
L/O/G/O Introduction to PL/SQL Oracle Database 10g Develop PLSQL Program Units.
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.
PL/SQL : INTRODUCTION. PL/SQL PL/SQL is Oracle's procedural language extension to SQL, the non-procedural relational database language. With PL/SQL, you.
Program with PL/SQL Lesson 5. Working with Composite Data Types.
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.
Copyright © 2010 Certification Partners, LLC -- All Rights Reserved Perl Specialist.
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.
1 Copyright © 2004, Oracle. All rights reserved. Introduction to 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.
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 -
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.
SQL Fundamentals  SQL: Structured Query Language is a simple and powerful language used to create, access, and manipulate data and structure in the database.
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 9: Advanced SQL and PL/SQL Guide to Oracle 10g.
Copyright © 2003 ProsoftTraining. All rights reserved. Perl Fundamentals.
Declaring PL/SQL Variables
PL/SQL INTRODUCTION.
Oracle10g Developer: PL/SQL Programming1 Objectives Named program units How to identify parameters The CREATE PROCEDURE statement Creating a procedure.
Creating Procedures. PL/SQL Program Construct Tools Constructs Anonymous Block Application procedures or functions Application packages Application Triggers.
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.
PL/SQL Writing Executable Statements. PL/SQL Block Syntax and Guidelines Statement can be split across lines, but keywords must not be split Lexical units.
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.
Stored Procedures and Functions Pemrograman Basis Data MI2183.
CS422 Principles of Database Systems Oracle PL/SQL Chengyu Sun California State University, Los Angeles.
1 Copyright © 2004, Oracle. All rights reserved. PL/SQL Programming Concepts: Review.
PL/SQL Declaring Variables Writing Executable Statements
CS322: Database Systems PL/ SQL PL/SQL by Ivan Bayross.
Declaring Variables Schedule: Timing Topic 45 minutes Lecture
Interacting with the Oracle Server
PL/SQL.
Oracle11g: PL/SQL Programming Chapter 2 Basic PL/SQL Block Structures.
PL/SQL Scripting in Oracle:
Working with Composite Datatypes
PRACTICE OVERVIEW PL/SQL Part - 2.
Chapter 2 Handling Data in PL/SQL Blocks Oracle9i Developer:
Database Management Systems 2
PL/SQL week10.
Chapter 8 Advanced SQL.
PL/SQL Declaring Variables.
MATERI PL/SQL Procedures Functions Packages Database Triggers
Presentation transcript:

SQL enables us to create, organize, retrieve and maintain data stored in database it does not provide the features which a typical programming language offers, such as control-of-flow construct ( sequence, selection and iteration construct ), or the facility to declare and use variables Though SQL is the natural language of the DBA, it suffers from various inherent disadvantages, when used as a conventional programming language. Although SQL is a very powerful tool, it's set of disadvantages prevent it from being a fully structured programming language. For a fully structured programming language, Oracle provides PL/SQL. PL/SQL PL/SQL is Oracle's Procedural Language extension to SQL.

Advantages of PL/SQL for the Developer and the Database Administrator : 1. PL/SQL is development tool that not only supports SQL data manipulation but also provides facilities of conditional checking, branching and looping. 2. PL/SQL sends an entire block of statements to the Oracle engine at one time. The communication between the program block and the Oracle engine reduces considerably. This in turn reduces network traffic. 3. PL/SQL also permits dealing with errors as required, and facilitates displaying user-friendly messages, when errors are encountered. 4. PL/SQL allows declaration and use of variables in blocks of code. These variables can be used to store intermediate results of a query for later processing, or calculate values and insert them into an Oracle table later. 5. Via PL/SQL, all sorts of calculations can be done quickly and efficiently without the use of the Oracle engine.

Benefits of PL/SQL  Integration Application Oracle Server Sharedlibrary

Benefits of PL/SQL Application Other DBMSs Application Oracle with PL/SQL SQL SQL SQL SQL SQLIF...THENSQLELSESQL END IF; SQL

Benefits of PL/SQL Improved maintenance Improved maintenance Improved data security and integrity Improved data security and integrity Improved performance Improved performance

Anonymous block Application trigger Stored procedure/ function Database trigger Application procedure/ function PackagePackage DECLARE BEGIN EXCEPTION END; PL/SQL Program Constructs

PL/SQL Anonymous Block Structure  DECLARE (optional) Declare PL/SQL objects to be used within this block  BEGIN (mandatory) Define the executable statements  EXCEPTION (optional) Define the actions that take place if an error arises  END; (mandatory)

Subprogram block structure  Header  IS|AS Declaration section  BEGIN Executable section  EXCEPTION (optional) Exception section  END;

PL/SQL The character set: The basic character set includes the following:  uppercase alphabets { A - Z }.  lowercase alphabets {a-z }  numerals { }`  symbols: () + - */ <> = !; %,“ # $ ^& _ \ { } ? [ ] Words used in a PL/SQL block are called Lexical Units. Blank spaces can be freely insert between lexical units in a PL/SQL block. The spaces have no effect on the PL/SQL block. PL/SQL Data Types Both PL/ SQL and Oracle have their foundations in SQL. Most PL/SQL data types are native to Oracle's data dictionary. hence, there is a very easy integration of PL/SQL code with the Oracle Engine. The default data types that can be declared in PL%SQL are number (for storing numeric data}, char (for storing character data}, date (for storing date and time data}, boolean (for storing TRUE, FALSE or NULL), number, char and date data types can have NULL values. The %TYPE attribute provides for further integration. PL/SQL can use the %TYPE attribute to declare variables based on definitions of columns in a table.

Use of Variables  Use variables for: Temporary storage of data Temporary storage of data Manipulation of stored values Manipulation of stored values Reusability Reusability Ease of maintenance Ease of maintenance

Handling Variables in PL/SQL Declare and initialize variables in the declaration section. Declare and initialize variables in the declaration section. Assign new values to variables in the executable section. Assign new values to variables in the executable section. Pass values into PL/SQL blocks through parameters. Pass values into PL/SQL blocks through parameters. View results through output variables. View results through output variables.

Types of Variables PL/SQL variables: PL/SQL variables: ScalarScalar CompositeComposite ReferenceReference LOB (large objects)LOB (large objects) Non-PL/SQL variables: Bind and host variables Non-PL/SQL variables: Bind and host variables

Variables: ` Variables in PL/SQL blocks are named variables. A variable name must begin with a character and can be followed by a maximum of 29 other characters. Reserved words cannot be used as variable names unless enclosed within double quotes. Variables must be separated from each other by at least one space or by a punctuation mark. Constants: Declaring a constant is similar to declaring a variable except that you have to add the keyword 'constant' and immediately assign a value to it. Thereafter, no further assignments to the constant are possible, while the constant is within the scope of the PL/SQL block.

Naming Rules Two variables can have the same name, provided they are in different blocks. Two variables can have the same name, provided they are in different blocks. The variable name (identifier) should not be the same as the name of table columns used in the block. The variable name (identifier) should not be the same as the name of table columns used in the block. DECLARE empnoNUMBER(4); BEGIN SELECTempno INTOempno FROMemp WHERE ename = 'SMITH'; END; DECLARE empnoNUMBER(4); BEGIN SELECTempno INTOempno FROMemp WHERE ename = 'SMITH'; END;

Assigning Values to Variables v_ename := 'Maduro'; v_hiredate := '31-DEC-98'; SyntaxExamples Set a predefined hiredate for new employees. Set the employee name to “Maduro.”   identifier := expr;

Variable Initialization and Keywords  Using: Assignment operator (:=) Assignment operator (:=) DEFAULT keyword DEFAULT keyword NOT NULL constraint NOT NULL constraint

Scalar Datatypes Hold a single value Have no internal components 25-OCT OCT-99 Atlanta TRUE

Base Scalar Datatypes VARCHAR2 (maximum_length) VARCHAR2 (maximum_length) NUMBER [(precision, scale)] NUMBER [(precision, scale)] DATE DATE CHAR [(maximum_length)] CHAR [(maximum_length)] LONG LONG LONG RAW LONG RAW BOOLEAN BOOLEAN BINARY_INTEGER BINARY_INTEGER PLS_INTEGER PLS_INTEGER

Scalar Variable Declarations v_jobVARCHAR2(9); v_countBINARY_INTEGER := 0; v_total_salNUMBER(9,2) := 0; v_orderdateDATE := SYSDATE + 7; c_tax_rateCONSTANT NUMBER(3,2) := 8.25; v_validBOOLEAN NOT NULL := TRUE;  Examples

The %TYPE Attribute Declare a variable according to: Declare a variable according to: A database column definitionA database column definition Another previously declared variableAnother previously declared variable Prefix %TYPE with: Prefix %TYPE with: The database table and columnThe database table and column The previously declared variable nameThe previously declared variable name

Declaring Variables with the %TYPE Attribute  Examples v_enameemp.ename%TYPE; v_balanceNUMBER(7,2); v_min_balancev_balance%TYPE := 10; v_enameemp.ename%TYPE; v_balanceNUMBER(7,2); v_min_balancev_balance%TYPE := 10;

Declaring Boolean Variables Only the values TRUE, FALSE, and NULL can be assigned to a Boolean variable. Only the values TRUE, FALSE, and NULL can be assigned to a Boolean variable. The variables are connected by the logical operators AND, OR, and NOT. The variables are connected by the logical operators AND, OR, and NOT. The variables always yield TRUE, FALSE, or NULL. The variables always yield TRUE, FALSE, or NULL. Arithmetic, character, and date expressions can be used to return a Boolean value. Arithmetic, character, and date expressions can be used to return a Boolean value.

Composite Datatypes PL/SQL TABLES PL/SQL TABLES PL/SQL RECORDS PL/SQL RECORDS

LOB Datatype Variables Recipe(CLOB) Photo(BLOB) Movie(BFILE) NCLOB

Bind Variables Server O/S Bind Variable

Referencing Non-PL/SQL Variables  Store the annual salary into a SQL*Plus host variable. Reference non-PL/SQL variables as host variables. Reference non-PL/SQL variables as host variables. Prefix the references with a colon (:). Prefix the references with a colon (:).  Store the annual salary into a SQL*Plus host variable. Reference non-PL/SQL variables as host variables. Reference non-PL/SQL variables as host variables. Prefix the references with a colon (:). Prefix the references with a colon (:). :g_monthly_sal := v_sal / 12;

Displaying user Messages on the Screen: Programming tools require a method through which message can be displayed to the user on the VDU screen. DBMS_OUTPUT is a package that includes a number of procedure and functions that accumulate information in a buffer so that it can be retrieved later. These functions can also be used to display messages to the user. PUT_LINE: Put a piece of information in the package buffer followed by an end-of-line marker, It can also be used to display message to the user. Put line expects a single parameter of character data type. if used to display a message, it is the message ‘string’. To display messages to the user the SERVEROUTPUT should be set to ON. SERVEROUTPUT is a SQL*PLUS environment parameter that displays the information passed as a parameter to the PUT_LINE function. Syntax SET SERVEROUTPUT (ON/OFF]

Comments : A comment can have two forms'  The comment line begins with a double hyphen (--). The entire line will be treated as a comment.  The comment line begins with a slash followed by an asterisk (/*) till the occurrence of an asterisk followed by a slash (*/). All lines within are treated as comments. This form of specifying comments can be used to span across multiple lines. This technique can also be used to enclose a section of a PL/SQL block that temporarily needs to be isolated and ignored.

Summary PL/SQL blocks are composed of the following sections: PL/SQL blocks are composed of the following sections: Declarative (optional)Declarative (optional) Executable (required)Executable (required) Exception handling (optional)Exception handling (optional) A PL/SQL block can be an anonymous block, procedure, or function. A PL/SQL block can be an anonymous block, procedure, or function. DECLARE BEGIN EXCEPTION END;