SlideShare a Scribd company logo
Database Programming (CS 423)
Course Coordinator:
Dr. Irshad Ahmed Abbasi
Email: aabasy@ub.edu.sa
www.ub.edu.sa â€«Ű§Ù„Ù…ŰčÙ„ÙˆÙ…Ű§ŰȘ‬ ‫وŰȘÙ‚Ù†ÙŠŰ©â€Ź â€«Ű§Ù„Ű­Ű§ŰłŰšŰ§ŰȘ‬ â€«ÙƒÙ„ÙŠŰ©â€Ź
Definition :-
PL/SQL, the Oracle procedural extension of SQL, is a portable,
high-performance transaction-processing language.
Introduction
â€«Ű§Ù„Ù…ŰčÙ„ÙˆÙ…Ű§ŰȘ‬ ‫وŰȘÙ‚Ù†ÙŠŰ©â€Ź â€«Ű§Ù„Ű­Ű§ŰłŰšŰ§ŰȘ‬ â€«ÙƒÙ„ÙŠŰ©â€Ź
Main Features of PL/SQL
PL/SQL combines the data-manipulating power of SQL with
the processing power of procedural languages.
When you can solve a problem with SQL, you can issue SQL
statements from your PL/SQL program, without learning
new APIs.
Like other procedural programming languages, PL/SQL lets
you declare constants and variables, control program flow,
define subprograms, and trap runtime errors.
You can break complex problems into easily understandable
subprograms, which you can reuse in multiple applications.
â€«Ű§Ù„Ù…ŰčÙ„ÙˆÙ…Ű§ŰȘ‬ ‫وŰȘÙ‚Ù†ÙŠŰ©â€Ź â€«Ű§Ù„Ű­Ű§ŰłŰšŰ§ŰȘ‬ â€«ÙƒÙ„ÙŠŰ©â€Ź
Types of SQL : static and dynamic SQL.
Static SQL is SQL whose full text is known at
compilation time.
Dynamic SQL is SQL whose full text is not known
until run time.
Dynamic SQL lets you make your applications
more flexible and versatile.
Types of SQL
â€«Ű§Ù„Ù…ŰčÙ„ÙˆÙ…Ű§ŰȘ‬ ‫وŰȘÙ‚Ù†ÙŠŰ©â€Ź â€«Ű§Ù„Ű­Ű§ŰłŰšŰ§ŰȘ‬ â€«ÙƒÙ„ÙŠŰ©â€Ź
PL/SQL Diagram
â€«Ű§Ù„Ù…ŰčÙ„ÙˆÙ…Ű§ŰȘ‬ ‫وŰȘÙ‚Ù†ÙŠŰ©â€Ź â€«Ű§Ù„Ű­Ű§ŰłŰšŰ§ŰȘ‬ â€«ÙƒÙ„ÙŠŰ©â€Ź
Advantages of PL/SQL
PL/SQL has these advantages:
1. Tight Integration with SQL
2. High Performance
3. High Productivity
4. Portability
5. Scalability
6. Manageability
7. Support for Object-Oriented Programming
8. Support for Developing Web Applications
9. Support for Developing Server Pages
â€«Ű§Ù„Ù…ŰčÙ„ÙˆÙ…Ű§ŰȘ‬ ‫وŰȘÙ‚Ù†ÙŠŰ©â€Ź â€«Ű§Ù„Ű­Ű§ŰłŰšŰ§ŰȘ‬ â€«ÙƒÙ„ÙŠŰ©â€Ź
Error Handling
PL/SQL makes it easy to detect and handle errors. When an
error occurs, PL/SQL raises an exception. Normal execution
stops and control transfers to the exception-handling part of the
PL/SQL block. You do not have to check every operation to
ensure that it succeeded, as in a C ++ program.
â€«Ű§Ù„Ù…ŰčÙ„ÙˆÙ…Ű§ŰȘ‬ ‫وŰȘÙ‚Ù†ÙŠŰ©â€Ź â€«Ű§Ù„Ű­Ű§ŰłŰšŰ§ŰȘ‬ â€«ÙƒÙ„ÙŠŰ©â€Ź
The basic unit of a PL/SQL source program is the block,
which groups related declarations and statements.
A PL/SQL block is defined by the keywords DECLARE, BEGIN,
EXCEPTION, and END.
Blocks contain :-
 Declarative part
 An executable part (is require)
 Exception-handling part.
 Block Label.
