DATABASE MANAGEMENT SYSTEM
ORACLE SQL AND PL/SQL
PART II : Application Using Oracle PL/SQL
Teaching Aid Material
(www.phindia.com/gupta)

Authors invite your valuable feedback and suggestions on the book
pkdasgupta@gmail.com
RadhaKrishna_P@infosys.com
DATABASE MANAGEMENT SYSTEM
ORACLE SQL AND PL/SQL
PART II : Application Using Oracle PL/SQL

CHAPTER 5
Introduction to PL/SQL Programming
CHAPTER 5
Introduction to PL/SQL Programming
1 of 9

Why PL/SQL?


Better Performance



Higher Productivity



Full Portability



Tight Integration with SQL



Security

Page reference in the book: 147-152
CHAPTER 5
Introduction to PL/SQL Programming
2 of 9

Character Set

Type

Characters

Uppercase, lowercase

A-Z , a-z

Digits

0-9

Mathematical and
punctuation symbols

~ ! @ # % & * ( )_ - + = | [ ] { }: ; ’’ ’ < > ?
/

White space

Space, tab, carriage return

Page reference in the book: 147-152
CHAPTER 5
Introduction to PL/SQL Programming
3 of 9

PL/SQL Identifiers






Identifiers are used to name PL/SQL objects such as constants,
variables, exceptions, procedures, cursors and reserved words.
Identifiers can be up to 30 characters in length, must start with a
letter and can include dollar sign, an underscore, and a pound sign.
Identifiers are case sensitive and cannot include space, tab or
carriage return. An identifier can be enclosed within double quotes.

Page reference in the book: 147-152
CHAPTER 5
Introduction to PL/SQL Programming
4 of 9

PL/SQL Literals


Literals are specific values and are not represented by identifiers



Can be character, number or Boolean value



To embed single quotes within a string literal, two single quotes next
to each other may be placed

Page reference in the book: 147-152
CHAPTER 5
Introduction to PL/SQL Programming
5 of 9

PL/SQL Delimiters


Delimiters are symbols with special meaning to PL/SQL



Used to separate identifiers from each other

Delimiter

Characteristics

--

Single-line comment indicator

/*

*/

Multiline comment delimiter

||

Concatenation operator

..

Range operator

;

Statement terminator

Page reference in the book: 147-152
CHAPTER 5
Introduction to PL/SQL Programming
6 of 9

PL/SQL Variable
Type
Scalar

Purpose
Numeric

NUMBER(p, s)
The maximum precision is 38 digits

Character

VARCHAR2, CHAR, LONG
CHAR and VARCHAR2 are up to 32767 bytes

Date

Range is between 01-Jan-4712BC and
31-Dec-9999AD

Boolean

Can hold TRUE, FLASE or NULL only

RAW

Similar to CHAR variables
LONG RAW is similar to LONG
LONG RAW can be up to 2 gigabytes

Page reference in the book: 147-152
CHAPTER 5
Introduction to PL/SQL Programming
7 of 9

PL/SQL Variable
Type
LOB

Purpose
BFILE

File locaters pointing to read only large objects in
operating system files

BLOB

BLOB locaters that point to large binary objects inside
the database

CLOB

CLOB locaters point to large character objects inside
the database

NCLOB

NCLOB locaters point large national character set
objects inside the database

Page reference in the book: 147-152
CHAPTER 5
Introduction to PL/SQL Programming
8 of 9

NULL




Represents unknown values as NULL values
NULL is never equal to anything
NVL, IS NULL or IS NOT NULL

CONSTANT



Requires an initial value
Value cannot be changed

Page reference in the book: 147-152
CHAPTER 5
Introduction to PL/SQL Programming
9 of 9

Default Value


A variable is assigned a default value of NULL while declaration



Can be initialized by assignment operator (: =)

Page reference in the book: 147-152
CHAPTER 5
PL/SQL Structure
1 of 3

PL/SQL Structure
Declare
Variables, Cursors, Constants
Begin
PL/SQL statements
Exception
Statements for error handling
End;

Page reference in the book: 152-159
CHAPTER 5
PL/SQL Structure
2 of 3

IF Statement


Allows actions based upon conditions