Declarations are local to the block and cease to exist when the block
completes execution, helping to avoid cluttered namespaces for
variables and subprograms.
Blocks can be nested: Because a block is an executable statement, it can
appear in another block wherever an executable statement is allowed.
Blocks
â€«Ű§Ù„Ù…ŰčÙ„ÙˆÙ…Ű§ŰȘ‬ ‫وŰȘÙ‚Ù†ÙŠŰ©â€Ź â€«Ű§Ù„Ű­Ű§ŰłŰšŰ§ŰȘ‬ â€«ÙƒÙ„ÙŠŰ©â€Ź
Example
<< label >> (optional)
DECLARE -- Declarative part (optional)
-- Declarations of local types, variables, & subprograms
BEGIN -- Executable part (required)
-- Statements (which can use items declared in declarative part)
[EXCEPTION -- Exception-handling part (optional)
-- Exception handlers for exceptions (errors) raised in executable part]
END;
â€«Ű§Ù„Ù…ŰčÙ„ÙˆÙ…Ű§ŰȘ‬ ‫وŰȘÙ‚Ù†ÙŠŰ©â€Ź â€«Ű§Ù„Ű­Ű§ŰłŰšŰ§ŰȘ‬ â€«ÙƒÙ„ÙŠŰ©â€Ź
Variables and Constants
PL/SQL lets you declare variables and constants, and then use
them wherever you can use an expression. As the program runs,
the values of variables can change, but the values of constants
does not.
â€«Ű§Ù„Ù…ŰčÙ„ÙˆÙ…Ű§ŰȘ‬ ‫وŰȘÙ‚Ù†ÙŠŰ©â€Ź â€«Ű§Ù„Ű­Ű§ŰłŰšŰ§ŰȘ‬ â€«ÙƒÙ„ÙŠŰ©â€Ź
Subprograms
A PL/SQL subprogram is a named PL/SQL block that can be invoked
repeatedly.If the subprogram has parameters, their values can differ
for each invocation.
PL/SQL has two types of subprograms, procedures and functions.
A function returns a result.
â€«Ű§Ù„Ù…ŰčÙ„ÙˆÙ…Ű§ŰȘ‬ ‫وŰȘÙ‚Ù†ÙŠŰ©â€Ź â€«Ű§Ù„Ű­Ű§ŰłŰšŰ§ŰȘ‬ â€«ÙƒÙ„ÙŠŰ©â€Ź
Packages
package is a schema object that groups logically related PL/SQL
types, variables, constants, subprograms, cursors, and
exceptions. A package is compiled and stored in the database,
where many applications can share its contents. You can think of
a package as an application.
â€«Ű§Ù„Ù…ŰčÙ„ÙˆÙ…Ű§ŰȘ‬ ‫وŰȘÙ‚Ù†ÙŠŰ©â€Ź â€«Ű§Ù„Ű­Ű§ŰłŰšŰ§ŰȘ‬ â€«ÙƒÙ„ÙŠŰ©â€Ź
Input and Output
â€«Ű§ï»Ÿï»€ï»Œï» ï»źï»ŁïșŽŰȘ‬ ‫ïș—ï»˜ï»šïŻżïș”‏ â€«Ùˆâ€Ź
‫ŰȘ‬ â€«Ű§ï»Ÿïș€ïșŽïșłïș’ïșŽâ€Ź â€«ï»›ï» ïŻżïș”‏
Most PL/SQL input and output (I/O) is done with SQL
statements that store data in database tables or query those
tables.
Data Abstraction
â€«Ű§ï»Ÿï»€ï»Œï» ï»źï»ŁïșŽŰȘ‬ ‫ïș—ï»˜ï»šïŻżïș”‏ â€«Ùˆâ€Ź
‫ŰȘ‬ â€«Ű§ï»Ÿïș€ïșŽïșłïș’ïșŽâ€Ź â€«ï»›ï» ïŻżïș”‏
Data abstraction lets you work with the essential properties of
data without being too involved with details. You can design a data
structure first, and then design algorithms that manipulate it.
1 Cursors
2 Composite Variables
3 %ROWTYPE Attribute
4 %TYPE Attribute
Abstract Data Types
Cursors
â€«Ű§ï»Ÿï»€ï»Œï» ï»źï»ŁïșŽŰȘ‬ ‫ïș—ï»˜ï»šïŻżïș”‏ â€«Ùˆâ€Ź
‫ŰȘ‬ â€«Ű§ï»Ÿïș€ïșŽïșłïș’ïșŽâ€Ź â€«ï»›ï» ïŻżïș”‏
A cursor is a pointer to a private SQL area that stores
information about processing a specific SQL statement or
PL/SQL SELECT INTO statement. You can use the cursor to
retrieve the rows of the result set one at a time. You can use
cursor attributes to get information about the state of the
cursor—for example, how many rows the statement has
affected so far. For more information about cursors,
Composite Variables
â€«Ű§ï»Ÿï»€ï»Œï» ï»źï»ŁïșŽŰȘ‬ ‫ïș—ï»˜ï»šïŻżïș”‏ â€«Ùˆâ€Ź
‫ŰȘ‬ â€«Ű§ï»Ÿïș€ïșŽïșłïș’ïșŽâ€Ź â€«ï»›ï» ïŻżïș”‏
A composite variable has internal components, which you can
access individually. You can pass entire composite variables to
subprograms as parameters. PL/SQL has two kinds of composite
variables, collections and records.
In a collection, the internal components are always of the same
data type, and are called elements. You access each element by
its unique index. Lists and arrays are classic examples of
collections.
In a record, the internal components can be of different data
types, and are called fields. You access each field by its name. A
record variable can hold a table row, or some columns from a
table row.
%ROWTYPE Attribute
The %ROWTYPE attribute lets you declare a record that
represents either a full or partial row of a database table or
view. For every column of the full or partial row, the record has
a field with the same name and data type. If the structure of the
row changes, then the structure of the record changes
accordingly.
â€«Ű§Ù„Ù…ŰčÙ„ÙˆÙ…Ű§ŰȘ‬ ‫وŰȘÙ‚Ù†ÙŠŰ©â€Ź â€«Ű§Ù„Ű­Ű§ŰłŰšŰ§ŰȘ‬ â€«ÙƒÙ„ÙŠŰ©â€Ź
%TYPE Attribute
The %TYPE attribute lets you declare a data item of the same data type
as a previously declared variable or column (without knowing what that
type is). If the declaration of the referenced item changes, then the
declaration of the referencing item changes accordingly.
The %TYPE attribute is particularly useful when declaring variables to
hold database values.
â€«Ű§Ù„Ù…ŰčÙ„ÙˆÙ…Ű§ŰȘ‬ ‫وŰȘÙ‚Ù†ÙŠŰ©â€Ź â€«Ű§Ù„Ű­Ű§ŰłŰšŰ§ŰȘ‬ â€«ÙƒÙ„ÙŠŰ©â€Ź
Abstract Data Types
An Abstract Data Type (ADT) consists of a data structure and
subprograms that manipulate the data. The variables that form the
data structure are called attributes. The subprograms that
manipulate the attributes are called methods.
ADTs are stored in the database. Instances of ADTs can be stored in
tables and used as PL/SQL variables.
ADTs let you reduce complexity by separating a large system into
logical components, which you can reuse.
In the static data dictionary view *_OBJECTS, the OBJECT_TYPE of an
ADT is TYPE. In the static data dictionary view *_TYPES,
the TYPECODE of an ADT is OBJECT.
â€«Ű§Ù„Ù…ŰčÙ„ÙˆÙ…Ű§ŰȘ‬ ‫وŰȘÙ‚Ù†ÙŠŰ©â€Ź â€«Ű§Ù„Ű­Ű§ŰłŰšŰ§ŰȘ‬ â€«ÙƒÙ„ÙŠŰ©â€Ź
Control Statements
â€«Ű§ï»Ÿï»€ï»Œï» ï»źï»ŁïșŽŰȘ‬ ‫ïș—ï»˜ï»šïŻżïș”‏ â€«Ùˆâ€Ź
‫ŰȘ‬ â€«Ű§ï»Ÿïș€ïșŽïșłïș’ïșŽâ€Ź â€«ï»›ï» ïŻżïș”‏
PL/SQL has three categories of control statements:
Conditional selection statements, which let you run different
statements for different data values.
Loop statements, which let you repeat the same statements with a
series of different data values.
Sequential control statements, which allow you to go to a
specified, labelled statement, or to do nothing.
A PL/SQL unit is one of these:
‱PL/SQL anonymous block
‱FUNCTION
‱LIBRARY
‱PACKAGE
‱PACKAGE BODY
‱PROCEDURE
‱TRIGGER
‱TYPE
â€«Ű§ï»Ÿï»€ï»Œï» ï»źï»ŁïșŽŰȘ‬ ‫ïș—ï»˜ï»šïŻżïș”‏ â€«Ùˆâ€Ź
‫ŰȘ‬ â€«Ű§ï»Ÿïș€ïșŽïșłïș’ïșŽâ€Ź â€«ï»›ï» ïŻżïș”‏
PL/SQL Units and Compilation Parameters
Blocks in PL SQL
Declarations
This section starts with the keyword DECLARE. It is an optional
section and defines all variables, cursors, subprograms, and other
elements to be used in the program.
Executable Commands
This section is enclosed between the keywords BEGIN and END and
it is a mandatory section. It consists of the executable PL/SQL
statements of the program. It should have at least one executable
line of code, which may be just a NULL command to indicate that
nothing should be executed.
Exception Handling
This section starts with the keyword EXCEPTION. This optional
section contains exception(s) that handle errors in the program.
â€«Ű§Ù„Ù…ŰčÙ„ÙˆÙ…Ű§ŰȘ‬ ‫وŰȘÙ‚Ù†ÙŠŰ©â€Ź â€«Ű§Ù„Ű­Ű§ŰłŰšŰ§ŰȘ‬ â€«ÙƒÙ„ÙŠŰ©â€Ź
Block Syntax
DECLARE
<declarations section>
BEGIN
<executable command(s)>
EXCEPTION
<exception handling>
END;
â€«Ű§Ù„Ù…ŰčÙ„ÙˆÙ…Ű§ŰȘ‬ ‫وŰȘÙ‚Ù†ÙŠŰ©â€Ź â€«Ű§Ù„Ű­Ű§ŰłŰšŰ§ŰȘ‬ â€«ÙƒÙ„ÙŠŰ©â€Ź
DECLARE
message varchar2(20):= 'Hello, World!';
BEGIN
dbms_output.put_line(message);
END;
/
Before this We have to set the serveroutput to on
â€«Ű§Ù„Ù…ŰčÙ„ÙˆÙ…Ű§ŰȘ‬ ‫وŰȘÙ‚Ù†ÙŠŰ©â€Ź â€«Ű§Ù„Ű­Ű§ŰłŰšŰ§ŰȘ‬ â€«ÙƒÙ„ÙŠŰ©â€Ź
BEGIN
FOR someone IN (
SELECT * FROM employees
WHERE employee_id < 120
ORDER BY employee_id )
LOOP DBMS_OUTPUT.PUT_LINE('First name = ' ||
someone.first_name || ', Last name = ' || someone.last_name);
END LOOP;
END; /
â€«Ű§Ù„Ù…ŰčÙ„ÙˆÙ…Ű§ŰȘ‬ ‫وŰȘÙ‚Ù†ÙŠŰ©â€Ź â€«Ű§Ù„Ű­Ű§ŰłŰšŰ§ŰȘ‬ â€«ÙƒÙ„ÙŠŰ©â€Ź
The end; line signals the end of the PL/SQL block.
To run the code from the SQL command line, you may need to
type / at the beginning of the first blank line after the last line of
the code.
When the above code is executed at the SQL prompt, it
produces the following result.
Hello World PL/SQL procedure successfully
completed.
â€«Ű§Ù„Ù…ŰčÙ„ÙˆÙ…Ű§ŰȘ‬ ‫وŰȘÙ‚Ù†ÙŠŰ©â€Ź â€«Ű§Ù„Ű­Ű§ŰłŰšŰ§ŰȘ‬ â€«ÙƒÙ„ÙŠŰ©â€Ź
PL/SQL Identifiers
The PL/SQL Identifiers
PL/SQL identifiers are constants, variables, exceptions,
procedures, cursors, and reserved words.
The identifiers consist of a letter optionally followed by more
letters, numerals, dollar signs, underscores, and number signs
and should not exceed 30 characters.
By default, identifiers are not case-sensitive.
So you can use integer or INTEGER to represent a numeric
value.
You cannot use a reserved keyword as an identifier.
â€«Ű§Ù„Ù…ŰčÙ„ÙˆÙ…Ű§ŰȘ‬ ‫وŰȘÙ‚Ù†ÙŠŰ©â€Ź â€«Ű§Ù„Ű­Ű§ŰłŰšŰ§ŰȘ‬ â€«ÙƒÙ„ÙŠŰ©â€Ź
The PL/SQL Delimiters
â€«Ű§Ù„Ù…ŰčÙ„ÙˆÙ…Ű§ŰȘ‬ ‫وŰȘÙ‚Ù†ÙŠŰ©â€Ź â€«Ű§Ù„Ű­Ű§ŰłŰšŰ§ŰȘ‬ â€«ÙƒÙ„ÙŠŰ©â€Ź
Delimiter Description
+, -, *, / Addition, subtraction/negation, multiplication, division
% Attribute indicator
' Characterstring delimiter
. Component selector
(,) Expression or list delimiter
: Host variable indicator
, Item separator
" Quoted identifier delimiter
= Relational operator
@ Remote access indicator
; Statementterminator
:= Assignment operator
=> Association operator
|| Concatenation operator
** Exponentiation operator
<<, >> Label delimiter (begin and end)
/*, */ Multi-line comment delimiter (begin and end)
-- Single-line comment indicator
.. Range operator
<, >, <=, >= Relational operators
<>, '=, ~=, ^= Different versions of NOT EQUAL
Program comments are explanatory statements that can be included in the
PL/SQL code that you write and helps anyone reading its source code.
All programming languages allow some form of comments.
The PL/SQL supports single-line and multi-line comments.
All characters available inside any comment are ignored by the PL/SQL
compiler.
The PL/SQL single-line comments start with the delimiter -- (double hyphen)
and
multi-line comments are enclosed by /* and */.
DECLARE
-- variable declaration
message varchar2(20):= 'Hello, World!';
BEGIN /* * PL/SQL executable statement(s) */
dbms_output.put_line(message);
END; /
The PL/SQL Comments
â€«Ű§Ù„Ù…ŰčÙ„ÙˆÙ…Ű§ŰȘ‬ ‫وŰȘÙ‚Ù†ÙŠŰ©â€Ź â€«Ű§Ù„Ű­Ű§ŰłŰšŰ§ŰȘ‬ â€«ÙƒÙ„ÙŠŰ©â€Ź

More Related Content

PPTX
PL/SQL is a block structured language that enables developers to combine the ...
DOCX
What does PL_SQL stand for and what is the functioning of PL_SQL.docx
PDF
PL/SQL Complete Tutorial. All Topics Covered
PDF
UNIT III Procedural Language and SQL.pdf
PDF
Pl sql
PDF
Pl sql
PDF
Pl sql
PPT
pl_sql.ppt
PL/SQL is a block structured language that enables developers to combine the ...
What does PL_SQL stand for and what is the functioning of PL_SQL.docx
PL/SQL Complete Tutorial. All Topics Covered
UNIT III Procedural Language and SQL.pdf
Pl sql
Pl sql
Pl sql
pl_sql.ppt

Similar to chapter 1.pdfbbbbbbbbbbbbbbbbbbbbbbbbbb (20)

DOC
3963066 pl-sql-notes-only
PDF
Oracle PL/SQL online training | PL/SQL online Training
PPT
Chapter8 pl sql
PPTX
Cursors, triggers, procedures
PDF
PDF
Pl sql
PPTX
PL SQL.pptx in computer language in database
PDF
PROCEDURAL LANGUAGE/ STRUCTURED QUERY LANGUAGE.pdf
PDF
rdbms.pdf plsql database system notes for students to study
PDF
Pl sql-ch1
PDF
PL/SQL for Beginners - PL/SQL Tutorial 1
PDF
Unit 4 rdbms study_material
PDF
Procedural Language/Structured Query Language
PPT
Oracle Intro.ppt
PDF
Oracle DBA interview_questions
DOC
Oracle etl openworld
PPTX
Pl sql chapter 1
DOC
Sql server
PDF
Dbms 2011
3963066 pl-sql-notes-only
Oracle PL/SQL online training | PL/SQL online Training
Chapter8 pl sql
Cursors, triggers, procedures
Pl sql
PL SQL.pptx in computer language in database
PROCEDURAL LANGUAGE/ STRUCTURED QUERY LANGUAGE.pdf
rdbms.pdf plsql database system notes for students to study
Pl sql-ch1
PL/SQL for Beginners - PL/SQL Tutorial 1
Unit 4 rdbms study_material
Procedural Language/Structured Query Language
Oracle Intro.ppt
Oracle DBA interview_questions
Oracle etl openworld
Pl sql chapter 1
Sql server
Dbms 2011
Ad