IF statement can also be nested



Three forms of IF statements:
IF … THEN …
IF … THEN … ELSE …
IF … THEN … ELSIF … ELSE ….

Page reference in the book: 152-159
CHAPTER 5
PL/SQL Structure
3 of 3

Loops in PL/SQL


Simple Loop

Loop
PL/SQL statements
Exit Condition
End Loop



For Loop

For variable IN Start..End
PL/SQL Statements
End Loop;



While Loop

While <Condition> Loop
PL/SQL Statements
End Loop

Page reference in the book: 152-159
CHAPTER 5
PL/SQL and Oracle
1 of 10

Scope of a Variable in Nested Block
PL/SQL statements can be nested

SQL Cursor




A cursor is a private SQL work area where all commands
defined in the cursor are executed
There are two types of cursors:


Implicit cursor



Explicit cursor

Page reference in the book: 159-168
CHAPTER 5
PL/SQL and Oracle
2 of 10

Implicit Cursor


It is automatically created and handled by Oracle Server



Support all types of DMLs [Insert/Update/Delete]



Supports SELECT statement that returns only

Explicit Cursor


Needs to be declared explicitly by the programmer



It is handled using cursor-related commands

Page reference in the book: 159-168
CHAPTER 5
PL/SQL and Oracle
3 of 10

INTO Clause





Into clause is mandatory in a PL/SQL program
It is placed between SELECT and FROM clauses
Acts as a container with the associated variable
Query must return only one row

%TYPE Attribute


Used to declare variables that refer to the column

% ROWTYPE Attribute


Represents a row in a table

Page reference in the book: 159-168
CHAPTER 5
PL/SQL and Oracle
4 of 10

Example EX5_1

Page reference in the book: 159-168
CHAPTER 5
PL/SQL and Oracle
5 of 10

EX5_2

Page reference in the book: 159-168
CHAPTER 5
PL/SQL and Oracle
6 of 10

EX5_3

Page reference in the book: 159-168
CHAPTER 5
PL/SQL and Oracle
EX5_5

Page reference in the book: 159-168

7 of 10
CHAPTER 5
PL/SQL and Oracle
8 of 10

EX5_10

Page reference in the book: 159-168
CHAPTER 5
PL/SQL and Oracle
EX5_11

Page reference in the book: 159-168

9 of 10
CHAPTER 5
PL/SQL and Oracle
10 of 10

EX5_12

Page reference in the book: 159-168
CHAPTER 5
Short/ Objective Type Question
Q1. Define identifiers, literals and delimiters in Oracle PL/SQL.
Q2. Explain %TYPE and %ROWTYPE attributes with the help of suitable examples.
Q3. What do you understand by SQL cursors?
Q4. Write a PL/SQL program which will accept three numbers and print the smallest among them.
SQL>@ C:/TTP/Q4
Enter value for a: 12
Enter value for b: 34
Enter value for c: 12
Smallest Number is 12
Q5. Write PL/SQL program using Loop … End Loop, For Loop and While Loop, which will accept integer from
1 to 10 and print factorial. For example, factorial of 5 is 5*4*3*2*1=120.
SQL>@ C:/TTP/Q5
Enter value for i_num: 6
FACTORIAL OF 6 IS 720
CHAPTER 5
Short/Objective Type Question
Q6. Write a PL/SQL program which will accept a number from 1 to 20 and display following the the figure.
SQL>@ C:/TTP/Q8
Enter value for i_num: 10
-- *
---- *
------ *
-------- *
---------- *
------------ *
-------------- *
---------------- *
------------------ *
-------------------- *

Q7.
(a)
(b)
(c)
(d)

The basic programming unit of a PL/SQL code is a
Procedure
Subprogram
Module
Block

Q8.
(a)
(b)
(c)
(d)

Which of the following sections of a PL/SQL program are optional?
Declarative Section
Exception Handler Section
Executable Section
All of these
CHAPTER 5
Home Assignment
WORKOUT
DATABASE MANAGEMENT SYSTEM
ORACLE SQL AND PL/SQL
PART II : Application using Oracle PL/SQL
Periods Proposed – 18
Chapter 5
Introduction to PL/SQL Programming

1 Period [Theory]
1 Period [Practical]

Chapter 6
Oracle Function, Procedure And Package

1 Period [Theory]
1 Period [Practical]

Chapter 7
Oracle Exception Handler and Database Triggers

1 Period [Theory]
1 Period [Practical]

Chapter 8
Implicit and Explicit Cursors

1 Period [Theory]
2 Periods [Practical]

Chapter 9
Advance Cursors

1 Period [Theory]
1 Period [Practical]

Chapter 10
PL/SQL Collection

1 Period [Theory]
1 Period [Practical]

Chapter 11
Oracle Objects and Dynamic SQL

1 Period [Theory]
1 Period [Practical]

Chapter 12
Performance Tuning

2 Period [Theory]
1 Period [Practical]
THE BOOK
The book is organized into three parts to introduce the
theoretical and programming concepts of DBMS. Part I
(Basic Concepts and Oracle SQL) deals with DBMS basic,
software analysis and design, data flow diagram, ER model,
relational algebra, normal forms, SQL queries, functions,
sub‐queries, different types of joins, DCL, DDL, DML, object
constraints and security in Oracle. Part II (Application Using
Oracle PL/SQL) explains PL/SQL basics, functions,
procedures, packages, exception handling, triggers, implicit,
explicit and advanced cursors using suitable examples. This
part also covers advanced concepts related to PL/SQL, such
as collection, records, objects, dynamic SQL and
performance tuning. Part III (Advanced Concepts and
Technologies) elaborates on advanced database concepts
such as query processing, file organization, distributed
architecture, backup, recovery, data warehousing, online
analytical processing and data mining concepts and their
techniques.

KEY FEATURES
 Includes about 300 examples to illustrate the concepts.
 Offers about 400 objective type questions.
 Provides about 100 challenging workouts.

TEACHING AID MATERIAL
Teaching Aid Material for all the chapters is provided on the
website of PHI Learning. Visit www.phindia.com/gupta to
explore the contents.
Visit PHI Learning Center www.phindia.com/gupta and Click on Instructor Resources to
access Teaching Aid Material for all the 18 chapters of the book.