Recently uploaded (20)

PPTX
RTS MASTER DECK_Household Convergence Scorecards. Use this file copy.pptx
PPTX
unit1d-communitypharmacy-240815170017-d032dce8.pptx
PDF
Printing Presentation to show beginners.
PPTX
Grade 10 System Servicing for Hardware and Software
PDF
Maxon CINEMA 4D 2025 Crack Free Download Latest Version
PPTX
Group 4 [BSIT-1C] Computer Network (1).pptx
PDF
20A LG INR18650HJ2 3.6V 2900mAh Battery cells for Power Tools Vacuum Cleaner
PDF
2_STM32&SecureElements2_STM32&SecureElements
PDF
CAB UNIT 1 with computer details details
PPTX
A Clear View_ Interpreting Scope Numbers and Features
PDF
ISS2022 present sdabhsa hsdhdfahasda ssdsd
PPTX
Clauses_Part1.hshshpjzjxnznxnxnndndndndndndndnndptx
PPTX
AI_ML_Internship_WReport_Template_v2.pptx
PDF
Tcl Scripting for EDA.pdf
PPTX
PLC ANALOGUE DONE BY KISMEC KULIM TD 5 .0
PDF
ICT grade for 8. MATATAG curriculum .P2.pdf
PPTX
Presentation 1.pptxnshshdhhdhdhdhdhhdhdhdhd
PPTX
Computers and mobile device: Evaluating options for home and work
PPTX
vortex flow measurement in instrumentation
PDF
SAHIL PROdhdjejss yo yo pdf TOCOL PPT.pdf
RTS MASTER DECK_Household Convergence Scorecards. Use this file copy.pptx
unit1d-communitypharmacy-240815170017-d032dce8.pptx
Printing Presentation to show beginners.
Grade 10 System Servicing for Hardware and Software
Maxon CINEMA 4D 2025 Crack Free Download Latest Version
Group 4 [BSIT-1C] Computer Network (1).pptx
20A LG INR18650HJ2 3.6V 2900mAh Battery cells for Power Tools Vacuum Cleaner
2_STM32&SecureElements2_STM32&SecureElements
CAB UNIT 1 with computer details details
A Clear View_ Interpreting Scope Numbers and Features
ISS2022 present sdabhsa hsdhdfahasda ssdsd
Clauses_Part1.hshshpjzjxnznxnxnndndndndndndndnndptx
AI_ML_Internship_WReport_Template_v2.pptx
Tcl Scripting for EDA.pdf
PLC ANALOGUE DONE BY KISMEC KULIM TD 5 .0
ICT grade for 8. MATATAG curriculum .P2.pdf
Presentation 1.pptxnshshdhhdhdhdhdhhdhdhdhd
Computers and mobile device: Evaluating options for home and work
vortex flow measurement in instrumentation
SAHIL PROdhdjejss yo yo pdf TOCOL PPT.pdf
Ad