Database management system chapter5

  • 1.
    DATABASE MANAGEMENT SYSTEM ORACLESQL AND PL/SQL PART II : Application Using Oracle PL/SQL Teaching Aid Material (www.phindia.com/gupta) Authors invite your valuable feedback and suggestions on the book pkdasgupta@gmail.com RadhaKrishna_P@infosys.com
  • 2.
    DATABASE MANAGEMENT SYSTEM ORACLESQL AND PL/SQL PART II : Application Using Oracle PL/SQL CHAPTER 5 Introduction to PL/SQL Programming
  • 3.
    CHAPTER 5 Introduction toPL/SQL Programming 1 of 9 Why PL/SQL?  Better Performance  Higher Productivity  Full Portability  Tight Integration with SQL  Security Page reference in the book: 147-152
  • 4.
    CHAPTER 5 Introduction toPL/SQL Programming 2 of 9 Character Set Type Characters Uppercase, lowercase A-Z , a-z Digits 0-9 Mathematical and punctuation symbols ~ ! @ # % & * ( )_ - + = | [ ] { }: ; ’’ ’ < > ? / White space Space, tab, carriage return Page reference in the book: 147-152
  • 5.
    CHAPTER 5 Introduction toPL/SQL Programming 3 of 9 PL/SQL Identifiers    Identifiers are used to name PL/SQL objects such as constants, variables, exceptions, procedures, cursors and reserved words. Identifiers can be up to 30 characters in length, must start with a letter and can include dollar sign, an underscore, and a pound sign. Identifiers are case sensitive and cannot include space, tab or carriage return. An identifier can be enclosed within double quotes. Page reference in the book: 147-152
  • 6.
    CHAPTER 5 Introduction toPL/SQL Programming 4 of 9 PL/SQL Literals  Literals are specific values and are not represented by identifiers  Can be character, number or Boolean value  To embed single quotes within a string literal, two single quotes next to each other may be placed Page reference in the book: 147-152
  • 7.
    CHAPTER 5 Introduction toPL/SQL Programming 5 of 9 PL/SQL Delimiters  Delimiters are symbols with special meaning to PL/SQL  Used to separate identifiers from each other Delimiter Characteristics -- Single-line comment indicator /* */ Multiline comment delimiter || Concatenation operator .. Range operator ; Statement terminator Page reference in the book: 147-152
  • 8.
    CHAPTER 5 Introduction toPL/SQL Programming 6 of 9 PL/SQL Variable Type Scalar Purpose Numeric NUMBER(p, s) The maximum precision is 38 digits Character VARCHAR2, CHAR, LONG CHAR and VARCHAR2 are up to 32767 bytes Date Range is between 01-Jan-4712BC and 31-Dec-9999AD Boolean Can hold TRUE, FLASE or NULL only RAW Similar to CHAR variables LONG RAW is similar to LONG LONG RAW can be up to 2 gigabytes Page reference in the book: 147-152
  • 9.
    CHAPTER 5 Introduction toPL/SQL Programming 7 of 9 PL/SQL Variable Type LOB Purpose BFILE File locaters pointing to read only large objects in operating system files BLOB BLOB locaters that point to large binary objects inside the database CLOB CLOB locaters point to large character objects inside the database NCLOB NCLOB locaters point large national character set objects inside the database Page reference in the book: 147-152
  • 10.
    CHAPTER 5 Introduction toPL/SQL Programming 8 of 9 NULL    Represents unknown values as NULL values NULL is never equal to anything NVL, IS NULL or IS NOT NULL CONSTANT   Requires an initial value Value cannot be changed Page reference in the book: 147-152
  • 11.
    CHAPTER 5 Introduction toPL/SQL Programming 9 of 9 Default Value  A variable is assigned a default value of NULL while declaration  Can be initialized by assignment operator (: =) Page reference in the book: 147-152
  • 12.
    CHAPTER 5 PL/SQL Structure 1of 3 PL/SQL Structure Declare Variables, Cursors, Constants Begin PL/SQL statements Exception Statements for error handling End; Page reference in the book: 152-159
  • 13.
    CHAPTER 5 PL/SQL Structure 2of 3 IF Statement  Allows actions based upon conditions  IF statement can also be nested  Three forms of IF statements: IF … THEN … IF … THEN … ELSE … IF … THEN … ELSIF … ELSE …. Page reference in the book: 152-159
  • 14.
    CHAPTER 5 PL/SQL Structure 3of 3 Loops in PL/SQL  Simple Loop Loop PL/SQL statements Exit Condition End Loop  For Loop For variable IN Start..End PL/SQL Statements End Loop;  While Loop While <Condition> Loop PL/SQL Statements End Loop Page reference in the book: 152-159
  • 15.
    CHAPTER 5 PL/SQL andOracle 1 of 10 Scope of a Variable in Nested Block PL/SQL statements can be nested SQL Cursor   A cursor is a private SQL work area where all commands defined in the cursor are executed There are two types of cursors:  Implicit cursor  Explicit cursor Page reference in the book: 159-168
  • 16.
    CHAPTER 5 PL/SQL andOracle 2 of 10 Implicit Cursor  It is automatically created and handled by Oracle Server  Support all types of DMLs [Insert/Update/Delete]  Supports SELECT statement that returns only Explicit Cursor  Needs to be declared explicitly by the programmer  It is handled using cursor-related commands Page reference in the book: 159-168
  • 17.
    CHAPTER 5 PL/SQL andOracle 3 of 10 INTO Clause     Into clause is mandatory in a PL/SQL program It is placed between SELECT and FROM clauses Acts as a container with the associated variable Query must return only one row %TYPE Attribute  Used to declare variables that refer to the column % ROWTYPE Attribute  Represents a row in a table Page reference in the book: 159-168
  • 18.
    CHAPTER 5 PL/SQL andOracle 4 of 10 Example EX5_1 Page reference in the book: 159-168
  • 19.
    CHAPTER 5 PL/SQL andOracle 5 of 10 EX5_2 Page reference in the book: 159-168
  • 20.
    CHAPTER 5 PL/SQL andOracle 6 of 10 EX5_3 Page reference in the book: 159-168
  • 21.
    CHAPTER 5 PL/SQL andOracle EX5_5 Page reference in the book: 159-168 7 of 10
  • 22.
    CHAPTER 5 PL/SQL andOracle 8 of 10 EX5_10 Page reference in the book: 159-168
  • 23.
    CHAPTER 5 PL/SQL andOracle EX5_11 Page reference in the book: 159-168 9 of 10
  • 24.
    CHAPTER 5 PL/SQL andOracle 10 of 10 EX5_12 Page reference in the book: 159-168
  • 25.
    CHAPTER 5 Short/ ObjectiveType Question Q1. Define identifiers, literals and delimiters in Oracle PL/SQL. Q2. Explain %TYPE and %ROWTYPE attributes with the help of suitable examples. Q3. What do you understand by SQL cursors? Q4. Write a PL/SQL program which will accept three numbers and print the smallest among them. SQL>@ C:/TTP/Q4 Enter value for a: 12 Enter value for b: 34 Enter value for c: 12 Smallest Number is 12 Q5. Write PL/SQL program using Loop … End Loop, For Loop and While Loop, which will accept integer from 1 to 10 and print factorial. For example, factorial of 5 is 5*4*3*2*1=120. SQL>@ C:/TTP/Q5 Enter value for i_num: 6 FACTORIAL OF 6 IS 720
  • 26.
    CHAPTER 5 Short/Objective TypeQuestion Q6. Write a PL/SQL program which will accept a number from 1 to 20 and display following the the figure. SQL>@ C:/TTP/Q8 Enter value for i_num: 10 -- * ---- * ------ * -------- * ---------- * ------------ * -------------- * ---------------- * ------------------ * -------------------- * Q7. (a) (b) (c) (d) The basic programming unit of a PL/SQL code is a Procedure Subprogram Module Block Q8. (a) (b) (c) (d) Which of the following sections of a PL/SQL program are optional? Declarative Section Exception Handler Section Executable Section All of these
  • 27.
  • 28.
    DATABASE MANAGEMENT SYSTEM ORACLESQL AND PL/SQL PART II : Application using Oracle PL/SQL Periods Proposed – 18 Chapter 5 Introduction to PL/SQL Programming 1 Period [Theory] 1 Period [Practical] Chapter 6 Oracle Function, Procedure And Package 1 Period [Theory] 1 Period [Practical] Chapter 7 Oracle Exception Handler and Database Triggers 1 Period [Theory] 1 Period [Practical] Chapter 8 Implicit and Explicit Cursors 1 Period [Theory] 2 Periods [Practical] Chapter 9 Advance Cursors 1 Period [Theory] 1 Period [Practical] Chapter 10 PL/SQL Collection 1 Period [Theory] 1 Period [Practical] Chapter 11 Oracle Objects and Dynamic SQL 1 Period [Theory] 1 Period [Practical] Chapter 12 Performance Tuning 2 Period [Theory] 1 Period [Practical]
  • 29.
    THE BOOK The book isorganized into three parts to introduce the theoretical and programming concepts of DBMS. Part I (Basic Concepts and Oracle SQL) deals with DBMS basic, software analysis and design, data flow diagram, ER model, relational algebra, normal forms, SQL queries, functions, sub‐queries, different types of joins, DCL, DDL, DML, object constraints and security in Oracle. Part II (Application Using Oracle PL/SQL) explains PL/SQL basics, functions, procedures, packages, exception handling, triggers, implicit, explicit and advanced cursors using suitable examples. This part also covers advanced concepts related to PL/SQL, such as collection, records, objects, dynamic SQL and performance tuning. Part III (Advanced Concepts and Technologies) elaborates on advanced database concepts such as query processing, file organization, distributed architecture, backup, recovery, data warehousing, online analytical processing and data mining concepts and their techniques. KEY FEATURES  Includes about 300 examples to illustrate the concepts.  Offers about 400 objective type questions.  Provides about 100 challenging workouts. TEACHING AID MATERIAL Teaching Aid Material for all the chapters is provided on the website of PHI Learning. Visit www.phindia.com/gupta to explore the contents.
  • 30.
    Visit PHI LearningCenter www.phindia.com/gupta and Click on Instructor Resources to access Teaching Aid Material for all the 18 chapters of the book.