chapter 1.pdfbbbbbbbbbbbbbbbbbbbbbbbbbb

  • 1. Database Programming (CS 423) Course Coordinator: Dr. Irshad Ahmed Abbasi Email: aabasy@ub.edu.sa www.ub.edu.sa â€«Ű§Ù„Ù…ŰčÙ„ÙˆÙ…Ű§ŰȘ‬ ‫وŰȘÙ‚Ù†ÙŠŰ©â€Ź â€«Ű§Ù„Ű­Ű§ŰłŰšŰ§ŰȘ‬ â€«ÙƒÙ„ÙŠŰ©â€Ź
  • 2. Definition :- PL/SQL, the Oracle procedural extension of SQL, is a portable, high-performance transaction-processing language. Introduction â€«Ű§Ù„Ù…ŰčÙ„ÙˆÙ…Ű§ŰȘ‬ ‫وŰȘÙ‚Ù†ÙŠŰ©â€Ź â€«Ű§Ù„Ű­Ű§ŰłŰšŰ§ŰȘ‬ â€«ÙƒÙ„ÙŠŰ©â€Ź
  • 3. Main Features of PL/SQL PL/SQL combines the data-manipulating power of SQL with the processing power of procedural languages. When you can solve a problem with SQL, you can issue SQL statements from your PL/SQL program, without learning new APIs. Like other procedural programming languages, PL/SQL lets you declare constants and variables, control program flow, define subprograms, and trap runtime errors. You can break complex problems into easily understandable subprograms, which you can reuse in multiple applications. â€«Ű§Ù„Ù…ŰčÙ„ÙˆÙ…Ű§ŰȘ‬ ‫وŰȘÙ‚Ù†ÙŠŰ©â€Ź â€«Ű§Ù„Ű­Ű§ŰłŰšŰ§ŰȘ‬ â€«ÙƒÙ„ÙŠŰ©â€Ź
  • 4. Types of SQL : static and dynamic SQL. Static SQL is SQL whose full text is known at compilation time. Dynamic SQL is SQL whose full text is not known until run time. Dynamic SQL lets you make your applications more flexible and versatile. Types of SQL â€«Ű§Ù„Ù…ŰčÙ„ÙˆÙ…Ű§ŰȘ‬ ‫وŰȘÙ‚Ù†ÙŠŰ©â€Ź â€«Ű§Ù„Ű­Ű§ŰłŰšŰ§ŰȘ‬ â€«ÙƒÙ„ÙŠŰ©â€Ź
  • 5. PL/SQL Diagram â€«Ű§Ù„Ù…ŰčÙ„ÙˆÙ…Ű§ŰȘ‬ ‫وŰȘÙ‚Ù†ÙŠŰ©â€Ź â€«Ű§Ù„Ű­Ű§ŰłŰšŰ§ŰȘ‬ â€«ÙƒÙ„ÙŠŰ©â€Ź
  • 6. Advantages of PL/SQL PL/SQL has these advantages: 1. Tight Integration with SQL 2. High Performance 3. High Productivity 4. Portability 5. Scalability 6. Manageability 7. Support for Object-Oriented Programming 8. Support for Developing Web Applications 9. Support for Developing Server Pages â€«Ű§Ù„Ù…ŰčÙ„ÙˆÙ…Ű§ŰȘ‬ ‫وŰȘÙ‚Ù†ÙŠŰ©â€Ź â€«Ű§Ù„Ű­Ű§ŰłŰšŰ§ŰȘ‬ â€«ÙƒÙ„ÙŠŰ©â€Ź
  • 7. Error Handling PL/SQL makes it easy to detect and handle errors. When an error occurs, PL/SQL raises an exception. Normal execution stops and control transfers to the exception-handling part of the PL/SQL block. You do not have to check every operation to ensure that it succeeded, as in a C ++ program. â€«Ű§Ù„Ù…ŰčÙ„ÙˆÙ…Ű§ŰȘ‬ ‫وŰȘÙ‚Ù†ÙŠŰ©â€Ź â€«Ű§Ù„Ű­Ű§ŰłŰšŰ§ŰȘ‬ â€«ÙƒÙ„ÙŠŰ©â€Ź
  • 8. The basic unit of a PL/SQL source program is the block, which groups related declarations and statements. A PL/SQL block is defined by the keywords DECLARE, BEGIN, EXCEPTION, and END. Blocks contain :-  Declarative part  An executable part (is require)  Exception-handling part.  Block Label. Declarations are local to the block and cease to exist when the block completes execution, helping to avoid cluttered namespaces for variables and subprograms. Blocks can be nested: Because a block is an executable statement, it can appear in another block wherever an executable statement is allowed. Blocks â€«Ű§Ù„Ù…ŰčÙ„ÙˆÙ…Ű§ŰȘ‬ ‫وŰȘÙ‚Ù†ÙŠŰ©â€Ź â€«Ű§Ù„Ű­Ű§ŰłŰšŰ§ŰȘ‬ â€«ÙƒÙ„ÙŠŰ©â€Ź
  • 9. Example << label >> (optional) DECLARE -- Declarative part (optional) -- Declarations of local types, variables, & subprograms BEGIN -- Executable part (required) -- Statements (which can use items declared in declarative part) [EXCEPTION -- Exception-handling part (optional) -- Exception handlers for exceptions (errors) raised in executable part] END; â€«Ű§Ù„Ù…ŰčÙ„ÙˆÙ…Ű§ŰȘ‬ ‫وŰȘÙ‚Ù†ÙŠŰ©â€Ź â€«Ű§Ù„Ű­Ű§ŰłŰšŰ§ŰȘ‬ â€«ÙƒÙ„ÙŠŰ©â€Ź
  • 10. Variables and Constants PL/SQL lets you declare variables and constants, and then use them wherever you can use an expression. As the program runs, the values of variables can change, but the values of constants does not. â€«Ű§Ù„Ù…ŰčÙ„ÙˆÙ…Ű§ŰȘ‬ ‫وŰȘÙ‚Ù†ÙŠŰ©â€Ź â€«Ű§Ù„Ű­Ű§ŰłŰšŰ§ŰȘ‬ â€«ÙƒÙ„ÙŠŰ©â€Ź
  • 11. Subprograms A PL/SQL subprogram is a named PL/SQL block that can be invoked repeatedly.If the subprogram has parameters, their values can differ for each invocation. PL/SQL has two types of subprograms, procedures and functions. A function returns a result. â€«Ű§Ù„Ù…ŰčÙ„ÙˆÙ…Ű§ŰȘ‬ ‫وŰȘÙ‚Ù†ÙŠŰ©â€Ź â€«Ű§Ù„Ű­Ű§ŰłŰšŰ§ŰȘ‬ â€«ÙƒÙ„ÙŠŰ©â€Ź
  • 12. Packages package is a schema object that groups logically related PL/SQL types, variables, constants, subprograms, cursors, and exceptions. A package is compiled and stored in the database, where many applications can share its contents. You can think of a package as an application. â€«Ű§Ù„Ù…ŰčÙ„ÙˆÙ…Ű§ŰȘ‬ ‫وŰȘÙ‚Ù†ÙŠŰ©â€Ź â€«Ű§Ù„Ű­Ű§ŰłŰšŰ§ŰȘ‬ â€«ÙƒÙ„ÙŠŰ©â€Ź
  • 13. Input and Output â€«Ű§ï»Ÿï»€ï»Œï» ï»źï»ŁïșŽŰȘ‬ ‫ïș—ï»˜ï»šïŻżïș”‏ â€«Ùˆâ€Ź ‫ŰȘ‬ â€«Ű§ï»Ÿïș€ïșŽïșłïș’ïșŽâ€Ź â€«ï»›ï» ïŻżïș”‏ Most PL/SQL input and output (I/O) is done with SQL statements that store data in database tables or query those tables.
  • 14. Data Abstraction â€«Ű§ï»Ÿï»€ï»Œï» ï»źï»ŁïșŽŰȘ‬ ‫ïș—ï»˜ï»šïŻżïș”‏ â€«Ùˆâ€Ź ‫ŰȘ‬ â€«Ű§ï»Ÿïș€ïșŽïșłïș’ïșŽâ€Ź â€«ï»›ï» ïŻżïș”‏ Data abstraction lets you work with the essential properties of data without being too involved with details. You can design a data structure first, and then design algorithms that manipulate it. 1 Cursors 2 Composite Variables 3 %ROWTYPE Attribute 4 %TYPE Attribute Abstract Data Types
  • 15. Cursors â€«Ű§ï»Ÿï»€ï»Œï» ï»źï»ŁïșŽŰȘ‬ ‫ïș—ï»˜ï»šïŻżïș”‏ â€«Ùˆâ€Ź ‫ŰȘ‬ â€«Ű§ï»Ÿïș€ïșŽïșłïș’ïșŽâ€Ź â€«ï»›ï» ïŻżïș”‏ A cursor is a pointer to a private SQL area that stores information about processing a specific SQL statement or PL/SQL SELECT INTO statement. You can use the cursor to retrieve the rows of the result set one at a time. You can use cursor attributes to get information about the state of the cursor—for example, how many rows the statement has affected so far. For more information about cursors,
  • 16. Composite Variables â€«Ű§ï»Ÿï»€ï»Œï» ï»źï»ŁïșŽŰȘ‬ ‫ïș—ï»˜ï»šïŻżïș”‏ â€«Ùˆâ€Ź ‫ŰȘ‬ â€«Ű§ï»Ÿïș€ïșŽïșłïș’ïșŽâ€Ź â€«ï»›ï» ïŻżïș”‏ A composite variable has internal components, which you can access individually. You can pass entire composite variables to subprograms as parameters. PL/SQL has two kinds of composite variables, collections and records. In a collection, the internal components are always of the same data type, and are called elements. You access each element by its unique index. Lists and arrays are classic examples of collections. In a record, the internal components can be of different data types, and are called fields. You access each field by its name. A record variable can hold a table row, or some columns from a table row.
  • 17. %ROWTYPE Attribute The %ROWTYPE attribute lets you declare a record that represents either a full or partial row of a database table or view. For every column of the full or partial row, the record has a field with the same name and data type. If the structure of the row changes, then the structure of the record changes accordingly. â€«Ű§Ù„Ù…ŰčÙ„ÙˆÙ…Ű§ŰȘ‬ ‫وŰȘÙ‚Ù†ÙŠŰ©â€Ź â€«Ű§Ù„Ű­Ű§ŰłŰšŰ§ŰȘ‬ â€«ÙƒÙ„ÙŠŰ©â€Ź
  • 18. %TYPE Attribute The %TYPE attribute lets you declare a data item of the same data type as a previously declared variable or column (without knowing what that type is). If the declaration of the referenced item changes, then the declaration of the referencing item changes accordingly. The %TYPE attribute is particularly useful when declaring variables to hold database values. â€«Ű§Ù„Ù…ŰčÙ„ÙˆÙ…Ű§ŰȘ‬ ‫وŰȘÙ‚Ù†ÙŠŰ©â€Ź â€«Ű§Ù„Ű­Ű§ŰłŰšŰ§ŰȘ‬ â€«ÙƒÙ„ÙŠŰ©â€Ź
  • 19. Abstract Data Types An Abstract Data Type (ADT) consists of a data structure and subprograms that manipulate the data. The variables that form the data structure are called attributes. The subprograms that manipulate the attributes are called methods. ADTs are stored in the database. Instances of ADTs can be stored in tables and used as PL/SQL variables. ADTs let you reduce complexity by separating a large system into logical components, which you can reuse. In the static data dictionary view *_OBJECTS, the OBJECT_TYPE of an ADT is TYPE. In the static data dictionary view *_TYPES, the TYPECODE of an ADT is OBJECT. â€«Ű§Ù„Ù…ŰčÙ„ÙˆÙ…Ű§ŰȘ‬ ‫وŰȘÙ‚Ù†ÙŠŰ©â€Ź â€«Ű§Ù„Ű­Ű§ŰłŰšŰ§ŰȘ‬ â€«ÙƒÙ„ÙŠŰ©â€Ź
  • 20. Control Statements â€«Ű§ï»Ÿï»€ï»Œï» ï»źï»ŁïșŽŰȘ‬ ‫ïș—ï»˜ï»šïŻżïș”‏ â€«Ùˆâ€Ź ‫ŰȘ‬ â€«Ű§ï»Ÿïș€ïșŽïșłïș’ïșŽâ€Ź â€«ï»›ï» ïŻżïș”‏ PL/SQL has three categories of control statements: Conditional selection statements, which let you run different statements for different data values. Loop statements, which let you repeat the same statements with a series of different data values. Sequential control statements, which allow you to go to a specified, labelled statement, or to do nothing.
  • 21. A PL/SQL unit is one of these: ‱PL/SQL anonymous block ‱FUNCTION ‱LIBRARY ‱PACKAGE ‱PACKAGE BODY ‱PROCEDURE ‱TRIGGER ‱TYPE â€«Ű§ï»Ÿï»€ï»Œï» ï»źï»ŁïșŽŰȘ‬ ‫ïș—ï»˜ï»šïŻżïș”‏ â€«Ùˆâ€Ź ‫ŰȘ‬ â€«Ű§ï»Ÿïș€ïșŽïșłïș’ïșŽâ€Ź â€«ï»›ï» ïŻżïș”‏ PL/SQL Units and Compilation Parameters
  • 22. Blocks in PL SQL Declarations This section starts with the keyword DECLARE. It is an optional section and defines all variables, cursors, subprograms, and other elements to be used in the program. Executable Commands This section is enclosed between the keywords BEGIN and END and it is a mandatory section. It consists of the executable PL/SQL statements of the program. It should have at least one executable line of code, which may be just a NULL command to indicate that nothing should be executed. Exception Handling This section starts with the keyword EXCEPTION. This optional section contains exception(s) that handle errors in the program. â€«Ű§Ù„Ù…ŰčÙ„ÙˆÙ…Ű§ŰȘ‬ ‫وŰȘÙ‚Ù†ÙŠŰ©â€Ź â€«Ű§Ù„Ű­Ű§ŰłŰšŰ§ŰȘ‬ â€«ÙƒÙ„ÙŠŰ©â€Ź
  • 23. Block Syntax DECLARE <declarations section> BEGIN <executable command(s)> EXCEPTION <exception handling> END; â€«Ű§Ù„Ù…ŰčÙ„ÙˆÙ…Ű§ŰȘ‬ ‫وŰȘÙ‚Ù†ÙŠŰ©â€Ź â€«Ű§Ù„Ű­Ű§ŰłŰšŰ§ŰȘ‬ â€«ÙƒÙ„ÙŠŰ©â€Ź
  • 24. DECLARE message varchar2(20):= 'Hello, World!'; BEGIN dbms_output.put_line(message); END; / Before this We have to set the serveroutput to on â€«Ű§Ù„Ù…ŰčÙ„ÙˆÙ…Ű§ŰȘ‬ ‫وŰȘÙ‚Ù†ÙŠŰ©â€Ź â€«Ű§Ù„Ű­Ű§ŰłŰšŰ§ŰȘ‬ â€«ÙƒÙ„ÙŠŰ©â€Ź
  • 25. BEGIN FOR someone IN ( SELECT * FROM employees WHERE employee_id < 120 ORDER BY employee_id ) LOOP DBMS_OUTPUT.PUT_LINE('First name = ' || someone.first_name || ', Last name = ' || someone.last_name); END LOOP; END; / â€«Ű§Ù„Ù…ŰčÙ„ÙˆÙ…Ű§ŰȘ‬ ‫وŰȘÙ‚Ù†ÙŠŰ©â€Ź â€«Ű§Ù„Ű­Ű§ŰłŰšŰ§ŰȘ‬ â€«ÙƒÙ„ÙŠŰ©â€Ź
  • 26. The end; line signals the end of the PL/SQL block. To run the code from the SQL command line, you may need to type / at the beginning of the first blank line after the last line of the code. When the above code is executed at the SQL prompt, it produces the following result. Hello World PL/SQL procedure successfully completed. â€«Ű§Ù„Ù…ŰčÙ„ÙˆÙ…Ű§ŰȘ‬ ‫وŰȘÙ‚Ù†ÙŠŰ©â€Ź â€«Ű§Ù„Ű­Ű§ŰłŰšŰ§ŰȘ‬ â€«ÙƒÙ„ÙŠŰ©â€Ź
  • 27. PL/SQL Identifiers The PL/SQL Identifiers PL/SQL identifiers are constants, variables, exceptions, procedures, cursors, and reserved words. The identifiers consist of a letter optionally followed by more letters, numerals, dollar signs, underscores, and number signs and should not exceed 30 characters. By default, identifiers are not case-sensitive. So you can use integer or INTEGER to represent a numeric value. You cannot use a reserved keyword as an identifier. â€«Ű§Ù„Ù…ŰčÙ„ÙˆÙ…Ű§ŰȘ‬ ‫وŰȘÙ‚Ù†ÙŠŰ©â€Ź â€«Ű§Ù„Ű­Ű§ŰłŰšŰ§ŰȘ‬ â€«ÙƒÙ„ÙŠŰ©â€Ź
  • 28. The PL/SQL Delimiters â€«Ű§Ù„Ù…ŰčÙ„ÙˆÙ…Ű§ŰȘ‬ ‫وŰȘÙ‚Ù†ÙŠŰ©â€Ź â€«Ű§Ù„Ű­Ű§ŰłŰšŰ§ŰȘ‬ â€«ÙƒÙ„ÙŠŰ©â€Ź
  • 29. Delimiter Description +, -, *, / Addition, subtraction/negation, multiplication, division % Attribute indicator ' Characterstring delimiter . Component selector (,) Expression or list delimiter : Host variable indicator , Item separator " Quoted identifier delimiter = Relational operator @ Remote access indicator ; Statementterminator := Assignment operator => Association operator || Concatenation operator ** Exponentiation operator <<, >> Label delimiter (begin and end) /*, */ Multi-line comment delimiter (begin and end) -- Single-line comment indicator .. Range operator <, >, <=, >= Relational operators <>, '=, ~=, ^= Different versions of NOT EQUAL
  • 30. Program comments are explanatory statements that can be included in the PL/SQL code that you write and helps anyone reading its source code. All programming languages allow some form of comments. The PL/SQL supports single-line and multi-line comments. All characters available inside any comment are ignored by the PL/SQL compiler. The PL/SQL single-line comments start with the delimiter -- (double hyphen) and multi-line comments are enclosed by /* and */. DECLARE -- variable declaration message varchar2(20):= 'Hello, World!'; BEGIN /* * PL/SQL executable statement(s) */ dbms_output.put_line(message); END; / The PL/SQL Comments â€«Ű§Ù„Ù…ŰčÙ„ÙˆÙ…Ű§ŰȘ‬ ‫وŰȘÙ‚Ù†ÙŠŰ©â€Ź â€«Ű§Ù„Ű­Ű§ŰłŰšŰ§ŰȘ‬ â€«ÙƒÙ„ÙŠŰ©â€Ź