Advanced SQL
Advanced SQL
Syllabus
F. Y. B. Sc. (Information Technology)
Paper - III, SEM - III
ADVANCED SQL
Unit - I
Unit - II
Advanced SQL :
Controlling user Access, using SET operators, Data Time
Functions, Enhancements to Group by clause (cube,
Rollup and Grouping), Advanced Subqueries (Multiple
column subqueries, Subqueries in FROM clause, Scalar
and correlated subqueries), WITH Clause, Hierarchical
retrieval.
Unit - III
PLSQL :
Introduction, Overview and benefits of PL/SQL,
Subprograms, types of PL/SQL blocks, Simple
Anonymous Block, Identifiers, types of identifiers,
Declarative Section, variables, Scalar Data Types, The %
Type attribute, bind variables, sequences in PL/SQL
expressions, Executable statements, PL/SQL block
syntax, comment the code, deployment of SQL functions
in PL/SQL, Convert Data Types, newted blocks,
operators. Interaction with the oracle server, Invoke
SELECT Statements in PL/SQL, SQL cursor concept,
Data Manipulation in the Server using PL/SQL, SQL
Cursor Attributes to obtain Feedback on DML, Save and
discard transactions.
Unit- IV
Control Structures :
Conditional processing using IF statements and CASE
statements, Loop Statement, while loop statement, for
loop statement, the continue statement composite data
types : PL/SQL records, The % ROWTYPE attribute,
insert and update with PL/SQL records, INDEX by tables,
INDEX BY Table Methods, Use INDEX BY Table of
Records, Explicit Cursors, Declare the Cursor, Open the
2
Cursor, Fetch data from the Cursor, Close the Cursor,
Cursor FOR loop, The % NOTFOUND and %
ROWCOUNT Attributes, the FOR UPDATE Clause and
WHERE CURRENT Clause, Exception Handling, Handle
Exceptions with PL/SQL, Trap Predefined and nonpredefined Oracle Server Errors, User - Defined
Exceptions,
Propagate
Exceptions,
RAISE_APPLICATION_ERROR Procedure.
Unit-V
Stored Procedures :
Create a Modularized and Layered Subprogram Design,
the PL/SQL Execution Environment, differences between
Anonymous Blocks and Subprograms, Create, Call, and
Remove Stored Procedures, Implement Procedures
Parameters and Parameters Modes, View Procedure
Information,
Stored
Functions
and
Debugging
Subprograms, Create, Call, and Remove a Stored
Function, advantages of using Stored Functions, the
steps to create a stored function, Invoke User-Defined
Functions in SQL Statements, Restrictions when calling
Functions, Control side effects when calling Functions,
View Functions Information, debug Functions and
Procedures, Packages, advantages of Packages,
components of a Package, Develop a Package, enable
visibility of a Packages Components, Create the Package
Specification and Body using the SQL CREATE
Statement and SQL Developer, Invoke the Package
Constructs, View the PL/SQL Source Code using the
Data Dictionary, Deploying Packages, Overloading
Subprograms in PL/SQL, Use the STANDARD Package,
Use Forward Declarations, Implement Package Functions
in SQL and Restrictions, Persistent State of Packages,
Persistent State of a Package Cursor, Control side effects
of PL/SQL Subprograms, Invoke PL/SQL Tables of
Records in Packages
Unit-VI
Dynamic SQL :
The Execution Flow of SQL, Declare Cursor Variables,
Dynamically Executing a PL/SQL Block, Configure Native
Dynamic SQL to Compile PL/SQL Code, invoke
DBMS_SQL Package, Implement DBMS_SQL with a
Parameterized DML Statement, Dynamic SQL Functional
Completeness, Triggers, the Triggers, Create DML
Triggers using the CREATE TRIGGER Statement and
SQL Developer, Identify the Trigger Event Types, Body,
and Firing (Timing), Statement Level Triggers and Row
Level Triggers, Create Instead of and Disabled Triggers,
3
Manage, Test and Remove Triggers. Creating
Compound, DDL and Event Database Triggers,
Compound Trigger Structure for Tables and Views,
Compound Trigger to Resolve the Mutating Table Error,
Comparison of Database Triggers and Stored
Procedures, Create Triggers on DDL Statements, Create
Database-Event and System-Events Triggers, System
Privileges Required to Manage Triggers
Books:
Murachs Oracle SQL and PLSQL by Joel Murach, Murach and
Associates.
Oracle Database 11g PL/SQL Programming Workbook, ISBN :
9780070702264, By : Michael McLaughlin, John Harper, Tata McGrawHill.
Reference :
Oracle PL/SQL Programming, Fifth Edition By Steven Feuerstein, Bill
Pribyl
Oracle 11g : SQL Reference Oracle press
Oracle 11g : PL/SQL Reference Oracle Press.
Expert Oracle PL/SQL, By : Ron Hardman, Michael McLaughlin, Tata
McGraw-Hill
Oracle database 11g : hands on SQL/PL SQL by Satish Asnani (PHI)
EEE edition
Unit - I
1
BASIC STRUCTURED QUERY
LANGUAGE I
Unit Structure
1.0 Objectives
1.1 Introduction
1.2 What is RDBMS?
1.2.1 Concepts of Relational Database
1.3 Introduction to SQL
1.4 What can SQL do?
1.5 SQL Language Elements
1.6 Classification of SQL commands
1.6.1 Data Query Language
1.6.2 Data Definition Language
1.6.3 Data Manipulation Language
1.6.4 Data Control Language
1.6.5 Transaction Control Language
1.7 Creating and Managing Tables
1.7.1 How to create a table?
1.8 Applying Constraints
1.8.1 Classification of Constraints
1.8.2 Primary Key
1.8.3 NOT NULL
1.8.4 UNIQUE
1.8.5 DEFAULT
1.8.6 CHECK
1.8.7 FOREIGN KEY
1.8.8 Adding, Removing and Altering Constraints
1.8.9 Enabling and Disabling Constraints
1.9 Summary
1.10 Review Questions
1.11 Lab Assignment
1.12 Bibliography, References and Further Reading
1.13 Online References
MissionMCA.com
1.0 OBJECTIVES
After going through this chapter, you will be able to
Understand RDBMS
1.1 INTRODUCTION
The amount of information available to us is literally
exploding, and the value of data as an organizational asset is
widely recognized. To get the most out of their large and complex
datasets, users require tools that simplify the tasks of managing the
data and extracting useful information in a timely fashion.
Otherwise, data can become a liability, with the cost of acquiring it
and managing it far exceeding the value derived from it.
A database management system, or DBMS, is software
designed to assist in maintaining and utilizing large collections of
data. The collection of data, usually referred to as database,
contains information relevant to an enterprise. The primary goal of
a DBMS is to provide a way to store and retrieve database
information that is both convenient and efficient.
A data model is a collection of high-level data description
constructs that hide many low-level storage details. A DBMS allows
a user to define the data to be stored in terms of a data model.
Most database management systems today are based on the
relational data model. A relational database consists of a
collection of tables (mathematical concept of relation). A row in a
table represents a relationship among a set of values. Informally, a
table is an entity set, and a row is an entity.
The relational model is very simple and elegant: a database
is a collection of one or more relations, where each relation is a
table with rows and columns. This simple tabular representation
enables even novice users to understand the contents of a
MissionMCA.com
6
database, and it permits the use of simple, high-level languages to
query the data.
MissionMCA.com
7
The SQL language has several parts:
Data-Definition Language (DDL): The SQL DDL provides
commands for defining relation schemas, deleting relations, and
modifying relation schemas.
Interactive Data-Manipulation Language (DML): The SQL DML
includes a query language based on both the relational algebra and
the tuple relational calculus. It also includes commands to insert
into tuples, delete tuples from, and modify tuples in the database.
View Definition: The SQL DDL includes commands for defining
views.
Transaction Control: SQL includes commands for specifying the
beginning and ending of transactions.
Embedded SQL and Dynamic SQL: Embedded and Dynamic
SQL define how SQL statements can be embedded within generalpurpose programming languages, such as C, C++, Java, PL/I,
Cobol, Pascal, and Fortran.
Integrity: The SQL DDL includes commands for specifying integrity
constraints that the data stored in the database must satisfy.
Updates that violate integrity constraints are disallowed.
Authorization: The SQL DDL includes commands for specifying
access rights to relations and views.
MissionMCA.com
8
Expressions, which can produce either scalar values or tables
consisting of columns and rows of data.
Predicates, which specify conditions that can be evaluated to
SQL three-valued logic (3VL) or Boolean (true/false/unknown)
truth values and which are used to limit the effects of statements
and queries, or to change program flow.
Queries, which retrieve the data based on specific criteria. This
is the most important element of SQL.
Statements, which may have a persistent effect on schemata
and data, or which may control transactions, program flow,
connections, sessions, or diagnostics.
SQL statements also include the semicolon (";") statement
terminator.
Insignificant whitespace is generally ignored in SQL
statements and queries, making it easier to format SQL code for
readability.
The following table shows the most common data types
Data Type
Description
NUMBER
CHAR(W)
VARCHAR2(W)
DATE
RAW(size)
TIMESTAMP
MissionMCA.com
9
SQL
DQL
DDL
DML
TCL
DCL
MissionMCA.com
10
undone by using the transaction control statements, like COMMIT,
ROLLBACK, SAVEPOINT.
11
CREATE TABLE student
(
rollno char(4),
name varchar2(10),
date_of_birth date,
marks number(3)
);
Rules for creating tables are as follows:
The name of the table can be of 30 characters at the
maximum.
Alphabets from A to Z and 0 to 9 are allowed.
Special characters like under score (_) are allowed.
SQL reserved keywords like SELECT, WHERE etc are not
allowed.
MissionMCA.com
12
Column Level
Table Level
Column Level Constraints
When a constraint is applied to a single column then it is
called a Column Level Constraint. This constraint will affect the
values being entered for that particular column only irrespective of
values of other columns.
Table Level Constraints
When a single constraint is applied to more than one column
then it is called Table Level Constraint. These constraints impact
the values being entered for a combination of column values, for
example, salary can never be less than the commission is a table
level constraint.
1.8.2 Primary Key
Primary key can be defined as the single column or
combination of columns which uniquely identify a row in a table, for
example, the rollno column in the student table is the primary key.
No two students can have the same rollno. The following are the
features of a primary key:
A table can have one and only one primary key.
Primary key implies UNIQUE as well as NOT NULL values,
UNIQUE implies duplicate values cannot be entered.
NOT NULL implies value cannot be unknown (NULL) for any
of the records.
Primary key columns are automatically indexed.
A Primary Key can contain more than 1 column known as a
Composite Key.
Primary Key = UNIQUE + NOT NULL + Automatic Indexed
An example of Primary key constraint:
CREATE TABLE student
(
MissionMCA.com
13
rollno CHAR(4)
PRIMARY KEY,
name VARCHAR2(10),
date_of_birth DATE,
marks NUMBER(3)
);
OR
CREATE TABLE student
(
rollno char(4),
name varchar2(10),
date_of_birth date,
marks number(3),
CONSTRAINT stud_pk PRIMARY KEY (rollno)
);
MissionMCA.com
14
values for that column can be entered. It is a type of Domain
Integrity Constraint.
An example of NOT NULL constraint:
CREATE TABLE student
(
rollno char(4),
name varchar2(10)
NOT NULL,
UNIQUE,
NOT NULL,
1.8.5 DEFAULT
MissionMCA.com
15
DEFAULT implies that if we do not specify a value for a
column in the INSERT statement, then the value specified in the
Default clause will get inserted.
An example of DEFAULT constraint:
CREATE TABLE student
(
rollno char(4)
UNIQUE,
name varchar2(10)
NOT NULL,
UNIQUE,
name varchar2(10)
NOT NULL,
date_of_birth date
NOT NULL,
MissionMCA.com
16
A table can have multiple foreign keys referring to different
tables for different columns.
Defining foreign key establishes a PARENT/CHILD relationship
between the two tables.
Defining foreign key implies two conditions:
We can insert only those records in the CHILD table for
which corresponding records exist in the PARENT table.
We can delete only those records from the PARENT
table for which there are no corresponding records in the
CHILD table.
An example of FOREIGN KEY constraint:
CREATE TABLE stud_detail
(
rollno char(4)
PRIMARY KEY,
name varchar2(10)
address date
NOT NULL,
NOT NULL,
17
(
rollno char(4)
PRIMARY KEY,
name varchar2(10)
address date
NOT NULL,
NOT NULL,
MissionMCA.com
18
the ALTER
constraints:
TABLE
command.
An
example
for
removing
Disabling Constraints
ALTER TABLE student DISABLE CONSTRAINT stud_pk;
Enabling Constraints
ALTER TABLE student ENABLE CONSTRAINT stud_pk;
1.9 SUMMARY
MissionMCA.com
19
A database management system, or DBMS, is software
designed to assist in maintaining and utilizing large
collections of data.
The collection of data, usually referred to as database,
contains information relevant to an enterprise.
A relational database consists of a collection of tables.
Relational Database is a structure consisting of a set of
objects related together and data integrity methods which
aims in the efficient storage of electronic data.
SQL stands for Structured Query Language and is a query
language used for accessing and modifying information in
the database.
SQL can execute queries against a database to store,
retrieve, modify and delete data.
SQL commands can be categorized into Data Query
Language (DQL), Data Definition Language (DDL), Data
Manipulation Language (DML), Data Control Language
(DCL) and Transaction Control Language (TCL).
A table is the fundamental building block of a database
application. Tables are used to store data on various entities
like employees, products, customers, orders, sales etc.
The CREATE TABLE statement is used to create a table
object.
The various constraints that can be applied on a table are as
follows:
PRIMARY KEY
NOT NULL
UNIQUE
DEFAULT
CHECK
FOREIGN KEY
The structure of a table can be changed after its creation
using the ALTER TABLE statement.
Constraints can also be applied to table after its creation
using the ALTER TABLE statement.
Constraints can be enabled or disabled
ENABLE/DISABLE CONSTRAINT statement.
using
the
MissionMCA.com
20
(1) What is SQL? What can be done using SQL?
(2) List and Explain the common data types in SQL.
(3) List and Explain the different elements in SQL language.
(4) Explain the parts of SQL language?
(5) Explain the classification of SQL statements?
(6) List the rules for creating a table? How can you create a
table?
(7) List and Explain the different types of constraints with
examples?
(8) Explain the commands used to alter table?
Data type
Size
Constraint
DEPTNO
NUMBER
PRIMARY KEY
DNAME
VARCHAR2
10
LOCATION
VARCHAR2
10
Data type
Size
Constraint
EMPNO
CHAR
PRIMARY KEY
ENAME
VARCHAR2
10
NOT NULL
JOB
VARCHAR2
10
MGR
CHAR
HIREDATE
TIMESTAMP
GENDER
CHAR
SAL
NUMBER
8,2
DEFAULT 0
COMM
NUMBER
8,2
DEFAULT 0
DEPTNO
NUMBER
NOT NULL
MissionMCA.com
21
4. Add table level constraint such that commission cannot be
greater than 30% of salary after the table has been created.
Assign the constraint name COMM_30_SAL.
5. Add new constraint with the name DEPT_CHK_LOCATION
to DEPT table such that LOCATION can be any one of the
following cities MUMBAI, PUNE, BENGALURU, LONDON,
SAN FRANSISCO only.
6. Remove the UNIQUE constraint from the LOCATION
column.
Ron
Third
Edition
by
Hardman,Michael
MissionMCA.com
22
2
BASIC STRUCTURED QUERY
LANGUAGE II
Unit Structure
2.0 Objectives
2.1 Introduction
2.2 Basic Data Retreival
2.2.1 Column Aliases
2.2.2 Duplicate Rows
2.3 Restricting and Sorting Data
2.3.1 Ordering Data
2.4 Dual Table
2.5 Single Row Functions
2.5.1 Numeric Functions
2.5.2 Character Functions
2.5.3 DateTime Functions
2.5.4 Conversion Functions
2.6 Joins
2.6.1 Inner Equi Join
2.6.2 Inner Non-Equi Join
2.6.3 Self Join
2.6.4 Outer Joins
2.6.5 Left Outer Join
2.6.6 Right Outer Join
2.6.7 Full Outer Join
2.6.8 Cartesian Product
2.7 Summary
2.8 Review Questions
2.9 Lab Assignment
2.10 Bibliograpy, References and Further Reading
2.11 Online References
MissionMCA.com
23
2.0 OBJECTIVES
At the end of this chapter, you will be able to
2.1 INTRODUCTION
In the previous chapter, we created tables using CREATE
TABLE statement and managed tables. In this chapter, we will
manipulate data from a single table and also retreive data and join
multiple tables.
MissionMCA.com
24
The WHERE clause includes a comparison predicate, which
restricts the rows returned by the query. The WHERE clause
eliminates all rows from the result set for which the
comparison predicate does not evaluate to true.
The GROUP BY clause is used to project rows having
common values into a smaller set of rows. GROUP BY is
often used in conjunction with SQL aggregation functions or
to eliminate duplicate rows from a result set. The WHERE
clause is applied before the GROUP BY clause.
The HAVING clause includes a predicate used to filter rows
resulting from the GROUP BY clause. Because it acts on the
results of the GROUP BY clause, aggregation functions can
be used in the HAVING clause predicate.
The ORDER BY clause identifies which columns are used to
sort the resulting data, and in which direction they should be
sorted (options are ascending or descending). Without an
ORDER BY clause, the order of rows returned by an SQL
query is undefined.
To illustrate the SQL commands in this chapter we consider the
records in the following tables
Student Table:
Rollno
Name
Course_no
Grade
201
Rohit
SC01
209
Raj
SC01
325
Rita
COM02
355
Parag
SC02
365
Mohini
SC03
Stud_details Table:
Rollno
Name
Age
Address
201
Rohit
25
MG road, Goregaon
209
Raj
27
310
Manisha
24
Vasai(W)
325
Rita
26
Borivali(W)
355
Parag
23
Thane(E)
365
Mohini
22
MissionMCA.com
25
Course Table:
Course_no
Course_name
Major
SC01
B.Sc IT
Information Technology
SC02
B.Sc CS
Computer Science
SC03
B.Sc Maths
Mathematics
COM01
B.Com
Accounts
COM02
B.M.S
Management Studies
MissionMCA.com
26
Syntax:
SELECT DISTINCT <column name> FROM <table name>;
For Example:
SELECT DISTINCT Course_no, Name Student Name
FROM Student;
MissionMCA.com
27
Range Test (BETWEEN)
Tests whether the value of an expression falls within a
specified range of values.
Syntax: SELECT <list> FROM <table name> WHERE <column
name> BETWEEN <lower-limit> AND <higher-limit>
Set Membership Test (IN)
Tests whether a data value matches one of a list of target
values.
Syntax: SELECT <list> FROM <table name> WHERE <column
name> IN (list of constants separated by comma);
NULL Value Test (IS NULL)
Used to check explicitly for NULL values in a search
condition.
Syntax: SELECT <list> FROM <table name> WHERE <column
name> IS NULL;
Pattern Matching Test (LIKE)
Checks to see whether the data value in a column matches
a specified pattern. The pattern is a string that may include one or
more wildcard characters.
Symbol
Represents
[]
[^]
MissionMCA.com
28
in a specified sequence may be alphabetically or by value. The
ORDER BY clause needs to be added to the end of the SELECT
statement.
Syntax: SELECT <list> FROM <table name> ORDER BY <column
name> [ASC | DESC]
MissionMCA.com
29
2.5.1 Numeric Functions:
Numeric functions accept numeric values as input and return
numeric values as output.
ABS: The ABS function calculates the absolute value of an
expression. Since, the absolute value of a real number is its
numeric value without regard to its sign (for example, 3 is the
absolute value of both 3 and -3), this function always returns a
positive value.
Syntax: ABS(expression)
Usage: SELECT ABS (expression/column name) FROM <table
name>;
CEIL: The CEIL function returns the smallest whole number greater
than or equal to a specified number.
Syntax: CEIL(n)
Usage: SHOW CEIL(15.7);
RESULT: 16
FLOOR: The FLOOR function returns the largest whole number
equal to or less than a specified number.
Syntax: FLOOR(n)
Usage: SHOW FLOOR(15.7);
RESULT: 15
ROUND: When a number is specified as an argument, the ROUND
function returns the number rounded to the nearest multiple of a
second number you specify or to the number of decimal places
indicated by the second number.
Syntax: ROUND(number_exp, roundvalue)
Usage: SHOW ROUND(2/3, .1);
RESULT: 0.70
TRUNC: When you specify a number as an argument, the
TRUNCATE function truncates a number to a specified number of
decimal places.
Syntax: TRUNC (number, truncvalue)
Usage: SHOW TRUNC (15.79, 1);
RESULT: 15.7
MissionMCA.com
30
2.5.2 Character Functions:
Character functions accept character values as input and
can return character or numeric values as output.
LOWER: The LOWER function converts all alphabetic characters in
a text expression into lowercase.
Syntax: LOWER(text-expression)
MissionMCA.com
31
Syntax: SELECT CURRENT_TIMESTAMP FROM dual;
Note: DateTime Functions have been covered in more detail in
later chapters.
2.5.4 Conversion Functions:
Conversion function converts a value from one datatype to
another datatype.
TO_CHAR: This function converts number or date data type to
character data type.
Syntax: TO_CHAR (value, [format mask])
TO_DATE: This function converts string data type to date data
type.
Syntax: TO_DATE (string, [format mask])
TO_NUMBER: This function converts string data type to number
data type.
Syntax: TO_NUMBER (string, [format mask])
Note: For more information about different functions and the
various categories of functions please refer to the Oracle
Documentation available on the Oracle web site.
2.6 JOINS
Till now we have been selecting records from a single table
at a time. However, in real life we do not work only on a single table
but we need to select columns from two or more tables at a time for
a single query. In order to combine two or more tables for a single
query we have to use a concept called JOIN to acheive the
desired result.
MissionMCA.com
32
JOIN
OUTER
INNER
EQUI
CARTESIAN PRODUCT
NON-EQUI
RIGHT
FULL
LEFT
A
A Inner
Join B
B
student.name,
course.course_name,
MissionMCA.com
33
WHERE student.course_no = course.course_no;
Rollno
Name
Course_name
Major
201
Rohit
B.Sc IT
Information Technology
209
Raj
B.Sc IT
Information Technology
325
Rita
B.M.S
Management Studies
355
Parag
B.Sc CS
Computer Science
365
Mohini
B.Sc Maths
Mathematics
Notice the WHERE clause specifying the join of two tables based
on the matching course_no column is known as the JOIN condition.
The Join condition may involve more than one column.
Above query can also be written as
SELECT student.rollno,
course.major
student.name,
course.course_name,
MissionMCA.com
34
SELECT s.rollno, s.name, c.course_name, c.major
FROM student s, course c
WHERE s.course_no = c.course_no AND s.age > 25;
Rollno
Name
Course_name
Major
209
Raj
B.Sc IT
Information Technology
325
Rita
B.M.S
Management Studies
MissionMCA.com
35
called as LEFT OUTER JOIN. The result set contains all rows from
the first or the left table and only matching records from the second
table.
Table
A
Table
B
Syntax:
SELECT <list>
FROM <table name1> LEFT OUTER JOIN <table name2>
ON JOIN CONDITION
Example for Left Outer Join
Suppose we want to display the list of names and addresses of all
students alongwith their course_no and grade.
SELECT sd.rollno, sd.name, sd.address, s.course_no, s.grade
FROM stud_details st LEFT OUTER JOIN student s
ON sd.rollno = s.rollno;
Rollno
Name
Address
Course_no
Grade
201
Rohit
MG road, Goregaon
SC01
209
Raj
SC01
310
Manisha
Vasai(W)
325
Rita
Borivali(W)
COM02
355
Parag
Thane(E)
SC02
365
Mohini
SC03
In the above result, all columns from the stud_details table have
been listed and only matching columns from the student table have
been listed.
MissionMCA.com
36
2.6.6 RIGHT OUTER JOIN
A query that performs a join on two tables A and B, and
returns all rows from table B and only matching rows from table A is
called as RIGHT OUTER JOIN. The result set contains all rows
from the second or the right table and only matching records from
the first table.
Table
Table
Syntax:
SELECT <list>
FROM <table name1> RIGHT OUTER JOIN <table name2>
ON JOIN CONDITION
Course_no
Course_name
Major
201
Rohit
SC01
B.Sc IT
Information Technology
209
Raj
SC01
B.Sc IT
Information Technology
325
Rita
COM02
B.M.S
Management Studies
355
Parag
SC02
B.Sc CS
Computer Science
365
Mohini
SC03
B.Sc Maths
Mathematics
COM01
B.Com
Accounts
MissionMCA.com
37
In the above result, all columns from the course table have been
listed and only matching columns from the student table have been
listed.
2.6.7 FULL OUTER JOIN
A query that performs a join on two tables A and B, and
returns all rows from table A and B alonwith the non-matching rows
is called as FULL OUTER JOIN. The result set contains all rows
from both the tables.
Table
Table
Syntax:
SELECT <list>
FROM <table name1> FULL OUTER JOIN <table name2>
ON JOIN CONDITION
2.6.8 CARTESIAN PRODUCT
A join in which each record from table 1 gets associated with
each and every record of table 2 is called as Cartesian Product. A
Cartesian product always generates many rows and is rarely
useful. For example, the Cartesian product of two tables, each with
100 rows, has 10,000 rows.
Syntax:
SELECT <list>
FROM <table name1>, <table name2>
MissionMCA.com
38
2.7 SUMMARY
The SELECT statement retreives data from the database and
returns in the form of query results.
Duplicate rows can be avoided by simply using the keyword
DISTINCT.
We need to use the WHERE clause in order to list a record on
the basis of some conditions.
WHERE clause can be used for
Equality conditions
Comparison Test
Range Test
Set Membership Test
NULL Value Test
Pattern Matching Test
To define a specific order in the output, we need to use the
ORDER BY clause with the SELECT statement.
Single row functions operate on a single row and returns a
single result row for every row of a queried table.There are
various types of single row functions
Numeric Functions
Character Functions
DateTime Functions
Conversion Functions
MissionMCA.com
39
Data type
Size
Constraint
CUSTOMER_ID
CHAR
PRIMARY KEY.
BEGIN WITH 'C'
CUSTOMER_NAME
VARCHAR2
20
NOT NULL
ADDRESS
VARCHAR2
20
UNIQUE
CITY
VARCHAR2
20
PINCODE
NUMBER
STATE
VARCHAR2
20
BALANCE_DUE
NUMBER
8,2
MUST
Data type
Size
Constraint
PRODUCT_CODE
CHAR
PRIMARY KEY
PRODUCT_NAME
VARCHAR2
20
UNIQUE
QTY_AVAIL
NUMBER
COST_PRICE
NUMBER
8,2
SELLING_PRICE
NUMBER
8,2
MissionMCA.com
40
3. Create a ORDER table with the following columns and constraints
Column name
Data type
Size
ORDER_NO
CHAR
ORDER_DATE
TIMESTAMP
CUSTOMER_ID
CHAR
PRODUCT_CODE
CHAR
QUANTITY
NUMBER
PRIMARY
KEY
=
ORDER_NO
CUSTOMER_ID + PRODUCT_CODE
Constraint
ORDER_DATE
MissionMCA.com
41
Wikipedia Link
http://en.wikipedia.org/wiki/SQL
MissionMCA.com
42
3
ADVANCED QUERIES AND DATABASE
OBJECTS
Unit Structure
3.0 Objectives
3.1 Introduction
3.2 Aggregate Functions
3.3 Group by Having Clause
3.3.1 Comparing Having clause and where clause
3.4 Creating Other Database Objects
3.4.1 Views
3.4.1.1 Classification of Views
3.4.1.2 Updateable Views
3.4.1.3 Non-updateable Views
3.4.2 Indexes
3.4.3 Sequences
3.4.4 Synonyms
3.5 Sub queries
3.5.1 Sub query in DDL and DML commands
3.6 Summary
3.7 Review Questions
3.8 Lab Assignment
3.9 Bibliography, References and Further Reading
3.10 Online References
3.0 OBJECTIVES
At the end of this chapter you will be able to:
MissionMCA.com
43
3.1 INTRODUCTION
In the previous chapter, you were introduced to scalar
(single row) functions, which operate on a single value and return a
single value. In this chapter, you'll learn how to code queries that
summarize data.
Description
MissionMCA.com
44
COUNT
([ALL
expression)
COUNT(*)
EMPNO
ENAME
HIREDATE
DEPTNO GENDER
SALARY COMM
111
Satish
19-DEC-2008
10
10,000
1000
222
Rashmi
01-JAN-1987
20
8000
550
333
Rishi
05-JUN-1976
10
7000
450
444
Anil
16-APR-1967
10
12,000
2000
555
Anita
30
8000
1000
666
Nilesh
20-MAY-1987
20
13,000
777
Ruchi
11-JUN-2000
30
5000
888
Sarika
20
4000
MissionMCA.com
45
Find the number of employees in the emp table.
SELECT COUNT(*) FROM emp;
Result:
COUNT(*)
8
Find the total salary and the average commission from the emp
table.
SELECT SUM(salary), AVG(comm) FROM emp;
Result:
SUM(salary) AVG(comm)
67000
1000
COUNT(*)
10
30
20
MissionMCA.com
46
GROUP BY deptno;
DEPTNO
SUM(salary)
10
29,000
30
13,000
20
25,000
SUM(salary)
10
29,000
20
25,000
EMPCOUNT
2
MissionMCA.com
47
3.3.1 Comparing HAVING clause and WHERE clause
A WHERE clause in a SELECT statement that uses grouping
& aggregates, the search condition is applied before rows are
grouped and aggregates are calculated. That way, only rows that
satisfy the condition are grouped. A HAVING clause in a SELECT
statement that uses grouping & aggregates, the search condition is
applied after rows are grouped and aggregates are calculated. That
way, only groups that satisfy the condition are included in the result
set.
A WHERE clause can refer to any column in the table. A
HAVING clause can only refer to a column included in the SELECT
clause.
A WHERE clause cannot contain aggregate functions.
Aggregate functions can only be coded in the HAVING clause.
product_name
company_name
unit_price
100
Shampoo
Pantene
180
101
Deospray
Denim
400
102
Tooth paste
Colgate
150
103
Soap
Lux
75
104
Hair gel
Laureal
300
MissionMCA.com
48
Assume table ORDER with the following records
order_id
product_id
total_units
Customer
O1
101
30
Lifestyle
O2
101
Shoppers stop
O3
103
25
Spencer
O4
101
10
Food bazaar
O5
103
200
Big bazaar
3.4.1 VIEWS
A view is a logical representation of one or more tables. In
essence, a view is a stored query. A view derives its data from the
tables on which it is based,called base tables. Base tables can be
tables or other views. All operations performed on a view actually
affect the base tables. You can use views in most places where
tables are used. You can query, insert, update and delete from
views. Views can be handled as any other table but they do not
occupy any space.
MissionMCA.com
49
Hide data complexity.
For example, a single view can be defined with a join, which
is a collection of related columns or rows in multiple tables.
However, the view hides the fact that this information
actually originates from several tables. A query might also
perform extensive calculations with table information. Thus,
users can query a view without knowing how to perform a
join or calculations.
Present the data in a different perspective from that of the
base table.
For example, the columns of a view can be renamed without
affecting the tables on which the view is based.
Isolate applications from changes in definitions of base
tables.
For example, if the defining query of a view references three
columns of a four column table, and a fifth column is added
to the table, then the definition of the view is not affected,
and all applications using the view are not affected.
3.4.1.1 Classification of Views
Views can be classified as updateable views and non-updateable
views.
3.4.1.2 Updateable Views
By updateable we mean to say that one can insert, update
and delete records from the view. Actually all the DML operations
are performed on the base table.
View with the following characteristics is called an
updateable view.
It is created from a single table.
It includes all PRIMARY KEYS and NOT NULL columns of
the base table.
Aggregate functions like SUM, AVG have not been used.
It should not have DISTINCT, GROUP BY, HAVING
clauses.
It must not use constants, strings or value expressions like
salary * 2.
It must not any function calls (e.g. RPAD, SUBSTR, etc.).
If a view is defined from another view then that view must
also be updateable.
3.4.1.3 Non-updateable Views
MissionMCA.com
50
Non-updateable means we cannot insert, update and delete
records from that view.
View with the following characteristics is called a nonupdateable view.
7. It is created from more than one table.
8. It has DISTINCT, GROUP BY, HAVING clause. Even if
view is derived from a single table but contains any of
these clauses then it is not updateable.
9. It does not include all the PRIMARY KEYS and NOT NULL
columns of base tables.
CREATING VIEWS
Syntax:
CREATE OR REPLACE VIEW <view name> AS
(SELECT query)
[WITH READ ONLY CONSTRAINT <constraint name>];
Create a view showing the details of the products which have been
ordered by the customers.
CREATE OR REPLACE VIEW prod_ordered AS
SELECT product_name, company_name, total_units
FROM product INNER JOIN order
ON product.product_id = order.product_id;
To see the details of the products ordered
SELECT * FROM prod_ordered
product_name
company_name total_units
Deospray
Denim
30
Deospray
Denim
Soap
Lux
25
Deospray
Denim
10
Soap
Lux
200
MissionMCA.com
51
VIEW WITH READ ONLY CONSTRAINT
The WITH READ ONLY option allows the user to create a
read-only view. You cannot use the DELETE, INSERT or UPDATE
commands to modify data for the view.
Syntax:
DROP VIEW <view name>
3.4.2 INDEXES
Index is an object which can be defined as the ordered list of
values of a column or combination of columns used for faster
searching and sorting of data. An index speeds up joins and
searches by providing a way for a database management system to
go directly to a row rather than having to search through all the
rows until it finds the one you want. By default, indexes are created
for primary keys and unique constraints of a table. However,
creating indexes must be avoided on columns that are updated
frequently since this slows down insert, update and delete
operations.
Creating Indexes
To create an index, you need to use the CREATE INDEX
command. In addition, you can use the UNIQUE keyword to specify
that an index contains only unique values.
Syntax:
MissionMCA.com
52
CREATE [UNIQUE] INDEX index_name
ON table_name (column_name1 [ASC | DESC] [, column_name2
[ASC | DESC]]......)
Removing Indexes
To remove or delete an index, we need to use the DROP INDEX
command.
Syntax:
DROP INDEX <index name>
3.4.3 SEQUENCES
SEQUENCE is a type of database object which can be used to
generate numbers in a sequence. This can be used to generate
values for primary keys.
Create Sequence
Sequences can be created using the CREATE SEQUENCE
command which has the following syntax:
CREATE SEQUENCE <sequence name>
START WITH <integer-value>
INCREMENT BY <integer-value>
MAXVALUE <integer-value> OR NOMAXVALUE
MINVALUE <integer-value> OR NOMINVALUE
CYCLE OR NOCYCLE
CACHE OR NOCACHE
ORDER OR NOORDER
START WITH <integer-value>: specifies the 1st sequence number
to be generated.
INCREMENT BY <integer-value>: The integer number by which
sequence number should be incremented for generating the next
number. If it is positive then values are ascending and if it is
negative then values are descending. The default value is 1.
MissionMCA.com
53
MAXVALUE <integer-value>: If the increment value is positive then
MAXVALUE determines the maximum value up to which the
sequence numbers will be generated.
NOMAXVALUE: Specifies the maximum value of 10^27 for an
ascending sequence or -1 for a descending sequence.
MINVALUE <integer-value>: If the increment value is negative then
MINVALUE determines the minimum value up to which the
sequence numbers will be generated.
NOMINVALUE: Specifies the minimum value of 1 for an ascending
sequence or -10^26 for a descending sequence.
CYCLE: Causes the sequences to automatically recycle to
minvalue when maxvalue is reached for ascending sequences; for
descending sequences, it causes to recycle from minvalue back to
maxvalue.
NOCYCLE: Sequence numbers will not be generated after
reaching the maximum value for ascending sequences or minimum
value for descending sequences.
CACHE: Specifies how many values are pre-allocated in buffers for
faster access. Default value is 20.
NOCACHE: Sequence numbers are not pre-allocated.
ORDER: Generates the number in a serial order.
NOORDER: Generates the number in a random order.
Initializing and Accessing Sequence
A sequence needs to be initialized before being used. Every
sequence is initialized by a pseudo column NEXTVAL. Once you've
created a sequence, you typically use it within an INSERT
statement. The NEXTVAL pseudo column gets the next value from
the sequence so it can be inserted into the table. The CURRVAL
pseudo column is used to check the current value of the sequence.
Modifying Sequence
It may be required later on to change certain parameters of an
already created sequence. This can be done using the ALTER
SEQUENCE command.
Syntax:
ALTER SEQUENCE <sequence name>
MissionMCA.com
54
[sequence attributes]
Dropping Sequence
A sequence can be deleted using the DROP SEQUENCE
command.
Syntax:
DROP SEQUENCE <sequence name>
3.4.4 SYNONYMS
Synonyms are alternative names for an existing object which are
permanently stored in the database. We have used alias names for
accessing tables with shorter names in various queries. The
difference is that these alias names are of temporary nature and
are lost from one query to another, whereas synonyms are
permanent alias names for objects.
Advantages of using synonyms
Synonyms are often used for security and convenience.
They can do the following things:
They can hide or mask the name and owner of an object.
Provide location transparency for remote objects of a
distributed database.
Simplify SQL statements for database users.
With the help of synonyms the user can insert, update or
retreive records from any database object. Synonyms cannot
be used in a DROP TABLE, DROP VIEW or TRUNCATE
TABLE statement.
The various objects for which synonyms can be created are as
follows:
Tables
Views
Materialized Views
Stored Function
Stored Procedures
Packages
Sequences
MissionMCA.com
55
Synonyms
There are two kinds of synonyms public and private.
Public Synonym: These are accessible to all users provided they
have the appropriate object privilege on the object on which the
synonym is created.
Private Synonym: These belong only to the user who creates it.
Creating Synonyms
Synonyms can be created using the CREATE SYNONYM
command
Syntax:
CREATE [PUBLIC] SYNONYM <synonym name> FOR <object
name>
Renaming Synonyms
Only Private Synonyms can be renamed using the Rename
statement.
Syntax:
RENAME <old synonym name> TO <new synonym name>
Modifying a Synonym
To modify or alter or change a synonym use the OR REPLACE
clause. You can use this clause to change the definition of an
existing synonym without dropping it.
Syntax:
CREATE OR REPLACE [PUBLIC] SYNONYM <synonym name>
FOR <object name>
Removing a Synonym
Synonyms can be removed or deleted using the DROP SYNONYM
statement.
Syntax:
DROP SYNONYM <synonym name>
3.5 SUBQUERIES
Queries can be nested so that the results of one query can
be used in another query via a relational operator or aggregation
function. A nested query is known as a subquery. While joins and
MissionMCA.com
56
other table operations provide computationally superior (i.e. faster)
alternatives in many cases, the use of subqueries introduces a
hierarchy in execution which can be useful or necessary.
Since you know how to code SELECT statements, you
already know how to code a subquery. It's simply a SELECT
statement that's coded within another SQL statement. A subquery
can return a single value, a result set that contains a single column
(single row subquery), or a result set that contains one or more
columns (multiple row subquery).
Description
Equal
>
Greater than
>=
<
Less than
<=
<>
Not equal
!=
Not equal
Description
IN, =ANY
>ANY
MissionMCA.com
57
<ANY
>ALL
<ALL
!=ALL, NOT IN
Get the names of the product which has the highest price.
SELECT product_name FROM product
WHERE unit_price >= ALL (SELECT unit_price FROM product);
MissionMCA.com
58
Or
SELECT product_name FROM product
WHERE product_id
order_prod);
ANY
(SELECT
product_id
FROM
Get the names of the products which have not been ordered
by any customers.
SELECT product_name FROM product
WHERE product_id
order_prod);
NOT
IN
(SELECT
product_id
FROM
product_id
FROM
Or
SELECT product_name FROM product
WHERE product_id
order_prod);
!=
ALL
(SELECT
MissionMCA.com
59
3.6 SUMMARY
Aggregate functions operate on a series of values and return a
single summary value.
Aggregate functions are commonly used with the GROUP BY
clause in a SELECT statement, where the rows of a queried
table are divided into groups.
The most common aggregate functions are
AVG
SUM
MIN
MAX
COUNT
GROUP BY clause forms groups on the specified columns. The
GROUP BY clause is used alongwith the aggregate functions to
retreive data grouped according to one or more columns.
The HAVING clause to restrict the groups of returned rows to
those groups for which the specified condition is TRUE.
A view is a logical representation of one or more tables. In
essence, a view is a stored query.
Views can be classified as updateable views and nonupdateable views.
Index is an object which can be defined as the ordered list of
values of a column or combination of columns used for faster
searching and sorting of data.
By default, indexes are created for primary keys and unique
constraints of a table.
Creating indexes must be avoided on columns that are updated
frequently since this slows down insert, update and delete
operations.
SEQUENCE is a type of database object which can be used to
MissionMCA.com
60
Data type
Size
Constraint
CUSTOMER_ID
CHAR
PRIMARY KEY.
BEGIN WITH 'C'
CUSTOMER_NAME
VARCHAR2
20
NOT NULL
MUST
MissionMCA.com
61
ADDRESS
VARCHAR2
20
UNIQUE
CITY
VARCHAR2
20
PINCODE
NUMBER
STATE
VARCHAR2
20
BALANCE_DUE
NUMBER
8,2
Data type
Size
Constraint
PRODUCT_CODE
CHAR
PRIMARY KEY
PRODUCT_NAME
VARCHAR2
QTY_AVAIL
NUMBER
COST_PRICE
NUMBER
8,2
SELLING_PRICE
NUMBER
8,2
UNIQUE
Data type
Size
ORDER_NO
CHAR
ORDER_DATE
TIMESTAMP
CUSTOMER_ID
CHAR
PRODUCT_CODE
CHAR
QUANTITY
NUMBER
Constraint
MissionMCA.com
62
PRIMARY
KEY
=
ORDER_NO
CUSTOMER_ID + PRODUCT_CODE
ORDER_DATE
Ron
Third
Edition
by
Hardman,Michael
MissionMCA.com
63
MissionMCA.com
64
Unit - II
4
SECURITY PRIVILEGES, SET
OPERATORS & DATETIME FUNCTIONS
Unit Structure
4.0 Objectives
4.1 Introduction
4.2 Enhancements to GROUP BY function
4.2.1 ROLLUP Operator
4.2.2 CUBE Operator
4.2.3 GROUPING Function
4.3 SET OPERATORS
4.3.1 INTERSECT Operator
4.3.2 UNION Operator
4.3.3 UNION ALL Operator
4.3.4 MINUS Operator
4.4 DATETIME FUNCTIONS
4.4.1 Parsing Date and Time
4.5 Controlling User Access
4.5.1 System privileges
4.5.2 Object Privileges
4.5.3 What a user can grant?
4.5.4 GRANT/REVOKE PRIVILEGES
4.5.4.1 GRANT COMMAND
4.5.4.2 REVOKE COMMAND
4.6 Summary
4.7 Review Questions
4.8 Lab Assignment
4.9 Bibliography, References and Further Reading
4.10 Online References
MissionMCA.com
65
4.0 OBJECTIVES
At the end of this chapter you will be able to:
Get summary information using CUBE, ROLLUP and
GROUPING
Combine queries using SET operators
Use DateTime Functions
Control User Access
4.1 INTRODUCTION
In the previous unit, we saw basic SQL operators,
statements, queries and subqueries. In this unit, we shall see
advanced subqueries, various advanced operators and security
privileges.
In the previous chapter we discussed SQL keywords and
functions. In this chapter we will see the extensions of GROUP BY
clause: ROLLUP and CUBE operators, and GROUPING function.
Also we will combine queries using SET operators and see various
DateTime functions.
ENAME HIREDATE
DEPTNO JOB
SALARY
COMM
111
Satish
19-DEC-2008
10
CLERK
7000
1000
222
Rashmi
01-JAN-1987
20
ANALYST
8000
550
333
Rishi
05-JUN-1976
10
MANAGER
10,000
450
444
Anil
16-APR-1967
10
PRESIDENT
15,000
2000
555
Anita
30
MANAGER
8000
1000
666
Nilesh
20-MAY-1987
20
MANAGER
13,000
777
Ruchi
11-JUN-2000
30
SALESMAN
5000
888
Sarika
20
SALESMAN
4000
MissionMCA.com
66
A simple GROUP BY clause will show the following result.
JOB
SUM(SAL)
10
CLERK
7000
10
MANAGER
10000
10
PRESIDENT
15000
20
ANALYST
8000
20
MANAGER
13000
20
SALESMAN
4000
30
MANAGER
8000
30
SALESMAN
5000
MissionMCA.com
67
DEPTNO
JOB
SUM(SAL)
10
CLERK
7000
10
MANAGER
10000
10
PRESIDENT
15000
10
(null)
32000
20
ANALYST
8000
20
MANAGER
13000
20
SALESMAN
4000
20
(null)
25000
30
MANAGER
8000
30
SALESMAN
5000
30
(null)
13000
(null)
(null)
70000
JOB
SUM(SAL)
10
CLERK
7000
10
MANAGER
10000
10
PRESIDENT
15000
10
(null)
32000
20
ANALYST
8000
20
MANAGER
13000
MissionMCA.com
68
20
SALESMAN
4000
20
(null)
25000
30
MANAGER
8000
30
SALESMAN
5000
30
(null)
13000
(null)
ANALYST
8000
(null)
CLERK
7000
(null)
MANAGER
31000
(null)
PRESIDENT
15000
(null)
SALESMAN
9000
(null)
(null)
70000
MissionMCA.com
69
END AS job,
SUM(sal)
FROM emp
GROUP BY ROLLUP (deptno, job)
ORDER BY deptno, job;
DEPTNO
JOB
SUM(SAL)
10
CLERK
7000
10
MANAGER
10000
10
PRESIDENT
15000
10
==========
32000
20
ANALYST
8000
20
MANAGER
13000
20
SALESMAN
4000
20
==========
25000
30
MANAGER
8000
30
SALESMAN
5000
30
==========
13000
==========
==========
70000
UNION
UNION ALL
INTERSECT
MINUS
MissionMCA.com
70
Conditions for SET operations
Two SELECT statements can be combined into a compund query
by a SET operation if they satisfy the following conditions:
The result set of both the queries must have the same
number of columns.
The data type of each column in the first result set must
match with the data types of the columns of the second
result set.
Restrictions on SET operations
The column names for the resultant data set will come from
the first query.
If you want to use the ORDER BY clause in the query
involving SET operations, you must place the ORDER BY
clause only once at the end of the compound query. The
component queries can't have individual ORDER BY
clauses.
Syntax:
<query 1>
[SET OPERATOR]
<query 2>
Assume two tables student and student_details as shown below.
CLASS
ROLLNO
NAME
201
Satish
201
Rashmi
205
Rishi
205
anil
ROLLNO
SUBJECT
MARKS
Maths
12
Physics
23
Maths
34
Maths
35
MissionMCA.com
71
4.3.1 INTERSECT Operator
Returns only those rows which are common to both queries.
Display the rollnos of students that are in both the student
and student_details table.
SELECT rollno FROM student
INTERSECT
SELECT rollno FROM student_details;
ROLLNO
1
2
3
Display the details of students from the student table for which
marks have been entered in the student_details table.
SELECT * FROM student
WHERE rollno IN
(
SELECT rollno FROM student
INTERSECT
SELECT rollno FROM student_details
);
CLASS
ROLLNO
NAME
201
Satish
201
Rashmi
205
Rishi
MissionMCA.com
72
4.3.2 UNION Operator
Returns the values which exist in either of the two queries. By
default, a UNION eliminates duplicate rows.
10. Display all the rollnos which exist either in the student or in
the student_details table.
SELECT rollno FROM student
UNION
SELECT rollno FROM student_details;
RO
LLNO
1
2
3
4
MissionMCA.com
73
Using ORDER BY clause
In order to arrange the final result of any SET operator, we can use
the ORDER BY clause at the end of the last SELECT statement.
Display all the rollnos which exist either in the student or in
the student_details table.
SELECT rollno FROM student
UNION ALL
SELECT rollno FROM student_details
ORDER BY rollno;
ROLLNO
1
1
1
2
2
3
3
4
MissionMCA.com
74
ROLLNO
4
Description
SYSDATE
CURRENT_DATE
MissionMCA.com
75
ROUND (date [, date_format])
MONTHS_BETWEEN
date2)
ADD_MONTHS
integer_months)
LAST_DAY (date)
Result
SYSDATE
19-OCT-09 04:23:36 PM
ROUND (SYSDATE)
20-OCT-09 12:00:00 AM
19-OCT-09 04:24:00 PM
19-OCT-09 04:23:00 PM
19-SEP-09
MissionMCA.com
76
LAST_DAY ('15-FEB-09')
28-FEB-09
21-AUG-08
SYSDATE 1
18-OCT-09
For Example, let us find out the number of years for various
employees who served the company. (refer to the emp table)
SELECT empno, ename,
hiredate)/365) no_of_years
hiredate,
ROUND((sysdate
FROM emp;
EMPNO
ENAME
HIREDATE
NO_OF_YEARS
111
Satish
19-DEC-2008
01
222
Rashmi
01-JAN-1987
22
333
Rishi
05-JUN-1976
33
444
Anil
16-APR-1967
42
666
Nilesh
20-MAY-1987
22
777
Ruchi
11-JUN-2000
Note: The employees whose hire date is NULL are not listed. Since
the ROUND function is used without any specified format, rounding
is done for the next year (i.e. for the first row the time between
hiredate (19/12/08) and sysdate (19/10/2009) is 10 months).
4.4.1 Parsing Date and Time
TO_CHAR function can be used to return various parts of a DATE
value as a string. To do that you need to specify the appropriate
date format element for the part of the DATE value that you want to
return.
MissionMCA.com
77
Example
Result
19-OCT-09 04:23:36 PM
2009
09
OCTOBER
OCT
10
MONDAY
MON
19
16
04
23
MissionMCA.com
78
Applying varying limitations on user access or actions. The
limitations placed on (or removed from) users can apply to
objects such as schemas, tables, or rows or to resources such
as time (CPU, connect, or idle times).
A user privilege is the right to run a particular type of SQL
statement, or the right to access an object that belongs to another
user, run a PL/SQL package, and so on.
Roles are created by users (usually administrators) to group
together privileges or other roles. They are a way to facilitate the
granting of multiple privileges or roles to users.
There are two types of privileges: system privileges and object
privileges.
4.5.1 System privileges:
A system privilege is the right to perform a particular action or to
perform an action on any schema objects of a particular type. For
example, the privileges to create tablespaces and to delete the
rows of any table in a database are system privileges.
Some of the most common system privileges are:
CREATE / ALTER / DROP USER
CREATE SESSION
CREATE / ALTER / DROP TABLE
CREATE VIEWS
CREATE PROCEDURE
CREATE SEQUENCE
CREATE PUBLIC SYNONYM
4.5.2 Object Privileges:
An object privilege is a right that you grant to a user on a
database objects like tables, views, sequences, packages,
procedures. Some examples of object privileges include the right
to:
Use an edition
Update a table
Select rows from another user's table
Execute a stored procedure of another user
79
Object
Privileges
Table
Sequence
SELECT, ALTER
EXECUTE
Index
EXECUTE
MissionMCA.com
80
The WITH ADMIN OPTION clause allows the user or role to grant
the specified system privileges to other users or roles.
For example,
A statement that grants a system privilege to a role
GRANT CREATE SESSION TO ap_user;
A statement that grants a system privilege to a role with the
admin option
GRANT CREATE SESSION TO ap_developer WITH ADMIN
OPTION;
Granting Object Privileges
Each type of object has different privileges associated with it.
Object privileges can be granted to users and roles. If you grant
object privileges to roles, then you can make the privileges
selectively available.
The syntax of GRANT statement for object privileges
GRANT <object_privilege>
ON <[schema_name.]object_name [(column [,.....])]>
TO <user_or_role>
[WITH GRANT OPTION]
The WITH GRANT OPTION clause allows the user or role to grant
the specified object privileges to other users or roles.
Consider the following examples,
A statement that grants an object privilege to a role
GRANT SELECT ON emp TO ap_user;
A statement that grants all object privileges to a role with the
grant option
GRANT SELECT, INSERT, UPDATE, DELETE ON emp TO
ap_developer WITH GRANT OPTION;
MissionMCA.com
81
The syntax of REVOKE statement for system privileges
REVOKE <system_privilege>
FROM <user_or_role>
Consider the following examples,
A statement that revokes a system privilege from a role
REVOKE DROP ANY VIEW FROM ap_user;
Revoking Object Privileges
Each type of object has different privileges associated with it.
Object privileges can be revoked from users and roles.
MissionMCA.com
82
Similarly, you can revoke all individually granted privileges by
specifying ALL.
Consider the following examples,
A statement that grants all object privileges to a role.
GRANT ALL ON emp TO ap_developer;
4.6 SUMMARY
The ROLLUP operator can be used to add one or more
summary rows to a result set that uses grouping and
aggregates.
The CUBE operator is similar to the ROLLUP operator, except it
adds summary rows for every combination of groups specified
in the GROUP BY clause.
The GROUPING function determines when a null value is
assigned to a column as a result of the ROLLUP or CUBE
operator.
Set operators combine the results of two component queries
into a single result. Queries containing set operators are called
compound queries.
The summary of various SET operators is as follows:
Operator
Description
UNION ALL
UNION
MINUS
MissionMCA.com
83
Datetime functions operate on date, timestamp, and interval
values.
TO_CHAR function can be used to return various parts of a
DATE value as a string.
A user privilege is the right to run a particular type of SQL
statement, or the right to access an object that belongs to
another user, run a PL/SQL package, and so on.
There are two types of privileges: system privileges and object
privileges.
A system privilege is the right to perform a particular action or to
perform an action on any schema objects of a particular type.
An object privilege is a right that you grant to a user on a
database objects like tables, views, sequences, packages,
procedures.
The GRANT command is used to grant system and object
privileges to a role or a user.
The REVOKE command is used to revoke system or object
privileges from a role or user.
Data type
Size
Constraint
CUSTOMER_ID
CHAR
PRIMARY
MUST
WITH 'C'
CUSTOMER_NAME
VARCHAR2
20
NOT NULL
ADDRESS
VARCHAR2
20
UNIQUE
CITY
VARCHAR2
20
KEY.
BEGIN
MissionMCA.com
84
PINCODE
NUMBER
STATE
VARCHAR2
20
BALANCE_DUE
NUMBER
8,2
Data type
Size
Constraint
PRODUCT_CODE
CHAR
PRIMARY KEY
PRODUCT_NAME
VARCHAR2
20
UNIQUE
QTY_AVAIL
NUMBER
COST_PRICE
NUMBER
8,2
SELLING_PRICE
NUMBER
8,2
Data type
Size
ORDER_NO
CHAR
ORDER_DATE
TIMESTAMP
CUSTOMER_ID
CHAR
PRODUCT_CODE
CHAR
QUANTITY
NUMBER
PRIMARY
KEY
=
ORDER_NO
CUSTOMER_ID + PRODUCT_CODE
Constraint
ORDER_DATE
MissionMCA.com
85
15. Permit the user Ram to be able to INSERT and DELETE
commands in the CUSTOMER table.
16. Grant INSERT permission to the user Ravi such that he can
further grant the INSERT permission to other users for the
CUSTOMER table.
17. Withdraw the DELETE permission from Ram.
18. Give the UPDATE permission on ADDRESS and STATE
column of the CUSTOMER table to Kishan.
Database
Management
Systems,
RamaKrishnan, Gehre. McGraw Hill
Third
Edition
by
MissionMCA.com
86
5
ADVANCED SUBQUERIES
Unit Structure
5.0 Objectives
5.1 Introduction
5.2 Multiple Column Subqueries
5.2.1 Coding Subqueries in the FROM clause
5.3 Scalar Subqueries
5.4 Correlated Subquery
5.5 WITH clause
5.5.1 Functions of the WITH clause
5.6 Hierarchical Queries
5.7 Summary
5.8 Review Questions
5.9 Lab Assignment
5.10 Bibliography, References and Further Reading
5.11 Online References
5.0 OBJECTIVES
At the end of this chapter you will be able to,
15. Understand Scalar and Correlated Subqueries
16. Write and Execute Multiple Column Subqueries
17. Understand WITH clause
18. Understand Hierarchical queries
MissionMCA.com
87
5.1 INTRODUCTION
In the previous chapters we learnt that, queries can be nested so that the
results of one query can be used in another query via a relational
operator or aggregation function. This type of nested query is known as a
subquery. In this chapter we will discuss more about the specifics of using
subqueries.
To illustrate the concepts in this chapter, assume the tables EMP and
EMP_DETAILS having the following records.
Table EMP:
EMPID
DESIGNATION
DEPT
SALARY
1001
Manager
Finance
50000
1002
Executive
Finance
25000
1003
Senior Executive
Finance
35000
1004
Manager
HR
20000
1005
Executive
HR
20000
1006
Senior Executive
HR
30000
1007
Manager
Admin
55000
1008
Executive
Finance
25000
1009
Executive
Finance
25000
1010
Executive
HR
25000
Table EMP_DETAILS:
EMPID
ENAME
AGE
1001
Rajesh
23
1002
Tejal
25
MissionMCA.com
88
1003
Himesh
35
1004
Himali
37
1005
Rehan
40
1006
Kiran
28
1007
Ashima
21
1008
Vikram
31
1009
Ridhi
26
MissionMCA.com
89
Both the queries above will give the same result set. The first query is the
pair wise comparison while the second query is the non pair wise
comparison. The output of the above query is:
EMPID
1002
1008
1009
1010
For example,
MAX(INVIEW.TOTAL_EMP)
5
In the above query, we use the alias 'inview' for the subquery that begins
after the FROM clause. The subquery counts the number of employees
that work in each department & the outer query takes the values from the
MissionMCA.com
90
subquery using the MAX function finds the maximum number of
employees to give the result set.
Get the name and age of the employee enjoying maximum salary.
SELECT ename, age FROM emp_details
WHERE empid = (SELECT empid FROM emp WHERE salary =
(SELECT MAX(salary) FROM emp));
ENAME
AGE
Ashima
21
For example,
Get the list of employees whose salary are higher than or equal to
the average salary of their respective departments.
MissionMCA.com
91
SELECT e1.empid, e1.dept FROM emp e1
WHERE salary >= (SELECT AVG(salary) FROM emp e2 GROUP
BY e2.dept
HAVING e1.empid = e2.empid);
EMPID
DEPT
1001
Finance
1003
Finance
1006
HR
1007
Admin
To run the inner query we need to know the dept of the employee
selected in the outer query. For each and every department value coming
from the outer query the average salary of the department is calculated in
the inner query and compared with the salary of the employee in the outer
query.
5.5 WITH
CLAUSE)
CLAUSE
(SUBQUERY
FACTORING
MissionMCA.com
92
5.5.1 FUNCTIONS OF THE WITH CLAUSE
(15)
(16)
(17)
Named queries can reference other named queries that
came before them and even correlate to previous named queries.
(18)
The scope of the WITH clause is local to the SELECT in
which they are defined.
Consider the following examples,
Get the empid of the employee who works in the HR department
and gets the maximum salary.
WITH hr_sal AS
(SELECT empid, salary FROM emp WHERE dept = HR)
SELECT MAX (salary) FROM hr_sal;
MAX (SALARY)
30000
MissionMCA.com
93
and so on. The number of levels returned by a hierarchical query
may be limited by available user memory.
START WITH Clause used to specify a condition that
identifies the row(s) to be used as the root(s) of a hierarchical
query.
CONNECT BY Clause followed by the PRIOR keyword is used to
specify a condition that identifies the relationship between parent
rows and child rows of the hierarchy.
Assume the records in the EMP_NEW table
EMPID
ENAME
DESIGNATION
DEPT
SALARY
MGRID
1001
Rajesh
Manager
Finance
50000
1007
1002
Tejal
Executive
Finance
25000
1003
1003
Himesh
Senior Executive
Finance
35000
1001
1004
Himali
Manager
HR
20000
1010
1005
Rehan
Executive
HR
20000
1010
1006
Kiran
Senior Executive
HR
30000
1007
1007
Ashima
President
Admin
55000
1008
Vikram
Executive
Finance
25000
1003
1009
Ridhi
Executive
Finance
25000
1001
1010
Prakash
Executive
HR
25000
1006
MissionMCA.com
94
Ashima
(1007)
Rajesh
Kiran
Himesh
Ridhi
Prakash
(1003)
(1009)
(1010)
Tejal
Vikram
Himali
Rehan
EMPID
ENAME
MGRID
1007
Ashima
1001
Rajesh
1007
1006
Kiran
1007
1003
Himesh
1001
1009
Ridhi
1001
1010
Prakash
1006
1002
Tejal
1003
1004
Himali
1010
1005
Rehan
1010
1008
Vikram
1003
MissionMCA.com
95
The hierarchical query uses the LEVEL pseudo-column to return a
column that identifies the level of the employee within the hierarchy. In
addition, this query uses the LEVEL pseudo-column in the ORDER BY
clause to sort by this column.
After the FROM clause, this query uses the START WITH clause to
identify the row to be used as the root of the hierarchy. Finally the
CONNECT BY clause specifies the condition that identifies the
relationship between the parent rows and the child rows.
5.7 SUMMARY
MissionMCA.com
96
Explain the WITH clause. Why should you use the WITH clause?
What is a hierarchical query? Illustrate with the help of an
example.
Data type
Size
Constraint
CUSTOMER_ID
CHAR
CUSTOMER_NAME
VARCHAR2
20
NOT NULL
ADDRESS
VARCHAR2
20
UNIQUE
CITY
VARCHAR2
20
PINCODE
NUMBER
STATE
VARCHAR2
20
BALANCE_DUE
NUMBER
8,2
Data type
Size
Constraint
PRODUCT_CODE
CHAR
PRIMARY KEY
PRODUCT_NAME
VARCHAR2
20
UNIQUE
QTY_AVAIL
NUMBER
COST_PRICE
NUMBER
8,2
SELLING_PRICE
NUMBER
8,2
MissionMCA.com
97
3. Create a ORDER table with the following columns and constraints
Column name
Data type
Size
ORDER_NO
CHAR
ORDER_DATE
TIMESTAMP
CUSTOMER_ID
CHAR
PRODUCT_CODE
CHAR
QUANTITY
NUMBER
Constraint
List the details of all products whose price is less than average price
of the products.
List the customers who have ordered for the same products.
Get the name, price and quantity in stock of the costliest product.
MissionMCA.com
98
http://docs.oracle.com/cd/E11882_01/appdev.920/a96590/adg09dyn.htm
MissionMCA.com
99
Unit - III
6
INTRODUCTION TO PL/SQL
Unit Structure
6.0 Objectives
6.1 Introduction
6.2 PL/SQL Overview
6.2.1 PL/SQL Engine
6.2.2 Advantages of PL/SQL
6.3 PL/SQL blocks
6.3.1 PL/SQL Block Structure
6.3.2 PL/SQL Subprograms
6.3.3 PL/SQL Anonymous Blocks
6.4 PL/SQL Identifiers
6.4.1 Reserved words and Keywords
6.4.2 Predefined Identifiers
6.4.3 User-defined Identifiers
6.4.3.1 Ordinary User-Defined Identifiers
6.4.3.2 Quoted User-Defined Identifiers
6.5 PL/SQL Placeholders
6.5.1 PL/SQL Variables
6.5.2 PL/SQL Constants
6.6 PL/SQL DATA TYPES
6.6.1 SCALAR DATA TYPE
6.6.1.1 Character Data Type
6.6.1.2 Numeric Data Type
6.6.1.3 Boolean Data Type
6.6.1.4 DateTime Data Type
6.7 %TYPE attribute
6.8 USING BIND VARIABLES
6.9 SEQUENCES in PL/SQL Expressions
MissionMCA.com
100
6.10 Summary
6.11 Review Questions
6.12 Bibliography, References and Further Reading
6.13 Online References
6.0 OBJECTIVES
At the end of this chapter you will be able to
11. Understand Basic Concepts of PL/SQL coding,
12. Understand variables, identifiers and its types,
13. Different data types,
14. Different types of PL/SQL blocks,
15. The %TYPE Attribute and Sequences in PL/SQL Expressions.
6.1 INTRODUCTION
In Oracle, there is a special language available for developers to
code stored procedures that seamlessly integrate with database object
access via the language of database objects, SQL. However, this
language offers far more execution potential than simple updates, selects,
inserts, and deletes. This language offers a procedural extension that
allows for modularity, variable declaration, loops and other logic
constructs, and advanced error handling. This language is known as
PL/SQL. PL/SQL stands for Procedural Language extension of SQL.
MissionMCA.com
101
any valid PL/SQL unit. The engine runs procedural statements, but
sends SQL statements to the SQL engine in the database, as
shown in the figure below.
MissionMCA.com
102
MissionMCA.com
103
caught, specific actions can be taken depending upon the type of the
exception or it can be displayed to the user with a message.
MissionMCA.com
104
(21)
(22)
Header Section:
Declaration Section:
Execution Section:
Exception Section:
MissionMCA.com
105
MissionMCA.com
106
Cursors
Subprograms
Packages
Keywords
Labels
Reserved words
FROM
ALL_TYPES
WHERE
MissionMCA.com
107
6.4.3.1 Ordinary User-Defined Identifiers
An ordinary user-defined identifier:
Begins with a letter
Can include letters, digits, and these symbols:
Dollar sign ($)
Number sign (#)
Underscore (_)
Is not a reserved word.
Examples of acceptable ordinary user-defined identifiers:
X
t2
phone#
credit_limit
LastName
oracle$number
money$$$tree
SN##
try_again_
6.4.3.2 Quoted User-Defined Identifiers
A quoted user-defined identifier is enclosed in double
quotation marks. Between the double quotation marks, any
characters from the database character set are allowed except
double quotation marks, new line characters, and null characters.
For example, these identifiers are acceptable:
"X+Y"
"last name"
"on/off switch"
"employee(s)"
"*** header info ***"
MissionMCA.com
108
These are placeholders that store the values that can change
through the PL/SQL Block. In order to use a variable, you need to declare
it
in
the
declaration
section
of
the
PL/SQL
block.
The General Syntax to declare a variable is:
DECLARE
salary number (6);
salary is a variable of datatype number and of length 6.
The value of a variable can change in the execution or exception
section of the PL/SQL Block. We can assign values to variables in the two
ways given below.
We
can
directly
assign
values
to
variables.
The General Syntax is:
variable_name:= value;
We can assign values to variables directly from the database
columns by using a SELECT.. INTO statement. The General
Syntax is:
SELECT column_name
INTO variable_name
FROM table_name
[WHERE condition];
Example: The below program will get the salary of an employee with id
'1116' and display it on the screen.
MissionMCA.com
109
DECLARE
var_salary number(6);
var_emp_id number(6) = 1116;
BEGIN
SELECT salary
INTO var_salary
FROM employee
WHERE emp_id = var_emp_id;
dbms_output.put_line(var_salary);
dbms_output.put_line('The employee ' || var_emp_id || ' has
salary ' || var_salary);
END;
/
Scope of Variables
PL/SQL allows the nesting of Blocks within Blocks i.e, the
Execution section (BEGIN block) of an outer block can contain inner
blocks. Therefore, a variable which is accessible to an outer Block is also
accessible to all nested inner Blocks. The variables declared in the inner
blocks are not accessible to outer blocks. Based on their declaration we
can classify variables into two types.
Local variables - These are declared in a inner block and cannot
be referenced by outside Blocks.
Global variables - These are declared in a outer block and can
be referenced by its itself and by its inner blocks.
MissionMCA.com
110
For example:
pi CONSTANT NUMBER (5,4) := 3.1412;
Note: In this chapter, we will discuss only about scalar data types. The
remaining data types will be discussed in further chapters.
MissionMCA.com
111
6.6.1 SCALAR DATA TYPE
MissionMCA.com
112
6.6.1.4 DateTime Data Type
The PL/SQL data type DateTime stores date and time values with
fractional precision of seconds. The different DateTime data types
available for use in PL/SQL programs are
DATE, TIME, TIMESTAMP, TIMESTAMP WITH TIME ZONE, INTERVAL
YEAR TO MONTH, INTERVAL DAY TO SECOND, etc.
variable_name table_name.column_name%TYPE;
For Example, Declaring Variable of Same Type as Column
DECLARE
surname employees.last_name%TYPE;
BEGIN
DBMS_OUTPUT.PUT_LINE('surname=' || surname);
END;
/
MissionMCA.com
113
MissionMCA.com
114
SET SERVEROUTPUT ON
DECLARE
a number;
BEGIN
SELECT seq_temp.NEXTVAL INTO a FROM dual;
DBMS_OUTPUT.PUT_LINE(a);
END;
/
Note that before Oracle 11g we could not assign a sequence value to a
variablein PL/SQL. The only way to use sequence values within PL/SQL
was to use SELECT sequence.NEXTVAL INTO variable.
Oracle 11g allows to refer to sequence values in a very intuitive way
using variables. Consider the above code rewritten as follows:
CREATE SEQUENCE seq_temp START WITH 100 INCREMENT BY 1;
SET SERVEROUTPUT ON
DECLARE
a number;
BEGIN
a := seq_temp.NEXTVAL;
DBMS_OUTPUT.PUT_LINE(a);
END;
/
As of Oracle Database 11g Release 1, you can use
sequence_name.CURRVAL and sequence_name.NEXTVAL in a PL/SQL
expression wherever you can use a NUMBER expression. However:
Using sequence_name.CURRVAL or sequence_name.NEXTVAL
to provide a default value for an ADT method parameter causes a
compilation error.
PL/SQL
evaluates
every
occurrence
of
sequence_name.CURRVAL
and
sequence_name.NEXTVAL
(unlike SQL, which evaluates a sequence expression for every
row in which it appears).
MissionMCA.com
115
Note: Each time you reference sequence_name.NEXTVAL, the
sequence is incremented immediately and permanently, whether you
commit or roll back the transaction.
6.10 SUMMARY
PL/SQL was developed by Oracle Corporation in the early 90s to
enhance the capabilities of SQL.
PL/SQL is a combination of SQL along with the procedural features of
programming languages.
The PL/SQL engine is the tool used to define, compile, and run
PL/SQL program units.
The basic unit in PL/SQL is a block, which groups related declarations
and statements. All PL/SQL programs are made up of blocks, which
can be nested within each other.
PL/SQL blocks are categorized into subprograms (procedures and
functions) and anonymous block.
A PL/SQL Block consists of the following sections:
The Header section.
The Declaration section.
The Execution section.
The Exception (Error Handling) section.
A PL/SQL subprogram is a named PL/SQL block that is either a
procedure or a function.
An anonymous block is an unnamed, non-persistent PL/SQL unit.
Identifiers are used to name PL/SQL program items & units.
Identifiers can be divided into the following types:
Reserved words and keywords
Predefined identifiers
User-defined identifiers
Oracle defines placeholders to store data temporarily, which are used
to manipulate data during the execution of a PL SQL block.
MissionMCA.com
116
Placeholders can be any of Variables, Constants and Records.
Every PL/SQL constant, variable, parameter, and function return
value has a data type that determines its storage format and its valid
values and operations.
Data types in PL/SQL can be divided into
Scalar Data Types
Composite Data Types
Reference Data Types
LOB Data Types
A scalar type has no internal components. It holds a single value,
such as number or character string.
The scalar types fall into 4 families, which store numeric, character,
boolean, & datetime data.
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).
Bind variable is a special type of variable that runs more efficiently
than SQL statements that use regular variables.
Sequences in PL/SQL expressions execute in a similar way as
sequences in SQL expressions using the CURRVAL pseudocolumn
and the NEXTVAL pseudocolumn.
MissionMCA.com
117
Explain the different types of scalar data types used in PL/SQL.
Why is %TYPE attribute used?
MissionMCA.com
118
PL/SQL tutorial
http://plsql-tutorial.com/index.htm
http://www.academictutorials.com/pl-sql/introduction.asp
Wikipedia links
http://en.wikipedia.org/wiki/PL/SQL
MissionMCA.com
119
7
WRITING EXECUTABLE STATEMENTS
Unit Structure
7.0 Objectives
7.1 Introduction
7.2 Basic Guidelines for PL/SQL block syntax
7.2.1 Rules of block Structure
7.2.2 Scope and Visibility of Identifiers
7.2.3 Handling variables in PL/SQL
7.2.4 Guidelines for PL/SQL coding
7.3 Comments in Code
7.4 Nested Blocks
7.5 DATA CONVERSION
7.5.1 Conversion Functions
7.6 Operators in PL/SQL
7.6.1 Mathematical Operators
7.6.2 Comparison or Relational Operators
7.6.3 Logical or Boolean Operators
7.6.4 Special Operators
7.7 Summary
7.8 Review Questions
7.9 Lab Assignment
7.10 Bibliography, References and Further Reading
7.11 Online References
MissionMCA.com
120
7.0 OBJECTIVES
7.1 INTRODUCTION
PL/SQL stands for Procedural Language/SQL. PL/SQL extends
SQL by adding constructs found in procedural languages, resulting in a
structural language that is more powerful than SQL. In other words,
PL/SQL is a procedural programming language that enhances the
functionalities of SQL. We can write various types of queries with SQL
statements but with PL/SQL we can group the related queries together to
perform a particular task or an activity.
PL/SQL offers many advantages over other programming
languages for handling the logic and business rule enforcement of
database applications. It is a straightforward language with all the
common logic constructs associated with a programming language, plus
many things other languages dont have, such as robust error handling
and modularization of code blocks. The PL/SQL code used to interface
with the database is also stored directly on the Oracle database, and is
the only programming language that interfaces with the Oracle database
natively and within the database environment.
MissionMCA.com
121
SELECT statements within PL/SQL blocks (which are not within a
cursor definition), you must store the returned values from a SELECT.
If PL/SQL variables or objects are defined for use in a block then you
must also have a DECLARE section.
If you include an EXCEPTION section the statements within it are only
processed if the condition to which they refer occurs. Block execution
is terminated after an exception handling routine is executed.
PL/SQL blocks may be nested, nesting can occur wherever an
executable statement could be placed (including the EXCEPTION
section).
Capitalize all keywords, and use lowercase for the other code in
a PL/SQL statement.
Separate the words in names with underscores, as in
last_name, first_name.
Start each clause on new line.
Break long clauses into multiple lines and indent continued
lines.
Use comments only for portions of code that are difficult to
MissionMCA.com
122
DECLARE
some_condition BOOLEAN;
pi
NUMBER := 3.1415926;
radius
NUMBER := 15;
area
NUMBER;
BEGIN
-- Perform some simple tests and assignments
IF 2 + 2 = 4 THEN
some_condition := TRUE;
/* We expect this THEN to always be performed */
END IF;
MissionMCA.com
123
/* This line computes the area of a circle using pi,
which is the ratio between the circumference and diameter.
After the area is computed, the result is displayed. */
area := pi * radius**2;
DBMS_OUTPUT.PUT_LINE('The area is: ' || TO_CHAR(area));
END;
/
MissionMCA.com
124
BEGIN
DECLARE
v_sum NUMBER (10);
v_avg NUMBER (10,2);
BEGIN
x := 10;
y := 20;
v_sum := x+y;
DBMS_OUTPUT.PUT_LINE('sum := '|| v_sum);
v_avg := (x+y)/2;
DBMS_OUTPUT.PUT_LINE('average := '|| v_avg);
END;
END;
MissionMCA.com
125
BEGIN
x := 10;
y := 20;
z := x+y;--adding 2 numeric values and storing result in z
p := x;
q := y;
r := p+q;--adding 2 character values and storing result in r
cd := '10-NOV-2011';
d := cd; --converts character data into date type of format
-- printing sum of two numbers
DBMS_OUTPUT.PUT_LINE('sum of x and y := '|| z);
-- printing sum of two VARCHAR2 data type values
DBMS_OUTPUT.PUT_LINE('sum of p and q := '|| r);
-- printing date in system format
DBMS_OUTPUT.PUT_LINE('Meeting is on '|| d);
END;
/
7.5.1 Conversion Functions
Oracle has several built-in functions that are designed to convert
information from one data type to another data type. The most commonly
used conversion functions are:
Function Name
Description
Example
TO_CHAR()
TO_DATE()
TO_NUMBER
The
TO_NUMBER TO_NUMBER('1010')
conversion function converts
character data to data type
number.
MissionMCA.com
126
Mathematical Operators
Comparison/Relational Operators
Logical/Boolean Operators
Special Operators
Description
**
Multiplication Operator.
Division Operator.
MissionMCA.com
127
7.6.2 Comparison or Relational Operators
These operators are used to compare values.
Operator
Description
Equal to
<
Less than
>
Greater than
<=
>=
Operator
Description
AND
OR
NOT
X AND Y
X OR Y
NOT X
TRUE
TRUE
TRUE
TRUE
FALSE
MissionMCA.com
128
TRUE
FALSE
FALSE
TRUE
FALSE
FALSE
TRUE
FALSE
TRUE
TRUE
FALSE
FALSE
FALSE
FALSE
TRUE
Operator
Description
IN
BETWEEN
LIKE
IS NULL
||
7.7 SUMMARY
MissionMCA.com
129
Every unit of PL/SQL must constitute a block. As a minimum there
must be the delimiting words BEGIN and END around the executable
statements.
If PL/SQL variables or objects are defined for use in a block then you
must also have a DECLARE section.
PL/SQL blocks may be nested, nesting can occur wherever an
executable statement could be placed (including the EXCEPTION
section).
The scope of an identifier is the region of a PL/SQL unit from which
you can reference the identifier.
The visibility of an identifier is the region of a PL/SQL unit from which
you can reference the identifier without qualifying it.
An identifier is local to the PL/SQL unit that declares it. If that unit has
subunits, the identifier is global to them.
Variables must be declared first before the usage.
%TYPE attribute can be used to define a variable which is of the
same type as a database column's type definition.
When executing PL/SQL program, comments are ignored by the
PL/SQL compiler.
Adding comments promotes readability and maintainability of the
code.
PL/SQL supports two types of comments: Single-Line comments and
Multi-Line comments.
PL/SQL allows the nesting of blocks within blocks i.e., the Execution
section of an outer block can contain inner blocks.
Based on their declaration we can classify variables into two types:
Local variables and Global variables.
Two types of conversion, from one data type to another, are allowed
in Oracle: Implicit Conversion and Explicit Conversion.
An operator manipulates data items and returns a result. Operators
are represented by special characters or by keywords.
MissionMCA.com
130
PL/SQL operators can be divided into the following categories
Mathematical Operators
Comparison/Relational Operators
Logical/Boolean Operators
Special Operators
ENAME
HIREDATE
DEPTNO
111
Satish
19-DEC-2008 10
10,000
1000
222
Rashmi
01-JAN-1987 20
8000
550
333
Rishi
05-JUN-1976 10
7000
450
444
Anil
16-APR-1967 10
12,000
2000
555
Anita
30
1000
666
Nilesh
20-MAY-1987 20
13,000
777
Ruchi
11-JUN-2000 30
888
Sarika
GENDER SALARY
COMM
MissionMCA.com
131
Retrieve the deptno, ename, salary and comm for all employees.
Retrieve the deptno, empno and total salary as salary + 10% of
(salary+comm) for all employees.
List the employees who have joined after 1st May 1987.
List the male employees of department number 10 and 30.
Display the employees who earned commission more than 1000 and
belong to department 10 or have salary more than 10,000/- but do not
belong to department 10.
List employees with zero salary.
List employees with either salary or commission unknown.
Display the employees whose name contains 'SA'.
Display the salary of employees as ZERO if unknown.
MissionMCA.com
132
Chapter 5 Introducing PL/SQL
http://www.cs.kent.edu/~wfan/link/dbapre/dbatest/54905f.htm
Oracle SQL & PL/SQL
http://sql-plsql.blogspot.in/2007/05/oracle-plsql-cursors-withparameters.html
http://sql-plsql.blogspot.in/2007/03/plsql-introduction.html
PL/SQL tutorial
http://plsql-tutorial.com/index.htm
http://www.academictutorials.com/pl-sql/introduction.asp
Wikipedia links
http://en.wikipedia.org/wiki/PL/SQL
MissionMCA.com
133
8
INTERACTION WITH THE ORACLE
SERVER
Unit Structure
8.0 Objectives
8.1 Introduction
8.2 Invoking SELECT statement
8.2.1 Restrictions in SELECT INTO clause
8.3 Data Manipulation in the server using PL/SQL
8.4 SQL Cursor Concept
8.4.1 Implicit Cursors
8.5 Save And Discard Transactions
8.5.1 Implicit Rollbacks
8.5.2 Ending Transactions
8.6 Summary
8.7 Review Questions
8.8 Lab Assignment
8.9 Bibliography, References and Further Reading
8.10 Online References
8.0 OBJECTIVES
At the end of this chapter you will be able to:
Invoke SELECT Statements in PL/SQL,
Manipulate Data in the Server using PL/SQL,
MissionMCA.com
134
Understand the SQL Cursor concept,
Use SQL Cursor Attributes to Obtain Feedback on DML,
Save and Discard Transactions.
8.1 INTRODUCTION
No usage of PL/SQL is complete without presenting the ease of
use involved in interacting with the Oracle database. Any data
manipulation or change operation can be accomplished within PL/SQL
without the additional overhead typically required in other programming
environments. There is no ODBC interface, and no embedding is required
for use of database manipulation with PL/SQL.
In this chapter we will invoke SELECT statements in PL/SQL and
also manipulate data in the server. Then we will discuss the concept of
cursor in SQL along with how to use cursor attributes in DML. Finally we
shall have a look on how to save and discard transactions in a database.
MissionMCA.com
135
Consider the following example:
To get the details of employees who work for the HR department and gets
salary of 30000.
SET SERVEROUTPUT ON
DECLARE
v_name emp_detail.ename%TYPE;
v_age emp_detail.age%TYPE;
BEGIN
SELECT ename, age INTO v_name, V_age
FROM emp_detail WHERE empid =
( SELECT empid FROM emp
WHERE dept = 'HR' AND salary = 30000);
MissionMCA.com
136
In the following program, a row is added, updated and deleted from the
EMPLOYEE table using variables in PL/SQL to manipulate data in the
database tables.
SET SERVEROUTPUT ON
DECLARE
My_employee employee%ROWTYPE;
My_lastname VARCHAR2 := SAMSON;
My_firstname VARCHAR2 := DELILAH;
My_salary NUMBER := 49500;
BEGIN
SELECT *
INTO my_employee
FROM employee
WHERE empid = 49594;
UPDATE employee
SET salary = my_employee.my_salary + 10000
WHERE empid = my_employee.my_empid;
INSERT INTO employee (empid, lastname, firstname, salary)
VALUES (emp_sequence.nextval, my_lastname, my_firstname,
my_salary);
My_empid := 59495;
DELETE FROM employee
WHERE empid = my_empid;
END;
/
MissionMCA.com
137
For SQL statements, there are two types of cursors:
Implicit cursor
Explicit cursor
In this chapter we will discuss about implicit cursors. Explicit cursors have
been explained in the later chapters.
8.4.1 Implicit Cursors
An implicit cursor is a session cursor that is constructed and
managed by PL/SQL. PL/SQL opens an implicit cursor every time you run
a SELECT or DML statement. You cannot control an implicit cursor, but
you can get information from its attributes. An implicit cursor closes after
its associated statement runs; however, its attribute values remain
available until another SELECT or DML statement runs. Cursor
attributes return information about the state of the cursor.
The syntax for the value of an implicit cursor attribute is SQLattribute (for
example, SQL%FOUND). SQLattribute always refers to the most recently
run DML or SELECT INTO statement.
The implicit cursor attributes are:
The table given below lists the cursor attributes and the values that they
can return.
Cursor Attribute Cursor Variable
%FOUND
SQL%FOUND
%NOTFOUND
SQL%NOTFOUND
MissionMCA.com
138
not return a row, TRUE.
%ROWCOUNT
SQL%ROWCOUNT
%ISOPEN
SQL%ISOPEN
MissionMCA.com
139
MissionMCA.com
140
28. The SAVEPOINT statement identifies a point in a transaction to which
you can later roll back.
The session in the following table illustrates the use of the above
statements
Time Session
Explanation
t0
COMMIT;
t1
SET TRANSACTION
'sal_update';
t2
t3
existing
UPDATE employees
SET salary = 7000
WHERE
last_name
'Sharma';
t4
UPDATE employees
SET salary = 12000
WHERE last_name = 'Naik';
t5
SAVEPOINT after_naik_sal;
t6
ROLLBACK TO SAVEPOINT
after_sharma_sal;
t7
UPDATE employees
SET salary = 11000
WHERE
last_name
'Mehta';
t8
ROLLBACK;
MissionMCA.com
141
Time Session
Explanation
NAME This
t9
SET TRANSACTION
'sal_update2';
t10
UPDATE employees
SET salary = 7050
WHERE last_name = 'Lal';
t11
UPDATE employees
SET salary = 10950
WHERE
last_name
'Yadav';
t12
COMMIT;
=
This statement commits all changes
made in transaction sal_update2,
ending the transaction. The commit
guarantees that the changes are saved
in the online redo log files.
MissionMCA.com
142
WHERE employee_id = emp_id;
DELETE FROM emp_name
WHERE employee_id = 130;
SAVEPOINT my_savepoint2;
INSERT INTO emp_name (employee_id, last_name, salary)
VALUES (emp_id, emp_lastname, emp_salary);
COMMIT;
EXCEPTION
WHEN DUP_VAL_ON_INDEX THEN
ROLLBACK TO my_savepoint2;
DBMS_OUTPUT.PUT_LINE('Transaction rolled back.');
END;
/
8.5.1 Implicit Rollbacks
Before running an INSERT, UPDATE, or DELETE statement, the
database marks an implicit savepoint (unavailable to you). If the
statement fails, the database rolls back to the savepoint. Usually, just the
failed SQL statement is rolled back, not the whole transaction. If the
statement raises an unhandled exception, the host environment
determines what is rolled back.
The database can also roll back single SQL statements to break
deadlocks. The database signals an error to a participating transaction
and rolls back the current statement in that transaction.
Before running a SQL statement, the database must parse it, that
is, examine it to ensure it follows syntax rules and refers to valid schema
objects. Errors detected while running a SQL statement cause a roll back,
but errors detected while parsing the statement do not.
8.5.2 Ending Transactions
You should explicitly commit or roll back every transaction. If you
do not commit or roll back a transaction explicitly, then Oracle determines
its final state. For example,
A user issues a COMMIT or ROLLBACK statement without a
SAVEPOINT clause.
In a commit, a user explicitly or implicitly requested that the
changes in the transaction be made permanent. Changes made
by the transaction are permanent and visible to other users only
after a transaction commits.
MissionMCA.com
143
A user runs a DDL command such as CREATE, DROP,
RENAME, or ALTER.
The database issues an implicit COMMIT statement before and
after every DDL statement. If the current transaction contains DML
statements, then Oracle Database first commits the transaction
and then runs and commits the DDL statement as a new, singlestatement transaction.
A user exits normally from most Oracle Database utilities and
tools, causing the current transaction to be implicitly committed.
A client process terminates abnormally or executes a ROLLBACK
statement, causing the transaction to be implicitly or explicitly
rolled back.
8.6 SUMMARY
The SELECT statement in SQL is used to extract data from the one or
more tables or views in the database.
The SELECT INTO statement retrieves values from one or more
database tables (as the SQL SELECT statement does) and stores
them in variables (which the SQL SELECT statement does not do).
PL/SQL blocks can be used to manipulate data in the server using
INSERT, DELETE and UPDATE statements.
Oracle performs a set of tasks for executing any SQL statement.
Implicit cursor
Explicit cursor
MissionMCA.com
144
the database concurrently, and ensures that each user sees a
consistent version of data and that all changes are applied in the right
order.
Transaction control is the management of changes made by DML
statements and the grouping of DML statements into transactions.
Transaction control involves using the following statements:
Explain
COMMIT,
ROLLBACK
and
ENAME
DEPT
DESG
SALARY
VARCHAR2
VARCHAR2
VARCHAR2
VARCHAR2
NUMBER
MissionMCA.com
145
Delete all the records of employees from the department HR getting
salary less than 10,000/- and also print the total number of records
deleted.
Modify the salary of the employees in the Admin department to give
them a hike of 25%.
MissionMCA.com
146
http://sql-plsql.blogspot.in/2007/03/plsql-introduction.html
PL/SQL tutorial
http://plsql-tutorial.com/index.htm
http://www.academictutorials.com/pl-sql/introduction.asp
Wikipedia links
http://en.wikipedia.org/wiki/PL/SQL
MissionMCA.com
147
UNIT - IV
9
CONTROL STRUCTURES
Unit Structure
9.1 Objectives
9.2 Control Structure
9.2.1 IF and CASE Statements
A) If Statement
B) IF-THEN Statement
C) IF-THEN-ELSE Statement
D) NESTED IF-THEN- ELSE statements:
9.3 CASE Statement
A) Simple CASE Syntax
B) Searched CASE Syntax
9.4 Loop Statement
9.5 Exit Statement
9.6 Labeling a PL/SQL Loop
9.7 WHILE Statement
9.8 FOR Statement
9.9 Continue Statement
A) CONTINUE Statement:
B) CONTINUE-WHEN Statement:
9.10 Questions
9.11 Further Reading
9.1 OBJECTIVE
After completing this chapter, you will be able to:
Understand how to use the conditional and looping
structures using PLSQL.
Understand how to utilize the control structures in queries.
MissionMCA.com
148
Aware of the syntax and examples for manipulating the
database using queries with the help of various control and
looping structures.
Understand the Continue statement for updating the
processes after a successful call.
MissionMCA.com
149
9.2.1 IF and CASE Statements
A) IF Statements:
The IF statements executes a sequence of statements,
depending on the value of a condition. When the condition is
satisfied, the necessary option will be executed. There are three
forms of IF statements: IF-THEN, IF-THEN-ELSE, and IF-THEN-ELSIF.
We can form the Condition using relational operators given in
following table. These operators help us to define different kind of
conditions or combination of conditions using following operator
Symbols.
The IF & CASE statements are used in various kinds of
operations on the database. These can be used to sort the data as
well as to find out the exact entry from the table cell depending on
its value. These two operations can used together if the need arise
in operation.
Operator
Meaning
>
>=
<
<=
Equal to Operator
LIKE
BETWEEN..AND
IN
IS NULL
B) IF-THEN Statement:
This statement uses the simple condition to evaluate, then it
executes the statement or a block of statements if the condition
returns true. The simplest form of IF statement associates a
condition with a sequence of statements enclosed by the keywords
THEN and END IF (not ENDIF).
MissionMCA.com
150
The general format of an IF statement is:
IF condition THEN
ex_statements
END IF;
Example:
SQL> DECLARE
2 a NUMBER(6);
3 b NUMBER(6);
4 BEGIN
5 a := 24;
6 b := 34;
7 IF a < b THEN
8 DBMS_OUTPUT.PUT_LINE(a || 'is less than' || b);
9 END IF;
10 END;
11 /
24 is less than 34
PL/SQL procedure successfully completed.
Example:
SQL> DECLARE
2 a VARCHAR(12);
3 b VARCHAR(12);
4 BEGIN
5 a := 'YASHASHREE';
6 b := 'YASHASHREE';
7 IF a LIKE b THEN
8 DBMS_OUTPUT.PUT_LINE(a || 'is Same as ' || b);
9 END IF;
10 END;
11 /
YASHASHREE is Same as YASHASHREE
PL/SQL procedure successfully completed.a
C) IF-THEN-ELSE Statement:
In second type of IF statement it adds the keyword ELSE
followed by an alternative sequence of statements. In this if the
condition returns true then first block of statement executes and if
the condition returns false then second block of statement
executes.
MissionMCA.com
151
The general format of an IF-THEN-ELSE statement is:
IF condition THEN
true_ ex_statement;
ELSE
false_ ex_statement;
END IF;
Example:
SQL> DECLARE
2 marks NUMERIC;
3 BEGIN
4 marks := '45';
5 IF marks>35 THEN
6
DBMS_OUTPUT.PUT_LINE('PASS');
7 ELSE
8
DBMS_OUTPUT.PUT_LINE('FAIL');
9 END IF;
10 END;
11 /
PASS
PL/SQL procedure successfully completed.
Example:
SQL> DECLARE
2 a VARCHAR(12);
3 b VARCHAR(12);
4 BEGIN
5 a := 'YASHASHREE';
6 b := 'yashashree';
7 IF a LIKE b THEN
8 DBMS_OUTPUT.PUT_LINE(a || 'is Same as ' || b);
9 ELSE
10 DBMS_OUTPUT.PUT_LINE(a || 'is not Same as ' || b);
11 END IF;
12
13 END;
14 /
YASHASHREE
MissionMCA.com
152
a series and execute the appropriate block of statements
depending on the conditions.
The general format of an IF-THEN-ELSE statement is:
IF <condition1>
THEN
...
ELSIF <condition2>
THEN
...
ELSIF <condition2>
THEN
...
END IF;
Example :
SQL>
DECLARE
2 marks NUMERIC;
3 BEGIN
4
marks := '67';
5
IF marks >= 75 THEN
6
DBMS_OUTPUT.PUT_LINE('YOU GOT DISINCTION');
7
ELSIF marks >= 60 AND marks<75 THEN
8
DBMS_OUTPUT.PUT_LINE('YOU GOT FIRST CLASS');
9
ELSIF marks >= 50 AND marks<60 THEN
10
DBMS_OUTPUT.PUT_LINE('YOU GOT SECOND CLASS ');
11
ELSIF marks >= '40' AND marks<50 THEN
12
DBMS_OUTPUT.PUT_LINE('YOU GOT PASS CLASS ');
13
ELSE
14 DBMS_OUTPUT.PUT_LINE('Sorry. You are fail..... ');
15
16 END IF;
17 END;
18 /
YOU GOT FIRST CLASS
PL/SQL procedure successfully completed.
Example :
SQL> DECLARE
2
D VARCHAR(10):= TO_CHAR(SYSDATE,'DY');
3 BEGIN
4 IF D= 'SAT' THEN
5 DBMS_OUTPUT.PUT_LINE('ENJOY YOUR WEEKEND');
6 ELSIF D= 'SUN' THEN
7 DBMS_OUTPUT.PUT_LINE('ENJOY YOUR WEEKEND');
8 ELSE
9 DBMS_OUTPUT.PUT_LINE('HAVE A NICE DAY ');
MissionMCA.com
153
10 END IF;
11 END;
12 /
HAVE A NICE DAY
PL/SQL procedure successfully completed.
MissionMCA.com
154
4
5
6
7
8
9
10
11
12
13
14
grade := 'D';
CASE grade
WHEN 'A' THEN DBMS_OUTPUT.PUT_LINE('Excellent');
WHEN 'B' THEN DBMS_OUTPUT.PUT_LINE('Very Good');
WHEN 'C' THEN DBMS_OUTPUT.PUT_LINE('Good');
WHEN 'D' THEN DBMS_OUTPUT.PUT_LINE('Fair');
WHEN 'F' THEN DBMS_OUTPUT.PUT_LINE('Poor');
ELSE DBMS_OUTPUT.PUT_LINE('No such grade');
END CASE;
END;
/
Fair
PL/SQL procedure successfully completed.
Example:
SQL> DECLARE
2
Product VARCHAR(20);
3 BEGIN
4
Product := 'PEN';
5
CASE Product
6
WHEN 'PEN' THEN DBMS_OUTPUT.PUT_LINE('PRICE IS
20 RS.');
7
WHEN 'PENCIL' THEN DBMS_OUTPUT.PUT_LINE('PRICE
IS 10 RS.');
8 WHEN 'ERASER' THEN DBMS_OUTPUT.PUT_LINE('PRICE
IS 5 RS.');
9
WHEN
'SHARPNER'
THEN
DBMS_
OUTPUT.
PUT_LINE('PRICE IS 10 RS.');
10
11
12
13
14
PRICE IS 20 RS.
PL/SQL procedure successfully completed.
B) Searched CASE Syntax:
CASE
WHEN SearchCondition THEN
ex_statements 1;
WHEN SearchCondition THEN
ex_statements 2;
:
END StatementN;
END CASE;
MissionMCA.com
155
Example :
SQL> DECLARE
2 grade CHAR(1):='C';
3 apprisal VARCHAR(20);
4 id number:=124;
5 attnd NUMBER:=150;
6 min_days CONSTANT NUMBER:=200;
7 BEGIN
8 apprisal:=
9 CASE
10
WHEN grade = 'F' OR attnd<min_days
11
THEN 'Poor'
12
WHEN grade = 'A' THEN 'Excellent'
13
WHEN grade = 'B' THEN 'Very Good'
14
WHEN grade = 'C' THEN 'Good'
15
WHEN grade = 'D' THEN 'Fair'
16
WHEN grade = 'F' THEN 'Poor'
17
ELSE 'No such grade'
18
END;
19
DBMS_OUTPUT.PUT_LINE ('Result for student '||id||' is
'||apprisal);
20 END;
21 /
Result for student 124 is Poor
PL/SQL procedure successfully completed.
MissionMCA.com
156
If EXIT is used with WHEN clause, then the current loop is
terminated only when the condition given after WHEN is satisfied.
We can put EXIT statements anywhere inside a loop, but not outside
a loop. To complete a PL/SQL block before it reaches its normal
end, use the RETURN statement.
Example:
SQL> DECLARE
2 c NUMBER(6);
3 BEGIN
4 c := 1;
5 LOOP
6 DBMS_OUTPUT.PUT_LINE('aI:' || c);
7 c := c + 1;
8 IF c > 5 THEN
9 EXIT;
10 END IF;
11 DBMS_OUTPUT.PUT_LINE('bI:' || c);
12 END LOOP;
13 END;
14 /
aI:1
bI:2
aI:2
bI:3
aI:3
bI:4
aI:4
bI:5
aI:5
PL/SQL procedure successfully completed.
Example :
SQL> DECLARE
2 num NUMBER(6);
3 BEGIN
4 num := 4;
5 LOOP
6 DBMS_OUTPUT.PUT_LINE('Table of 4:' || num);
7 num := num + 4;
8 IF num > 40 THEN
9 EXIT;
10 END IF;
11 END LOOP;
12 END;
13 /
Table of 4:4
MissionMCA.com
157
Table of 4:8
Table of 4:12
Table of 4:16
Table of 4:20
Table of 4:24
Table of 4:28
Table of 4:32
Table of 4:36
Table of 4:40
PL/SQL procedure successfully completed.
MissionMCA.com
158
18
DBMS_OUTPUT.PUT_LINE('The sum of products is
equals to: ' || TO_CHAR(a));
19 END;
20 /
sum of products is equals to: 166
PL/SQL procedure successfully completed.
MissionMCA.com
159
6 LOOP
7 DBMS_OUTPUT.PUT_LINE('Table of 10 :' || num);
8 num := num + 10;
9 END LOOP;
10 END;
11 /
Table of 10 :10
Table of 10 :20
Table of 10 :30
Table of 10 :40
Table of 10 :50
Table of 10 :60
Table of 10 :70
Table of 10 :80
Table of 10 :90
Table of 10 :100
PL/SQL procedure successfully completed.
MissionMCA.com
160
8
END LOOP;
9
END;
10 /
Table of 19 :19
Table of 19 :38
Table of 19 :57
Table of 19 :76
Table of 19 :95
Table of 19 :114
Table of 19 :133
Table of 19 :152
Table of 19 :171
Table of 19 :190
PL/SQL procedure successfully completed.
Example :
SQL> CREATE
VARCHAR(20));
Table created.
TABLE
temp(num
NUMERIC,
str
SQL> BEGIN
2 FOR i IN 1..10 LOOP
3
IF MOD(i,2) = 0 THEN
4
INSERT INTO temp VALUES(i,'number is even');
5
ELSE
6
INSERT INTO temp VALUES(i,'number is odd');
7
END IF;
8
END LOOP;
9 END;
10 /
PL/SQL procedure successfully completed.
SQL> SELECT * FROM temp;
NUM
-------1
2
3
4
5
6
7
8
9
10
STR
-------------------number is odd
number is even
number is odd
number is even
number is odd
number is even
number is odd
number is even
number is odd
number is even
10 rows selected.
MissionMCA.com
161
If REVERSE option is used the following steps will take place:
1. Counter is set to upper range.
2. If counter is greater than or equal to lower range then statements
are executed otherwise loop is terminated.
3. Counter is decremented by one.
4. Go to step 2.
Example
SQL> DECLARE
2
n NUMBER := 10;
3
BEGIN
4
DBMS_OUTPUT.PUT_LINE('RESULT');
5
FOR i IN REVERSE 1..n LOOP
6
DBMS_OUTPUT.PUT_LINE(i);
7
END LOOP;
8
END;
9 /
RESULT
10
9
8
7
6
5
4
3
2
1
PL/SQL procedure successfully completed.
MissionMCA.com
162
MissionMCA.com
163
5 END LOOP;
6 END;
7 /
Odd Num := 1
Odd Num := 3
Odd Num := 5
Odd Num := 7
Odd Num := 9
PL/SQL procedure successfully completed.
9.10 QUESTIONS
1. Explain the PLSQL control Structure with its types and Syntax.
2. What are the different Operators used in IF statement?
3. Explain IF-THEN-ELSE Statement with help of Example.
4. Write a short example to demonstrate use of Case Statement in
PLSQL.
5. Explain Searched CASE statement in PLSQL with Help of
Example.
6. Write Short note on EXIT and Continue Statement in PLSQL.
7. How to Label PLSQL loop?
8. Write a short example to demonstrate use of WHILE loop in
PLSQL.
9. Write a short example to demonstrate use of FOR loop in
PLSQL.
Practice Questions:
10. Write a PL/SQL block of code for reverse number(e.g. 123=321)
11. If there are no transaction taken place in the last 365 days then
mark the account status as inactive and then record the account
number, opening date and the type of account in new table.
12. Write a PL/SQL block of code for area of a Triangle two times
with different values. Store the values in a table.
13. Write a PL/SQL block of code for area of a Circle three times
with different values. Store the values in a table.
14. Write a PL/SQL block of code that will accept an ID number of a
student check if students presence is less than 75% then
declared not eligible for exam. (Create Student table with proper
fields.)
MissionMCA.com
164
Fifth
Edition
By
Steven
MissionMCA.com
165
10
COMPOSITE DATA TYPE & CURSORS
Unit Structure
10.1 Objectives
10.2 Introduction to Composite Data types
10.3 Collection
10.4 Index by Tables
10.5 Collection Methods
10.6 How to use INDEX BY Table of Records?
10.7 Introduction to PLSQL Record
A) %TYPE and %ROWTYPE
B) Use of %TYPE
10.8 Writing Insert and Update with PL/SQL Records
10.9 Cursor
10.10 Classification of CURSORS
10.11 Using Cursor in FOR loop
10.12 The %NOTFOUND and %ROWCOUNT Attributes
10.13 FOR UPDATE Clause and WHERE CURRENT Clause
10.14 Questions
10.15 Further Reading
10.1 OBJECTIVES
After completing this chapter, we will be able to:
Learn and understand the Composite Data types.
Understand the structure and working of Collections.
Implement Collection Methods
Understand the PLSQL Records
Understand the Cursor implementation
Implement the Cursor in FOR loop
MissionMCA.com
166
10.3 COLLECTION:
A collection is referred as a sequence of multiple elements. It
is an ordered group of elements, all of the same type. It is a general
concept that includes lists, arrays, and other datatypes used in
classic programming algorithms. Each element is addressed by a
unique subscript.
PL/SQL offers following collection types:
MissionMCA.com
167
SQL> DECLARE
2 TYPE table_type IS TABLE OF NUMBER(10)
3
INDEX BY BINARY_INTEGER;
4
5 v_tab table_type;
6 v_idx NUMBER;
7 BEGIN
8 -- Initialise the collection.
9 << load_loop >>
10 FOR i IN 1 .. 5 LOOP
11
v_tab(i) := i;
12 END LOOP load_loop;
13
14 -- Delete the third item of the collection.
15 v_tab.DELETE(3);
16
17 -- Traverse sparse collection
18 v_idx := v_tab.FIRST;
19 << display_loop >>
20 WHILE v_idx IS NOT NULL LOOP
21
DBMS_OUTPUT.PUT_LINE('The number ' || v_tab(v_idx));
22
v_idx := v_tab.NEXT(v_idx);
23 END LOOP display_loop;
24 END;
25 /
The number 1
The number 2
The number 4
The number 5
PL/SQL procedure successfully completed.
MissionMCA.com
168
B) COUNT:- This method help to count the number of elements
in a collection.
Example:
IF subs.COUNT = 24 THEN ...
C) LIMIT:- This method is used in varrays, LIMIT returns the
bound value or maximum number of elements the varray can
contain.
Example:
IF subs.LIMIT = 24 THEN ...
D) FIRST:- This method returns the first member of the
collection.
Example:
IF sub.FIRST = sub.LAST THEN ...
E) LAST:- This method returns the last member of the
collection.
Example:
IF sub.FIRST = sub.LAST THEN ...
F) PRIOR:- This method returns the preceding index number of
nth element.
Example:
n := subs.PRIOR(subs.FIRST);
G) PRIOR:- This method returns the next or succeeding index
number of nth element.
Example:
a := subs.NEXT(a);
H) EXTEND:- This method is used to increase the size of
nested table or varray. It either appends one null element to
a collection, n null elements or n copies of ith element of a
collection.
Example :
subs.EXTEND(5,1); // It appends 5 copies of element 1.
I) TRIM:- this method is used to decrease the size of collection
by removing one element from end of collection or n
elements.
Example:
subs.TRIM(3);
J) DELETE:- This method deletes the collection elements. It
either deletes all elements, nth element from an array, all
elements in the range m, n.
Example:
subs.DELETE(2);
MissionMCA.com
169
Example on collection methods:
1 DECLARE
2 TYPE my_office IS TABLE OF VARCHAR2 (100);
3
4 office_staff
names_t := names_t ( );
5 clerk
names_t := names_t ( );
6 manager
names_t := names_t ( );
7 BEGIN
8 office_staff.EXTEND (4);
9 office_staff (1) := Takshak;
10 office_staff (2) := Viraj;
11 office_staff (3) := Sonal;
12 office_staff (4) := pooja;
13
14 clerk.EXTEND;
15 clerk(clerk.LAST) := Sonal;
16 clerk.EXTEND;
17 clerk(clerk.LAST) := Viraj;
18
19 manager: = office_staff MULTISET EXCEPT clerk;
20
21 FOR l_row IN 1 .. manager.COUNT
22 LOOP
23
DBMS_OUTPUT.put_line (manager (l_row));
24 END LOOP;
25 END;
MissionMCA.com
170
SQL> INSERT INTO EMP VALUES (749, 'Tushar', 'SALESMAN', 769,
TO_DATE('20-FEB-2001', 'DD-MON-YYYY'), 1600, 300, 30);
1 row created.
SQL> INSERT INTO EMP VALUES (752, 'Priya', 'SALESMAN', 769,
TO_DATE('22-FEB-2000', 'DD-MON-YYYY'), 1250, 500, 30);
1 row created.
SQL> INSERT INTO EMP VALUES (756, 'Tanu', 'MANAGER', 783,
TO_DATE('2-APR-2001', 'DD-MON-YYYY'), 2975, NULL, 20);
1 row created.
SQL> INSERT INTO EMP VALUES (765, 'Sara', 'SALESMAN',
769,TO_DATE('28-SEP-2001', 'DD-MON-YYYY'), 1250, 1400, 30);
1 row created.
SQL> INSERT INTO EMP VALUES (769, 'Sana', 'MANAGER',
783,TO_DATE('1-MAY-2001', 'DD-MON-YYYY'), 2850, NULL, 30);
1 row created.
SQL> INSERT INTO EMP VALUES (778, 'Tejas', 'MANAGER',
783,TO_DATE('9-JUN-2001','DD-MON-YYYY'), 2450, NULL, 10);
1 row created.
SQL> INSERT INTO EMP VALUES (778, 'Akhi', 'ANALYST',
756,TO_DATE('09-DEC-2002','DD-MON-YYYY'), 3000, NULL, 20);
1 row created.
SQL> INSERT INTO EMP VALUES (783, 'Amol', 'PRESIDENT',
NULL,TO_DATE('17-NOV-2001', 'DD-MON-YYYY'), 5000, NULL, 10);
1 row created.
SQL> INSERT INTO EMP VALUES (784, 'Sandip', 'SALESMAN',
769,TO_DATE('8-SEP-2001', 'DD-MON-YYYY'), 1500, 0, 30);
1 row created.
SQL> DECLARE
2
CURSOR all_emps IS
3
SELECT *
4
FROM emp
5
ORDER BY ename;
6
7
TYPE emp_table IS TABLE OF emp%ROWTYPE
8
INDEX BY BINARY_INTEGER;
9
emps emp_table;
10
emps_max BINARY_INTEGER;
11 BEGIN
12
emps_max := 0;
MissionMCA.com
171
13
FOR emp IN all_emps LOOP
14
emps_max := emps_max + 1;
15
emps(emps_max).empno := emp.empno;
16
emps(emps_max).ename := emp.ename;
17
emps(emps_max).JOB
:= emp.JOB;
18
emps(emps_max).HIREDATE := emp.HIREDATE;
19
emps(emps_max).DEPTNO := emp.DEPTNO;
20
END LOOP;
21 END;
22 /
PL/SQL procedure successfully completed.
MissionMCA.com
172
type and also, when the column data type changes in the table, we
do not come to procedure and change the data type.
Syntax :
record_name record_type_name;
Example :
SQL> DECLARE
2 v_name student.name%TYPE;
3 BEGIN
4 SELECT name INTO v_name FROM student WHERE
ROWNUM = 1;
5 DBMS_OUTPUT.PUT_LINE('Name = ' || v_name);
6 END;
7 /
Name = abc
PL/SQL procedure successfully completed.
%ROWTYPE
The %ROWTYPE attribute is used to declare a record with
the same types as found in the specified database table, view or
cursor. % ROWTYPE provides the record type that represents a
entire row of a table or view or columns selected in the cursor. We
normally use %ROWTYPE to retrieve the record which contains all
of the columns from a specified database table.
Syntax :
record_name table_name%ROWTYPE;
Example :
SQL> DECLARE
2 v_stud student%ROWTYPE;
3 BEGIN
4
v_stud.rollno := 6;
5
v_stud.name := 'xyz';
6
v_stud.mark1 := 35;
7
v_stud.mark2 := 56;
8
v_stud.mark3 := 67;
9 DBMS_OUTPUT.PUT_LINE('rollno : '||v_stud.rollno);
10 DBMS_OUTPUT.PUT_LINE('name : '||v_stud.name);
11 DBMS_OUTPUT.PUT_LINE('mark1 : '||v_stud.mark1);
12 DBMS_OUTPUT.PUT_LINE( 'mark2 : '||v_stud.mark2);
13 DBMS_OUTPUT.PUT_LINE( 'mark3 : '||v_stud.mark3);
14 END;
15 /
rollno : 6
name : xyz
mark1 : 35
MissionMCA.com
173
mark2 : 56
mark3 : 67
PL/SQL procedure successfully completed.
C) Using of %type :
create table student(
2 rollno number(4) primary key, name varchar(4),
3 mark1 number(4),
4 mark2 number(4),
5 mark3 number(4)
6 );
SQL> declare
2
v_rollno student.rollno%type := &rollno;
3
v_name student.name%type := '&name';
4
v_mark1 student.mark1%type := &mark1;
5
v_mark2 student.mark2%type := &mark2;
6
v_mark3 student.mark3%type := &mark3;
7 begin
8
insert into student(rollno , name,mark1 , mark2 , mark3)
9
values(v_rollno, v_name, v_mark1 , v_mark2 , v_mark3);
10
dbms_output.put_Line('Inserted Successfully');
11 end;
12 /
Enter value for rollno: 4
old 2:
v_rollno student.rollno%type := &rollno;
new 2:
v_rollno student.rollno%type := 4;
Enter value for name: Sana
old 3:
v_name student.name%type := '&name';
new 3:
v_name student.name%type := 'Sana';
Enter value for mark1: 24
old 4:
v_mark1 student.mark1%type := &mark1;
new 4:
v_mark1 student.mark1%type := 24;
Enter value for mark2: 25
old 5:
v_mark2 student.mark2%type := &mark2;
new 5:
v_mark2 student.mark2%type := 25;
Enter value for mark3: 45
old 6:
v_mark3 student.mark3%type := &mark3;
new 6:
v_mark3 student.mark3%type := 45;
Inserted Successfully
MissionMCA.com
174
SQL> CREATE TABLE I1( a INTEGER, b INTEGER);
Table created.
SQL> INSERT INTO I1 VALUES(1, 3);
1 row created.
SQL> INSERT INTO I1 VALUES(2, 4);
1 row created.
SQL> DECLARE
2
x I1.a%TYPE:=64;
3
y I1.b%TYPE:=54;
4 BEGIN
5 INSERT INTO I1 VALUES(x,y);
6 END;
7 /
PL/SQL procedure successfully completed.
SQL> SELECT * FROM I1;
A
B
---------- ---------1
3
2
4
64
54
SQL> DECLARE
2
x I1.a%TYPE:=64;
3
y I1.b%TYPE:=54;
4 BEGIN
5 UPDATE I1 SET a = x WHERE b < y;
7 END;
8 /
PL/SQL procedure successfully completed.
SQL> SELECT * FROM I1;
A
B
---------- ---------64
3
64
4
64
54
SQL> DECLARE
2
x I1.a%TYPE:=64;
3
y I1.b%TYPE:=54;
4 BEGIN
5 DELETE FROM I1 WHERE a = x;
6 END;
7 /
MissionMCA.com
175
PL/SQL procedure successfully completed.
SQL> SELECT * FROM I1;
no rows selected
10.9: CURSOR
A cursor in PL/SQL means a specific private SQL area
where information for the specific statement is kept for processing.
PL/SQL uses both implicit and explicit cursors. PL/SQL implicitly
declares a cursor for all SQL data manipulation statements on a set
of rows, including queries that return only one row. For queries that
return more than one row, we can explicitly declare a cursor to
process the rows individually.
In the simplest form, a cursor is defined as the pointer into a
table in the database. This simplifies the task of finding proper
values among the huge set of database tables.
In PL/SQL block , SELECT statement cannot return more
than one row at a time. So Cursor use to some group of rows(more
than one row) for implementing certain logic to all the records of
group are show.
MissionMCA.com
176
Implicit cursor variables
Cursor
Attribute
%ISOPEN
%FOUND
Cursor Variable
Description
SQL%ISOPEN
The
Oracle
engine
automatically opens the
cursor
If cursor open return true
otherwise return false.
SQL%FOUND
UPDATE student
IF SQL%NOTFOUND THEN
6
7
8
9
dbms_output.put_line('Marks for ' || var_rows || 'students are
updated');
10
END IF;
11 END;
12 /
177
Marks for 6 students are updated
PL/SQL procedure successfully completed.
Drawbacks of Implicit Cursors
When we use implicit cursor, if our query returns only a single row,
we can still decide to use an explicit cursor. The implicit cursor has
the following drawbacks:
Implicit cursors are less efficient than an explicit cursor.
Implicit cursors are more vulnerable to data errors.
Implicit cursors gives us less programmatic control.
B) Explicit Cursor:
The Cursors which are declared by user are called Explicit Cursor.
The user has to declare the cursor, open cursor to reserve the
memory are and populate data, fetch the records from the active
data set one at a time, apply logic and last close the cursor.
Cursor Declaration :
CURSOR c_student IS SELECT
RollNo,mark1,mark2,mark3 FROM student;
In above syntax we have created a cursor with name c_student
which is associated with student table.
Once we declared the cursor, we can open it as:
OPEN c_student;
Then we can fetch rows from it as:
FETCH c_student INTO
var_rollno,var_mark1,var_mark2,var_mark3 ;
After finishing the use of cursor we can close it as:
CLOSE c_student;
MissionMCA.com
178
Explicit cursor variables
Cursor
Attribute
Cursor
Variable
Description
179
1 row created.
SQL> select * from student;
ROLLNO NAME MARK1 MARK2 MARK3
-- -------- ---- ---------- ---------- ---------1 Hitesh
56
78
89
2 Suresh
55
44
66
3 Kamal
56
78
89
4 Varun
55
44
66
5 Anu
56
78
89
6 Dinu
55
44
66
6 rows selected.
SQL> create table student_performance
2 (
3 Rollno number(4),
4 Total number(4),
5 Average number(4),
6 Grade varchar(15)
7 );
Table created.
SQL> Declare
var_total number(4);
var_average number(4);
var_grade varchar(15);
var_rollno number(4);
var_mark1 number(4);
var_mark2 number(4);
var_mark3 number(4);
cursor c_student is
select RollNo,mark1,mark2,mark3 from student;
begin
open c_student;
loop
fetch c_student into var_rollno,var_mark1,var_mark2,var_mark3 ;
exit when c_student%notfound;
var_total:=var_mark1+var_mark2+var_mark3;
var_average:=var_total/3;
if var_average<35 then
var_grade:='fail';
else
MissionMCA.com
180
var_grade:='pass';
end if;
insert into student_performance
values(var_rollno,var_total,var_average,var_grade);
end loop;
close c_student;
end;
/
PL/SQL procedure successfully completed.
SQL> select * from student_performance;
ROLLNO TOTAL AVERAGE GRADE
---------- ---------- ---------- --------------1
223
74 pass
2
165
55 pass
3
223
74 pass
4
165
55 pass
5
223
74 pass
6
165
55 pass
6 rows selected.
Example
SQL> declare
2 cursor s_stud (rno NUMBER) is
3
select * from student where rollno = rno;
4 begin
5
for r_stud in s_stud(5) loop
6
update student
7
set mark2=mark2+5
8
where rollno = r_stud.rollno;
MissionMCA.com
181
9
A) %NOTFOUND
The %NOTFOUND attribute works opposite of %FOUND. It
returns TRUE if the cursor is unable to fetch another row because
the last row was fetched. If the cursor is unable to return a row
because of an error, the appropriate exception is raised. If the
cursor has not yet been opened, a reference to the %NOTFOUND
attribute raises the INVALID_CURSOR exception. We can evaluate
the %NOTFOUND attribute of any open cursor, because we
reference the cursor by name.
B) %ROWCOUNT
The %ROWCOUNT is a cursor attribute. The value of
%ROWCOUNT is set after the FETCH command is executed or an
INSERT, UPDATE or DELETE implicit cursor is used. The
%ROWCOUNT attribute (for EXPLICIT and IMPLICIT cursors) does not
return the total number of rows for a query prior to the first fetch. It does
return:
The number of rows fetched so far following a fetch.
The number of rows affected by a INSERT, UPDATE, and
DELETE.
The attribute can be used in procedural statements but not in SQL
statements.
Statement
%ROWCOUNT value
FETCH
SELECT INTO
UPDATE
DELETE
INSERT
MissionMCA.com
182
Example :
SQL> DECLARE
2 TYPE item_record IS RECORD
3 ( id NUMBER, title VARCHAR2(60));
4 item ITEM_RECORD;
5 CURSOR c IS
6 SELECT rollno, name
7 FROM student
8 WHERE rollno = 1;
9 BEGIN
10 OPEN c;
11 LOOP
12 FETCH c INTO item;
13 IF c%NOTFOUND THEN
14 IF c%ROWCOUNT = 0 THEN
15 dbms_output.put_line('No Data Found');
16 END IF;
17 EXIT;
18 ELSE
19 dbms_output.put_line('Name of student :'||item.title);
20 END IF;
21 END LOOP;
22 END;
23 /
Name of student :abc
PL/SQL procedure successfully completed.
MissionMCA.com
183
Syntax
FOR
{
READ ONLY | FETCH ONLY |
UPDATE [ OF Simple-column-Name [ , Simple-columnName]* ]
}
Simple-column-Name refers to the names visible for the table
specified in the FROM clause of the underlying query.
Note: The use of the FOR UPDATE clause is not mandatory to
obtain an updatable JDBC ResultSet. As long as the statement
used to generate the JDBC ResultSet meets the requirements for
updatable cursor, it is sufficient for the JDBC Statement that
generates the JDBC ResultSet to have concurrency mode
ResultSet.CONCUR_UPDATABLE for the ResultSet to be
updatable.
The optimizer is able to use an index even if the column in the
index is being updated.
Example :
SQL> create table ftbl (a number, b varchar2(10));
Table created.
SQL> insert into ftbl values (5,'five');
1 row created.
SQL> insert into ftbl values (6,'six');
1 row created.
SQL> insert into ftbl values (7,'seven');
1 row created.
SQL> insert into ftbl values (8,'eight');
1 row created.
SQL> insert into ftbl values (9,'nine');
1 row created.
SQL> create or replace procedure pincr as
2 cursor c_ftbl is
3
select a,b from f where length(b) = 5 for update;
4
v_a ftbl.a%type;
5
v_b ftbl.b%type;
6 begin
7 open c_ftbl;
MissionMCA.com
184
8
9
10
11
12
13
14
15
loop
fetch c_ftbl into v_a, v_b;
exit when c_ftbl%notfound;
update ftbl set a=v_a+5 where current of c_ftbl;
end loop;
close c_ftbl;
end;
/
Procedure created.
SQL> exec pincr;
PL/SQL procedure successfully completed.
B) WHERE CURRENT Clause:
The WHERE CURRENT OF clause is a clause in some
UPDATE and DELETE statements. It allows we to perform
positioned updates and deletes on updatable cursors. For more
information about updatable cursors, see SELECT statement
Syntax
WHERE CURRENT OF cursor-Name
Example
SQL> create table testc ( n number(3), c varchar(50));
Table created.
SQL> insert into testc values (1, 'one');
1 row created.
SQL> insert into testc values (10, 'ten');
1 row created.
SQL> insert into testc values (15, 'one five');
1 row created.
SQL> insert into testc values (99, 'nine nine');
1 row created.
SQL> insert into testc values (42, 'four two');
1 row created.
SQL> declare
2 cursor cur_test is
3
select n, c from testc for update;
4 n number(3);
5 c varchar(50);
6 begin
7 open cur_test;
8 loop
MissionMCA.com
185
9
fetch cur_test into n, c;
10
exit when cur_test%notfound;
11
if n>12 then
12
update testc set n=n*2, c=upper(c) where current of
cur_test;
13
end if;
14 end loop;
15 end;
16 /
PL/SQL procedure successfully completed.
10.14 QUESTIONS
1
MissionMCA.com
186
Fifth
Edition
By
Steven
MissionMCA.com
187
11
EXCEPTIONS HANDLING
Unit Structure
11.1 Objective
11.2 Introduction to Exception
11.3 Coding Structure of Exception Handling.
11.4 Rules for PL/SQL Exceptions
11.5 PRAGMA EXCEPTION_INIT
11.6 Classification of PLSQL Exception
A) The Named System Exceptions
B) The Unnamed System Exceptions
C) The Named Programmer-defined Exceptions
1.7 WHEN OTHERS clause
1.8 The SQLCODE Function
1.9 The SQLERRM Function
1.10 The RAISE_APPLICATION_ERROR ( )
1.11 Questions
1.12 Further Reading
11.1 OBJECTIVE
After completing this chapter, you will be able to:
Understand the Errors and Handling Errors in PLSQL
Understand the Structure of Exception Handling
Implement the different types of Exception Handling Code
Understand the PLSQL Functions like SQLCODE and
SQLEERM
Using the RAISE_APPLICATION_ERROR ( )
MissionMCA.com
188
MissionMCA.com
189
BEGIN
SELECT ...
SELECT ...
SELECT ...
...
EXCEPTION
WHEN NAME_NOT_FOUND THEN -- catches all ' name not
found errors
The Exceptions brings readability in code by separate ErrorHandling blocks.The Exceptions also improve reliability. We
need not worry about checking for an error at every point it
might occur. We just have to add an Exception Handler to
our PL/SQL block. If the exception is ever raised in that
block (or any sub-block), we can make sure it will be
handled.
MissionMCA.com
190
We can use normal, general PL/SQL statements in the
Exception block. When an Exception is raised, Oracle searches for
an appropriate exception handler in the exception section. For
example in the above example, if the error raised is
'First_Exception ', then the error is handled according to the
statements under it. Since, We cannot assume all the errors at
once while coding and it is not possible to determine all the
possible runtime errors during testing our code, the 'WHEN Others'
exception is used to manage the exceptions that are not explicitly
handled. Only one exception can be raised in a Block and the
control does not return to the execution section after the error is
handled.
If there exists, nested PL/SQL blocks as given in the following:
DELCARE
Declaration section // Here we can declare any PLSQL
variables etc
BEGIN
DECLARE
Declaration section // Here we can declare any PLSQL
variables etc
BEGIN
Execution section // This is the section which we have
to monitor for outer errors
EXCEPTION
Exception section // Here we have to write the PLSQL
inner Exception Handling code
END;
EXCEPTION
Exception section // Here we have to write the PLSQL
inner Exception Handling code
END;
/
MissionMCA.com
191
INSERT INTO errlog VALUES (v_err, v_msg, v_prog,
SYSDATE, USER);
WHEN OTHERS THEN
v_err := SQLCODE;
v_msg := SQLERRM;
v_prog := 'fixdebt';
INSERT INTO errlog VALUES (v_err, v_msg, v_prog,
SYSDATE, USER);
RAISE;
MissionMCA.com
192
The enclosing block does not handle the raised exception
because the declaration of balance in the sub-block prevails.
Though they share the same name, the two balance
exceptions are different, just as the two customer_id variables
share the same name but they are different variables. Therefore,
the RAISE statement and the WHEN clause refer to different
exceptions. To have the enclosing block handle the raised
exception, we must remove its declaration from the sub-block or
define an OTHERS handler.
MissionMCA.com
193
Oracle Error
Explanation
DUP_VAL_ON_INDEX
ORA-00001
We tried to execute an
INSERT
or
UPDATE
statement that has created
a duplicate value in a field
restricted by a unique
index.
TIMEOUT_ON_RESOURCE
ORA-00051
TRANSACTION_BACKED_OUT ORA-00061
INVALID_CURSOR
ORA-01001
We tried to reference a
cursor that does not yet
exist. This may have
happened because we've
executed a FETCH cursor
or CLOSE cursor before
OPENing the cursor.
NOT_LOGGED_ON
ORA-01012
ORA-01017
LOGIN_DENIED
MissionMCA.com
194
ORA-01403
TOO_MANY_ROWS
ORA-01422
We tried to execute a
SELECT INTO statement
and more than one row
was returned.
ZERO_DIVIDE
ORA-01476
We tried to divide
number by zero.
INVALID_NUMBER
ORA-01722
We tried to execute an
SQL statement that tried to
convert a string to a
number,
but
it
was
unsuccessful.
STORAGE_ERROR
ORA-06500
ORA-06501
VALUE_ERROR
ORA-06502
We tried to perform an
operation and there was a
error on a conversion,
truncation,
or
invalid
constraining of numeric or
character data.
CURSOR_ALREADY_OPEN
ORA-06511
NO_DATA_FOUND
PROGRAM_ERROR
MissionMCA.com
195
EXCEPTION
WHEN exception_name1 THEN
[statements] // Exception handling code
WHEN exception_name2 THEN
[statements] // Exception handling code
WHEN exception_name_n THEN
[statements] // Exception handling code
WHEN OTHERS THEN
[statements] // Exception handling code
END [procedure_name];
/
The syntax for the Named System exception in a function is:
CREATE [OR REPLACE] FUNCTION function_name
[ (param1 [,param2]) ]
RETURN return_datatype
IS | AS
[declaration_section]
BEGIN
executable_section // The code of this section will be
monitored for errors
EXCEPTION
WHEN exception_name1 THEN
[statements] // Exception handling code
WHEN exception_name2 THEN
[statements] // Exception handling code
WHEN exception_name_n THEN
[statements] // Exception handling code
WHEN OTHERS THEN
[statements] // Exception handling code
END [function_name];
/
MissionMCA.com
196
dbms_output.put_line ('Sorry the given statement
dose not return any row ');
END;
/
Here is an example of a procedure that uses a Named System
Exception:
Example:
CREATE OR REPLACE PROCEDURE new_customer (cust_id IN
NUMBER, cust_name IN VARCHAR2)
IS
BEGIN
INSERT INTO customer (cid,csname ) VALUES ( cust_id,
cust_name );
EXCEPTION
WHEN DUP_VAL_ON_INDEX THEN
raise_application_error (-20001,'We have tried to insert a
duplicate customer' id);
WHEN OTHERS THEN
raise_application_error (-20002,'An error has occurred
inserting a customer.');
END;
/
In this example, we are trying to trap the Named System Exception
called DUP_VAL_ON_INDEX.
Example: NO_DATA_FOUND EXCEPTION
SQL> DECLARE
2 uid all_users.username%TYPE := 10;
3 uname all_users.username%TYPE;
4 BEGIN
5 SELECT username
6 INTO uname
7 FROM all_users
8 WHERE user_id = uid;
9
10 DBMS_OUTPUT.put_line('uname=' || uname);
11 EXCEPTION
12 WHEN NO_DATA_FOUND THEN
13
DBMS_OUTPUT.put_line('No users have a user_id=' || uid);
14 END;
15 /
MissionMCA.com
197
No users have a user_id=10
PL/SQL procedure successfully completed.
Example: ZERO_DIVIDE EXCEPTION
SQL> declare
2 n number;
3 begin
4 n:=10/0;
5 exception
6 when ZERO_DIVIDE then
7 dbms_output.put_line('zero divide error');
8 end;
9 /
zero divide error
PL/SQL procedure successfully completed.
MissionMCA.com
198
Syntax:
DECLARE
exception_name EXCEPTION;
PRAGMA
EXCEPTION_INIT (exception_name, Err_code);
BEGIN
Execution section
EXCEPTION
WHEN exception_name THEN
handle the exception
END;
/
Consider the customer and the order table from sql joins.
Here cid is a primary key in customer table and a foreign key in
order table. If we try to delete cid from the product table while it has
child records in oid table an exception will be thrown with oracle
code number -1517. We can provide a name to this exception and
handle it in the exception section as given below.
DECLARE
Child_record_exception EXCEPTION;
PRAGMA
EXCEPTION_INIT (Child_record_exception, -1517);
BEGIN
Delete FROM customer where cid= 345;
EXCEPTION
WHEN Child_record_exception
THEN dbms_output.put_line('Child records are present for
this product_id.');
END;
/
MissionMCA.com
199
The syntax for the Named Programmer-Defined Exception in a
procedure is:
CREATE [OR REPLACE] PROCEDURE procedure_name
[ (param1 [,param2]) ]
IS
[declaration_section]
exception_name EXCEPTION; // programmer defined
exception
BEGIN
executable_section
RAISE exception_name ; // rasing the programmer
defined exception
EXCEPTION
WHEN exception_name THEN
[statements]
// handling the programmer defined
exception
WHEN OTHERS THEN
[statements]
END [procedure_name];
/
The syntax for the Named programmer-defined exception in a
function is:
CREATE [OR REPLACE] FUNCTION function_name
[ (param1 [,param2]) ]
RETURN return_datatype
IS | AS
[declaration_section]
exception_name EXCEPTION;
BEGIN
executable_section
RAISE exception_name ;
EXCEPTION
WHEN exception_name THEN
[statements]
WHEN OTHERS THEN
[statements]
END [function_name];
/
Here is an example of a procedure that uses a Named
Programmer-Defined Exception:
MissionMCA.com
200
Example:
CREATE OR REPLACE PROCEDURE new_customer(cut_id IN
NUMBER, CP_in IN VARCHAR)
IS
no_customer EXCEPTION;
BEGIN
IF CP_in = 'NULL' THEN
RAISE no_customer;
ELSE
INSERT INTO customerer (cid, CP )VALUES (cust_id, CP_in );
END IF;
EXCEPTION
WHEN no_customer THEN
raise_application_error (-20001,'We must have customer
Product in order to submit customer.');
WHEN OTHERS THEN
raise_application_error (-20002,'An error has occurred
inserting an Supplier.');
END;
/
In this example, we have declared a Named programmerdefined Exception called no_customer in our declaration
statement with the following code:
no_ customer EXCEPTION;
We've then raised the exception in the executable section of the
code:
IF CP_in = NULL THEN
RAISE no_ customer;
Now if the CP_in variable contains a NULL, our code will jump
directly to the Named programmer-defined exception called no_
customer.
Finally, we can tell the procedure what to do when the no_
customer exception is encountered by including code in the WHEN
clause:
WHEN no_ customer THEN
raise_application_error (-20001,'We must have Customer
Product in order to submit customer.');
MissionMCA.com
201
MissionMCA.com
202
WHEN OTHERS THEN
[statements]
// handling the all uncaught exception
END [function_name];
/
Here is an example of a procedure that uses a WHEN OTHERS
clause:
CREATE OR REPLACE PROCEDURE new_customer(cust_id IN
NUMBER, CP_in IN VARCHAR)
IS
no_cust EXCEPTION;
BEGIN
IF CP_in = 'NULL' THEN
RAISE no_cust;
ELSE
INSERT INTO customer(cid, cp ) VALUES ( cust_id, CP_in );
END IF;
EXCEPTION
WHEN DUP_VAL_ON_INDEX THEN
raise_application_error (-20001,'We have tried to insert a
duplicate cid.');
WHEN no_cust THEN
raise_application_error (-20001,'We must have CP in order to
submit the customer.');
WHEN OTHERS THEN
raise_application_error (-20002,'An error has occurred
inserting an customer.');
END;
/
In this example, if an exception is encountered that is not a
DUP_VAL_ON_INDEX or a no_cust, it will be trapped by the
WHEN OTHERS clause.
MissionMCA.com
203
EXCEPTION
WHEN exception_name1 THEN
[statements]
WHEN exception_name2 THEN
[statements]
WHEN exception_name_n THEN
[statements]
WHEN OTHERS THEN
[statements] // Here we can use the SQLCODE
function
END [procedure_name];
/
We could use the SQLCODE function to raise an error as follows:
Example :
EXCEPTION
WHEN OTHERS THEN
raise_application_error(-20001,'An error was encountered '||SQLCODE||' -ERROR- '||SQLERRM);
END;
/
Or we could log the error to a table as follows:
Example:
EXCEPTION
WHEN OTHERS THEN
err_code := SQLCODE;
err_msg := substr(SQLERRM, 1, 200);
INSERT INTO audit_table (error_number,
error_message)
VALUES (err_code, err_msg);
END;
/
MissionMCA.com
204
WHEN exception_name2 THEN
[statements]
WHEN exception_name_n THEN
[statements]
WHEN OTHERS THEN
[statements] // Here we can use the SQLERRM
function
END [procedure_name];
/
We could use the SQLERRM function to raise an error as follows:
Example:
EXCEPTION
WHEN OTHERS THEN
raise_application_error(-20001,'An error was encountered '||SQLCODE||' -ERROR- '||SQLERRM);
END;
/
Or we could log the error to a table as follows:
Example:
EXCEPTION
WHEN OTHERS THEN
err_code := SQLCODE;
err_msg := substr(SQLERRM, 1, 200);
INSERT INTO audit_table (error_number,
error_message)
VALUES (err_code, err_msg);
END;
/
MissionMCA.com
205
RAISE_APPLICATION_ERROR is used for the following reasons,
a) Used to create a unique id to the user-defined exception.
b) Used to make the user-defined exception look like an Oracle
error.
The General Syntax to use this procedure is:
RAISE_APPLICATION_ERROR (error_number, error_message);
MissionMCA.com
206
WHEN large_quantity THEN
raise_application_error(-2100, 'The number of unit is
above the discount limit.');
END;
/
11.11 QUESTIONS
1. Explain Exception. Explain the syntax of exception handling in
PL/SQL.
2. Explain the parts of exception message in PL/SQL.
3. Write short note on scope rules of PL/SQL exception.
4. List and explain types of PL/SQL exception in short.
5. List and explain NAMED System exceptions.
6. What UNNAMED exception? Explain with examples.
7. Explain in detail programmer defined exception with examples.
8. Explain use of WHEN OTHER clause with example.
9. Write short note on SQLCODE and SQLERRM functions.
10. Explain in detail use of RAISE_APPLICATION_ERROR ( ).
Practice Questions:
11. Write a PL/SQL block of code which shows the use of exception
handling.
12. Write a PL/SQL block of code which shows the use of user
define exception.
MissionMCA.com
207
UNIT - V
12
STORED PROCEDURE
Unit Structure
12.1
Objectives
12.1 OBJECTIVES:
After completing this chapter, you will be able to:
Create a Modularized and Layered Subprogram
Understand the advantages of modular program constructs
Understand the Creation and use of Stored Procedure
Understand the Functions and its parameters
Understand the Difference between Procedures & Functions
Utilizing the stored procedures in critical PLSQL queries
MissionMCA.com
208
12.3 THE
BENEFITS
OF
PROGRAM CONSTRUCTS:
USING
MODULAR
MissionMCA.com
209
The PLSQL modular programs allow us to extend the
PL/SQL language. Procedures act like new statements. Functions
act like new expressions and operators. Subprogram allows us to
break a program down into manageable, well-defined modules. We
can use stepwise refinement approach to problem solving.
Model subprograms allow us to distinguish the definition of
procedures and functions unless the main program is tested. You
can design applications with the top down approach without
worrying about implementation details.
When we use PL/SQL subprograms to define an API, we
can make our code even more reusable and maintainable by
grouping the subprograms into a PL/SQL package.
We can summarize the benefits of PLSQL modular program
construct as follows:
1. Easy to maintain: Because the code is well arranged so it very
easy to maintain for the developers.
2. Better data security and integrity: The code is separated in
layered paradigm so that it helps to improve the security and the
data integrity.
3. Better performance: Due to separation and sequential
arrangement the code suppose to give improved performance.
4. Better code clarity: The code is layered and every layer
contains the self explanatory code which improves the code
readability and clarity.
MissionMCA.com
210
12.5.1 Stored
Functions)
Program
Units
(The
Procedures
&
The
MissionMCA.com
211
CREATE PROCEDURE :
The general format of a create procedure statement is :
CREATE [OR REPLACE] PROCEDURE procedure_name
[ (param1 [,param2]) ]
IS
[declaration_section] // Application variables
BEGIN
[executable_section] // Application Logic
EXCEPTION
[ exception_section] // Exception handling statements
END [procedure_name];
/
The following is a simple example of a procedure:
Example 1: (Simple procedure without parameter)
SQL> set serveroutput on
SQL> CREATE OR REPLACE PROCEDURE DEMO AS
2 BEGIN
3 DBMS_OUTPUT.PUT_LINE('HELLO WORLD');
4 END;
5 /
Procedure created.
Example 2 : (Simple procedure with parameters)
Note:(This procedure can be called Using PLSQL block as shown
below point 3rd of CALL procedure section.)
SQL> CREATE OR REPLACE PROCEDURE Square(sq_num INT,
sq OUT INT) AS
2 BEGIN
3 sq:= sq_num*sq_num;
4 DBMS_OUTPUT.PUT_LINE('Square of entered number is ' ||
sq );
5 END;
6 /
Procedure created.
CALL PROCEDURE :
You can call procedure in three ways1.Using EXECUTE
Example 1 Executl :
SQL> EXECUTE DEMO
HELLO WORLD
PL/SQL procedure successfully completed.
MissionMCA.com
212
2.Using CALL
Example 1 Call:
SQL> call HELLO();
Hello World
Call completed.
3.Using PL/SQL block
Example 1 call :
SQL> begin
2 HELLO();
3 end;
4 /
Hello World
PL/SQL procedure successfully completed.
Example 2 call :
SQL> Declare
2 my_num int;
3 Begin
4 SQUARE(4,my_num);
5 END;
6 /
Square of entered number is 16
PL/SQL procedure successfully completed.
OUT
It is the default.
It
must
specified.
IN OUT
be It must be specified.
MissionMCA.com
213
Formal
parameter Formal parameter Formal parameter acts
acts like a constant.
acts
like
an like
an
initialized
uninitialized
variable.
variable.
Formal
parameter Formal parameter Formal
parameter
cannot be assigned a cannot be used in should be assigned a
value.
an
expression; value.
must be assigned
a value.
Actual parameter can Actual parameter Actual parameter must
be
a
constant, must
be
a be a variable.
initialized
variable, variable.
literal, or expression.
Example 1:
12.6.1 IN and OUT MODE
SQL> CREATE OR REPLACE
2 PROCEDURE SUM_AB (A IN INT, B IN INT, C OUT INT) IS
3 BEGIN
4 C := A + B;
5 END;
6 /
Procedure created.
SQL> DECLARE
2 R INT;
3 BEGIN
4 SUM_AB(23,29,R);
5 DBMS_OUTPUT.PUT_LINE('SUM IS: ' || R);
6 END;
7 /
SUM IS: 52
PL/SQL procedure successfully completed.
Example 2:
SQL> CREATE OR REPLACE PROCEDURE CONCAT
( subchars1 IN STRING, subchars2 IN STRING, ConcChar OUT
STRING ) IS
MissionMCA.com
214
2 BEGIN
3 ConcChar:= subchars1 + subchars2;
4 DBMS_OUTPUT.PUT_LINE('After Concatination of characters word
is ' || ConcChar );
5 END;
6 /
Procedure created.
SQL> DECLARE
2 my_char STRING(10);
3 BEGIN
4 CONCAT('SONA','LI', my_char);
5 END;
6 /
After Concatination of characters word is SONALI
PL/SQL procedure successfully completed.
12.6.2 IN OUT MODE
Example 1 :
SQL> set serveroutput on
SQL> CREATE OR REPLACE
2 PROCEDURE DOUBLEN (N IN OUT INT) IS
3 BEGIN
4 N := N * 2;
5 END;
6 /
Procedure created.
SQL> DECLARE
2 R INT;
3 BEGIN
4 R := 7;
5 DBMS_OUTPUT.PUT_LINE('BEFORE CALL R IS: ' || R);
6 DOUBLEN(R);
7 DBMS_OUTPUT.PUT_LINE('AFTER CALL R IS: ' || R);
8 END;
9 /
BEFORE CALL R IS: 7
AFTER CALL R IS: 14
PL/SQL procedure successfully completed.
MissionMCA.com
215
Example 2 :
SQL> CREATE OR REPLACE PROCEDURE DIVISION
(my_num1 int,my_num2 IN OUT INT) IS
2 BEGIN
3 my_num2 := my_num1 / my_num2;
4 END;
5 /
Procedure created.
SQL> DECLARE
2 num1 INT;
3 num2 INT;
4 BEGIN
5 num1 := 180;
6 num2 := 18;
7 DBMS_OUTPUT.PUT_LINE('BEFORE IN OUT DIVISION NUMBER
IS: ' || num2);
8 DIVISION(num1,num2);
9
DBMS_OUTPUT.PUT_LINE('AFTER CALL to DIVSION IN OUT
NUMBER IS: ' || num2);
10
11
END;
/
WHERE
DROP PROCEDURE
If were interested in getting rid of a procedure totally, we can
DROP it. The general
format of a DROP is:
DROP PROCEDURE procedure_name;
SQL>DROP PROCEDURE DEMO;
Procedure dropped.
MissionMCA.com
216
MissionMCA.com
217
1 row created.
SQL> select * from student;
ROLLNO NAME
MARK1
------------- ---------1
abc
56
2
pqr
55
3
xyz
56
4
qwe
55
5
iop
56
6
tgb
55
MARK2
MARK3
---------- ---------78
89
44
66
78
89
44
66
78
89
44
66
===========================================
CREATE OR REPLACE PROCEDURE displayinfo(
p_rollno IN student.rollno%TYPE,
o_name OUT student.name%TYPE,
o_mark1 OUT student.mark1%TYPE,
o_mark2 OUT student.mark2%TYPE,
o_mark3 OUT student.mark3%TYPE)
IS
BEGIN
SELECT name,mark1,mark2,mark3
INTO o_name,o_mark1,o_mark2 ,o_mark3
FROM student WHERE rollno = p_rollno;
END;
/
=============================================
DECLARE
o_name student.name%TYPE;
o_mark1 student.mark1%TYPE;
o_mark2 student.mark2%TYPE;
o_mark3 student.mark3%TYPE;
total NUMERIC;
BEGIN
displayinfo(1,o_name,o_mark1,o_mark2 ,o_mark3);
DBMS_OUTPUT.PUT_LINE('name : ' || o_name);
DBMS_OUTPUT.PUT_LINE('mark1 : ' || o_mark1);
DBMS_OUTPUT.PUT_LINE('mark2 : ' || o_mark2);
DBMS_OUTPUT.PUT_LINE('mark3 : ' || o_mark3);
total:=o_mark1+o_mark2 +o_mark3 ;
DBMS_OUTPUT.PUT_LINE('Total : ' || total);
END;
MissionMCA.com
218
PL/SQL procedure successfully completed.
name : abc
mark1 : 56
mark2 : 78
mark3 : 89
Total : 223
MissionMCA.com
219
5 /
Function created.
Example 2:
SQL> CREATE OR REPLACE FUNCTION STRINGRETURN (
myname STRING) RETURN STRING IS
2 BEGIN
3 RETURN (myname);
4 END;
5 /
Function created.
CALL FUNCTION :
Call for Example 1:
SQL> BEGIN
DBMS_OUTPUT.PUT_LINE ('RESULT IS:' || ADD2 (25,50));
END;
/
RESULT IS:75
PL/SQL procedure successfully completed.
Call for Example 2:
SQL> BEGIN
1 DBMS_OUTPUT.PUT_LINE ('MY NAME IN FUNCTION IS: ' ||
STRING_RETURN('YASHASHREE'));
3 END;
4 /
MY NAME IN FUNCTION IS: YASHASHREE
PL/SQL procedure successfully completed.
MissionMCA.com
220
2. OUT - The parameter cannot be referenced by the procedure or
function, but the value of the parameter can be overwritten by
the procedure or function.
3. IN OUT - The parameter can be referenced by the procedure or
function and the value of the parameter can be overwritten by
the procedure or function.
SOURCE CODE OF FUNCTION
SQL> SELECT TEXT FROM USER_SOURCE
NAME='ADD2';
TEXT
------------------------------------------------------FUNCTION ADD2 (X INT, Y INT) RETURN INT IS
BEGIN
RETURN (X + Y);
END;
WHERE
DROP FUNCTION
If youre interested in getting rid of a function totally, you can DROP
it.
The general format of a DROP is:
DROP FUNCTION function_name;
SQL>DROP FUNCTION ADD2;
Procedure dropped.
LIST OF ALL FUNCTION
SQL> SELECT OBJECT_NAME
2
FROM USER_OBJECTS
3
WHERE OBJECT_TYPE = 'FUNCTION';
OBJECT_NAME
----------------------------------------------DISPLAY1
ADD2
5 rows selected.
MissionMCA.com
221
7 RETURN NUMBER
8 IS total NUMBER;
9 BEGIN
10 SELECT name,mark1,mark2,mark3
11 INTO o_name,o_mark1,o_mark2 ,o_mark3
12 FROM student WHERE rollno = p_rollno;
13 total:=o_mark1+o_mark2 +o_mark3 ;
14 RETURN(total);
15 END;
16 /
Function created.
SQL> DECLARE
2 o_name student.name%TYPE;
3 o_mark1 student.mark1%TYPE;
4 o_mark2 student.mark2%TYPE;
5 o_mark3 student.mark3%TYPE;
6
total NUMERIC;
7 BEGIN
8 total:= display(1,o_name,o_mark1,o_mark2 ,o_mark3);
9 DBMS_OUTPUT.PUT_LINE('name : ' || o_name);
10 DBMS_OUTPUT.PUT_LINE('mark1 : ' || o_mark1);
11 DBMS_OUTPUT.PUT_LINE('mark2 : ' || o_mark2);
12 DBMS_OUTPUT.PUT_LINE('mark3 : ' || o_mark3);
13 DBMS_OUTPUT.PUT_LINE('Total : ' || total);
14 END;
15 /
name : abc
mark1 : 56
mark2 : 78
mark3 : 89
Total : 223
PL/SQL procedure successfully completed.
12.11
DIFFERENCE
FUNCTIONS
BETWEEN PROCEDURES
&
MissionMCA.com
222
organization code for a particular GL account, you would write a
function.
Here are a few more differences between a procedure and a
function:
12.12 QUESTIONS
1. How to create Modularized and Layered Subprogram?
2. What is a Stored Procedure?
3. Explain Difference between anonymous blocks and sub
programs in PL SQL.
4. How to create and call Stored Procedure Explain with help of
Example.
5. Write short note on PROCEDURE PARAMETERS.
6. Give the example for using PARAMETERS in Procedures.
7. What are stored functions? Write its Creation and calling
Example.
8. Write short note on Function PARAMETERS.
9. Give a brief example of Stored Function.
10. Explain the Difference between Procedures & Functions.
MissionMCA.com
223
Oracle PL/SQL Programming,
Feuerstein, Bill Pribyl
Fifth
Edition
By
Steven
MissionMCA.com
224
13
PACKAGE
Unit Structure :
13.1
13.2
13.3
13.4
13.5
13.6
13.7
13.8
13.9
13.10
13.11
13.12
13.12
Objectives
What is Package?
Contents of PL/SQL Package:
Introduction to PL/SQL Package:
Advantages of Package:
Components of Packages
Data Dictionary and PL/SQL Source Code:
Overloading Subprograms in PL/SQL:
The STANDARD Package
Product-Specific Packages:
Points to ponder for Writing Packages:
Questions:
Further reading
13.1 OBJECTIVES
After completing this chapter, you will be able to:
Learn and understand the Complete Structures of Packages
Understand the advantages of Packages
Understand the Components of packages
Overload Subprograms in PL/SQL etc.
MissionMCA.com
225
PL/SQL types, variables, and subprograms. Using packages is an
alternative to creating procedures and functions as standalone
schema objects.
Packages have two parts, a specification and a body;
sometimes the body is unnecessary. The specification is the
interface to the package. It declares the types, variables, constants,
exceptions, cursors, and subprograms that can be referenced from
outside the package. The body defines the queries for the cursors
and the code for the subprograms. We can think of the specification
as an interface and of the body as a black box. We can debug,
enhance, or replace a package body without changing the package
spec (specification). To create package specs, we have to use the
SQL statement CREATE PACKAGE. A CREATE PACKAGE
BODY statement defines the package body. The spec holds public
declarations, which are visible to stored procedures and other code
outside the package. We must declare subprograms at the end of
the spec after all other items.
The body holds implementation details and private
declarations, which are hidden from code outside the package.
Following the declarative part of the package body is the optional
initialization part, which holds statements that initialize package
variables and do any other one-time setup steps. The AUTHID
clause determines whether all the packaged subprograms execute
with the privileges of their definer or invoker, and whether their
unqualified references to schema objects are resolved in the
schema of the definer or invoker.
A call spec lets we map a package subprogram to a Java
method or external C function. The call spec maps the Java or C
name, parameter types, and return type to their SQL counterparts.
MissionMCA.com
226
convenient than standalone stored procedures and functions
when they call back and forth to each other.
PL/SQL Package
MissionMCA.com
227
MissionMCA.com
228
EXCEPTION
<PL/SQL Exception block> // Exception handling code goes
here
END;
FUNCTION [Schema..] <FunctionName>(<argument> IN
<Data Type>,..)
return <Data Type> {IS,AS}
<variable> declarations;
<constant> declarations;
BEGIN
<PL/SQL subprogram body> // Application code goes here
EXCEPTION
<PL/SQL Exception block> // Exception handling code
goes here
END;
END;
/
Example 1 :
Defining Package Specification
SQL> create or replace package pkg_demo
2 as
3
function cArea (r NUMBER) return NUMBER;
4
procedure pPrint (str1 VARCHAR2 :='hello',
5
str2 VARCHAR2 :='world',
6
str3 VARCHAR2 :='!' );
7 end;
8 /
Package created.
Defining Package Body
SQL> create or replace package body pkg_demo
2
as
3
function cArea (r NUMBER)return NUMBER
4
is
5
pi NUMBER:=3.14;
6
begin
7
return (pi * r *r);
8
end;
9
procedure pPrint(str1 VARCHAR2 :='hello',
10
str2 VARCHAR2 :='world',
MissionMCA.com
229
11
12
13
14
15
Empname empage
salary
Job_title
Hiredate
Amol
29
29000
Manager
23/11/1998 1
Suresh
27
26000
Developer 16/10/1999 2
Kapil
28
28000
Designer
05/08/2000 3
Pappu
22
17000
Designer
18/06/2001 4
Prashant
25
18000
Tester
11/10/2002 5
KomaL
23
18500
Executive
23/10/2003 6
16
17
empid
end;
/
MissionMCA.com
230
SQL> CREATE OR REPLACE PACKAGE BODY emp_ constraints
AS -- body
2 CURSOR draw_salary RETURN emprectype IS
3 SELECT empid, salary FROM emp ORDER BY salary DESC;
4
PROCEDURE hire_emp (
5 empname VARCHAR2,
6 empage VARCHAR2,
7 salary NUMBER,
8 Hiredate date,
9 job VARCHAR2,
10 empid int) IS
11 BEGIN
12 INSERT INTO emp VALUES
13
(empname,empage,salary,SYSDATE,job,empid_seq.NEXTVAL);
14 END hire_emp;
15
16 PROCEDURE fire_employee(emp_id NUMBER) IS
17 BEGIN
18
DELETE FROM emp WHERE eid = empid;
19 END fire_employee;
20 END emp_ constraints ;
21 /
Package body created.
Call Package Function( For Example 1)
SQL> SELECT pkg_demo.cArea(2) FROM DUAL;
PKG_DEMO.CAREA(2)
----------------12.56
Call Package Procedure( For Example 1)
SQL> call pkg_demo.pPrint();
hello,world!
Call completed.
Package Alter
Package Alter Syntax
ALTER PACKAGE <Package Name> COMPILE BODY;
/
Package Alter Code:
SQL>ALTER PACKAGE pkg1 COMPILE BODY;
Package body Altered.
MissionMCA.com
231
Package Drop
Package Drop Syntax:
DROP PACKAGE <Package Name>;
Package Drop Code:
SQL>DROP PACKAGE pkg1;
Package dropped.
MissionMCA.com
232
The main purpose of overloading procedures is that, when a
PL SQL block is found to do a same operation with different inputs,
we names them same and feed different parameters
Example :
SQL> create or replace package pkg_demo1
2 as
3
procedure pOvr(a in int, b in int);
4
procedure pOvr (str1 VARCHAR2 :='hello',
5
str2 VARCHAR2 :='world',
6
str3 VARCHAR2 :='!' );
7 end;
8 /
Package created.
SQL> create or replace package body pkg_demo1
2
as
3
procedure pOvr(a in int, b in int)
4
is
5
c int;
6
begin
7
c := a + b;
8
dbms_output.put_line(c);
9
end;
10
procedure pOvr(str1 VARCHAR2 :='hello',
11
str2 VARCHAR2 :='world',
12
str3 VARCHAR2 :='!' )
13
is
14
begin
15
dbms_output.put_line(str1||','||str2||str3);
16
end;
17 end;
18 /
Package body created.
SQL> call pkg_demo1.pOvr();
hello,world!
Call completed.
SQL> call pkg_demo1.pOvr(2,3);
5
Call completed
MissionMCA.com
233
MissionMCA.com
234
PL/SQL-based applications. For example, Oracle is supplied with
many utility packages, a few of which are highlighted below.
DBMS_ALERT Package :
Package DBMS_ALERT allows us to use database triggers
to alert an application when specific database values change. The
alerts are transaction based and asynchronous (that is, they
operate independently of any timing mechanism). For example, a
company might use this package to update the value of its
investment portfolio as new stock and bond quotes arrive.
We can also use DBS package in the exception handling
mechanism to show the error message.
DBMS_OUTPUT Package :
Package DBMS_OUTPUT enables us to display output from
PL/SQL blocks and subprograms, which makes it easier to test and
debug them. The procedure put_line outputs information to a buffer
in the SGA. We display the information by calling the procedure
get_line or by setting SERVEROUTPUT ON in Oracle. For
example, suppose we create the following stored procedure:
CREATE PROCEDURE Salary_Statement (payroll OUT
NUMBER) AS
CURSOR c1 IS SELECT salary, empage FROM emp;
BEGIN
payroll := 0;
FOR t1rec IN c1 LOOP
t1rec.empage := NVL(t1rec.empage, 0);
payroll := payroll + t1rec.salary + t1rec.empage;
END LOOP;
/* Display debug info. */
dbms_output.put_line('Value of payroll: ' ||
TO_CHAR(payroll));
END;
/
When we issue the following commands, Oracle displays the
value assigned by the procedure to parameter payroll:
SQL> SET SERVEROUTPUT ON
SQL> VARIABLE num NUMBER
SQL> CALL Salary_Statement (:num);
Value of payroll: 4523
DBMS_PIPE Package :
Package DBMS_PIPE allows different sessions to
communicate over named pipes. (A pipe is an area of memory
used by one process to pass information to another.) We can use
MissionMCA.com
235
the procedures pack_message and send_message to pack a
message into a pipe, send it to another session in the same
instance.
At the other end of the pipe, we can use the procedures
receive_message and unpack_message to receive and unpack
(read) the message. Named pipes are useful in many ways. For
example, we can write routines in C that allow external programs to
collect information, then send it through pipes to procedures stored
in an Oracle database.
UTL_FILE Package :
Package UTL_FILE allows our PL/SQL programs to read
and write operating system (OS) text files. It provides a restricted
version of standard OS stream file I/O, including open, put, get, and
close operations.
When we want to read or write a text file, we call the function
fopen, which returns a file handle for use in subsequent procedure
calls. For example, the procedure put_line writes a text string and
line terminator to an open file, and the procedure get_line reads a
line of text from an open file into an output buffer.
UTL_HTTP Package :
Package UTL_HTTP allows our PL/SQL programs to make
hypertext transfer protocol (HTTP) callouts. It can retrieve data from
the Internet or call Oracle Web Server cartridges. The package has
two entry points, each of which accepts a URL (uniform resource
locator) string, contacts the specified site and returns the requested
data, which is usually in hypertext markup language (HTML) format.
13.11
POINTS TO
PACKAGES:
PONDER
FOR
WRITING
MissionMCA.com
236
procedures. However, changes to a package spec require Oracle to
recompile every stored subprogram that references the package.
Separating Cursor Specs and Bodies with Packages:
We can separate a cursor specification from its body for
placement in a package. That way, we can change the cursor body
without having to change the cursor spec. We code the cursor spec
in the package spec by using this syntax:
CURSOR cursor_name [(param1 [, param2]...)] RETURN
return_type;
In the following example, we use the %ROWTYPE attribute to
provide a record type that represents a row in the database table
employee
CREATE or replace PACKAGE emp_salary AS
CURSOR t1 RETURN employee%ROWTYPE; -- declare
cursor spec
END emp_salary;
/
MissionMCA.com
237
FETCH emp_salary.t1 INTO emp_rec; /* Do more
processing here... */
EXIT WHEN emp_salary.t1%NOTFOUND;
END LOOP;
CLOSE emp_salary.t1;
END;
/
The scope of a packaged cursor is not limited to a PL/SQL
block. When we open a packaged cursor, it remains open until we
close or disconnect it from the session.
13.12 QUESTIONS:
1. What are packages in PL/SQL? What are the advantages of
Packages?
2. Explain the Components and specification of PL/SQL packages.
3. Give simple example of package specification and body
element.
4. Write short note on Data Dictionary and PL/SQL Source Code.
5. How to overload Subprograms in PL/SQL
6. Explain the PL/SQL package BODY with the example.
7. Explain some package features with example.
8. Explain STANDARD package in PL/SQL.
9. Elaborate use of following product specific packages.
DBMS_ALERT
DBMS_OUTPUT
DBMS_PIPE
UTL_FILE
UTL_HTTP
MissionMCA.com
238
14. Write a PL/SQL block of code for a function which calculates
square of a number. Use IN OUT parameter.
15. Write a PL/SQL block of code for a procedure which displays
the information of a Student table. (Create Student table with
proper fields.)
16. Write a PL/SQL block of code for a procedure which displays
the message HELLO WORLD.
Fifth
Edition
By
Steven
MissionMCA.com
239
Unit - VI
14
DYNAMIC SQL
Unit Structure
14.1 Objectives
14.2 The Execution Flow of SQL
14.3 Execution Flow of SQL in PL/SQL Subprograms
14.4 Dynamic SQL
14.5 Dynamic Queries Execution :
14.6 Dynamically Executing a PL/SQL Block
14.7 Dynamic SQL Using Native Dynamic SQL
14.8 Using DBMS_SQL Package
14.9 Advantages of Native Dynamic SQL
14.10 Native Dynamic SQL is faster than DBMS_SQL
14.11 Advantages of the DBMS_SQL Package
14.12 Performing DML Using Dynamic SQL:
14.13 Use of Dynamic SQL in Different Languages:
14.14 Questions
14.15 Further Reading
14.1 OBJECTIVES
After completing this chapter, you will be able to:
Understand the Execution Flow of SQL with PL SQL
Understand Dynamically Executing a PL/SQL Block
Understand to use DBMS_SQL Package
Manage and use Advantages of Native Dynamic SQL
Understand Performing DML Using Dynamic SQL
MissionMCA.com
240
14.3
EXECUTION FLOW
SUBPROGRAMS
OF
SQL
IN
PL/SQL
MissionMCA.com
241
some code which is not part of current code. Instead the static
SQL statements do not change from execution to execution. The
full code of static SQL statements is known at compilation, which
gives the following benefits:
The total compilation checks that the necessary privileges
are already given to access the database objects.
The total compilation verifies that the SQL statements
reference valid database objects.
The Static SQL execution is very much straight forward and
self explanatory. The result and performance of static SQL is
always good than dynamic SQL. Due to these advantages, we
should use dynamic SQL only when we cannot use static SQL to
achieve our results. The static SQL has limitations that can be
avoided with dynamic SQL. Sometimes we may not know the
complete code of the SQL statements that must be executed in a
PL/SQL procedure. Our program may accept user input that
defines the SQL statements to execute, or our program may need
to complete some processing to determine the correct course of
action. In such situation, use dynamic SQL.
For example, a duration calculating application in a data
warehouse environment may not know the exact table name until
runtime. These tables might be named according to the starting
month and year of the quarter, for example INV_01_1997,
INV_04_1997, INV_07_1997, INV_10_1997, INV_01_1998, and so
on. We can use dynamic SQL in our duration calculating application
to specify the table name at runtime.
When we want to run a complex query with a user-selectable
sort order then we can use Dynamic SQL. Instead of coding the
query twice, with different ORDER BY clauses, we can construct
the query dynamically to include a specified ORDER BY clause.
MissionMCA.com
242
MissionMCA.com
243
6
7
8
9
10
11
my_emp_handler_2(e);
ELSIF (emp = 3) THEN
my_emp_handler_3(e);
END IF;
END;
/
Null?
NOT_NULL
Type
VARCHAR2(200)
MissionMCA.com
244
Each emp_location table has the following definition:
Column Name
EMP_NO
E_NAME
JOB
SAL
DEPT_NO
Null?
NOT_NULL
NOT_NULL
NOT_NULL
NOT_NULL
NOT_NULL
Type
NUMBER(4)
VARCHAR2(10)
VARCHAR2(9)
NUMBER(7,2)
NUMBER(2)
TYPE loc_array_type IS
dml_str VARCHAR2(200);
loc_array
6 BEGIN
7
8
loc_array_type;
10
11
12
13
END LOOP
14 END;
15 /
SHOW ERRORS;
MissionMCA.com
245
DDL Operation Using Native Dynamic SQL
The EXECUTE IMMEDIATE statement can perform DDL
operations. For example, the following procedure adds an office
location.
SQL> CREATE OR REPLACE PROCEDURE add_loc (loc
VARCHAR2) IS
2 BEGIN
3
-- insert new location in master table
4
INSERT INTO offices VALUES (loc);
-- create an
employee information table
5
EXECUTE IMMEDIATE
6
'CREATE TABLE ' || 'emp_' || loc ||
7
'(
8
empno NUMBER(4) NOT NULL,
9
ename VARCHAR2(10),
10
job VARCHAR2(9),
11
sal NUMBER(7,2),
12
deptno NUMBER(2)
13
)';
14 END;
15 /
MissionMCA.com
246
10
INTO num_of_emp
11
USING job;
12
RETURN num_of_emp;
13 END;
14 /
MissionMCA.com
247
We must use the DBMS_SQL package to execute a dynamic
SQL statement when we don't know either of the following until runtime:
SELECT list
What placeholders in a SELECT or DML statement must be
bound
In the following situations, we must use native dynamic SQL
instead of the DBMS_SQL package:
The dynamic SQL statement retrieves rows into records.
We want to use the SQL cursor attribute %FOUND, %ISOPEN,
%NOTFOUND, or %ROWCOUNT after issuing a dynamic SQL
statement that is an INSERT, UPDATE, DELETE, or single-row SELECT
statement.
When we need both the DBMS_SQL package and native dynamic
SQL, we can switch between them, using the following:
DBMS_SQL.TO_REFCURSOR Function
DBMS_SQL.TO_CURSOR_NUMBER Function
Note:
MissionMCA.com
248
has
following
advantages
over
the
MissionMCA.com
249
2. Code of Native Dynamic SQL
SQL> CREATE PROCEDURE insertion1
( tname VARCHAR2, dno NUMBER, dname VARCHAR2,
2
location VARCHAR2) IS
3 stmt_str VARCHAR2(200);
4
5 BEGIN
6 stmt_str := 'INSERT INTO ' || tname || ' values (:deptno,
:dname, :loc)';
7
8 EXECUTE IMMEDIATE stmt_str
9
USING
10
dno, dname, location;
11
12 END;
13 /
Procedure created.
SQL> SHOW ERRORS;
No errors.
MissionMCA.com
250
MissionMCA.com
251
query := 'INSERT INTO dept_new VALUES (:dept_no,
:dept_name, :loc)';
This example inserts a new row for which the column values
are in the PL/SQL variables dept_no, dept_name, and location.
Table shows sample code that accomplishes this DML operation
using the DBMS_SQL package and native dynamic SQL.
Table DML Operation Using the DBMS_SQL Package and Native
Dynamic SQL
1. Code of DBMS_SQL DML Operation
SQL> DECLARE
2 stmt_str VARCHAR2(350); my_cur NUMBER;
3 deptid NUMBER := 101; deptname VARCHAR2(20);
4 location VARCHAR2(20);myresources VARCHAR2(20);
rows_processed NUMBER;
5 BEGIN
6 stmt_str := 'INSERT INTO departments VALUES(:did,
:dname,
:location,:resources)';
7 my_cur := DBMS_SQL.OPEN_CURSOR;
8 DBMS_SQL.PARSE(my_cur, stmt_str,
DBMS_SQL.NATIVE);
9
-- supply binds
10 DBMS_SQL.BIND_VARIABLE (my_cur, ':did',
deptid);
11 DBMS_SQL.BIND_VARIABLE (my_cur, ':dname',
deptname);
12 DBMS_SQL.BIND_VARIABLE (my_cur, ':location',
location);
13 DBMS_SQL.BIND_VARIABLE (my_cur, ':resources',
myresources);
14
15
rows_processed := dbms_sql.execute(my_cur);
16
-- execute
17 DBMS_SQL.CLOSE_CURSOR(my_cur); -- close
18 END;
19 /
2. Code of Native Dynamic SQL DML Operation:
SQL> DECLARE
2 stmt_str VARCHAR2(350); deptid NUMBER := 102;
3 deptname VARCHAR2(20); location VARCHAR2(20);
4 myresource VARCHAR2(20);
5 BEGIN
MissionMCA.com
252
6 stmt_str := 'INSERT INTO departments VALUES
7
(:did, :dname, :location,:resources)';
8 EXECUTE IMMEDIATE stmt_str
9
USING deptid, deptname, location ,myresource;
10 END;
11 /
14.13 USE OF
LANGUAGES:
DYNAMIC
SQL
DIFFERENT
14.14 QUESTIONS
1. State the execution flow of SQL in PL/SQL Subprograms.
2. How to execute PL/SQL Block Dynamically?
3. Write short note on Dynamic SQL.
4. How to execute Dynamic queries?
5. What is Native Dynamic SQL?
6. Write short note on DBMS_SQL Package.
7. State the Advantages of Native Dynamic SQL.
8. State the Advantages of DBMS_SQL Package.
9. Where we can use Dynamic SQL other than PLSQL?
MissionMCA.com
253
Practice Questions:
10. Write a Simple example for DML Operation Using Native
Dynamic SQL.
11. Write a Simple example for DDL Operation Using Native
Dynamic SQL.
12. Write a Simple example for Multiple-Row Query Using Native
Dynamic SQL.
13. Use the DBMS_SQL package in the above examples.
Fifth
Edition
By
Steven
MissionMCA.com
254
15
TRIGGERS
Unit Structure
15.1 Objectives
15.2 Defining a Trigger :
15.3 Inside the Triggers
15.4 The Database Triggers & Application Triggers
15.5 Classification of PL/SQL Triggers:
15.6 Difference between BEFORE &. AFTER Triggers
15.7 Execution Sequence of PL/SQL Trigger
15.8 Difference between Statement Level and Row Level triggers
15.9 Building a DML Row Level Trigger
15.10 DDL Trigger creation:
15.11 Calling a Procedure in a Trigger Body:
15.12 Building a Database Event Trigger:
15.13 Creation of a SCHEMA Trigger:
15.14 Identifiers (OLD and NEW):
15.15 INSTEAD OF Triggers (View Triggers):
15.16 Listing of Trigger Information:
15.17 Altering a Trigger:
15.18 Knowing Information about Triggers:
15.19 CYCLIC CASCADING in a TRIGGER
15.20 Boundaries on Trigger Conditions:
15.21 Trigger Exceptions:
15.22 Privileges Required to Use Triggers
15.23 Questions
15.24 Further Reading
15.1 OBJECTIVES
After completing this chapter, you will be able to:
Understand the Fundamentals of Triggers
Create and use Triggers
MissionMCA.com
255
Understand the types of Triggers
Understand the Execution Hierarchy of PL/SQL Trigger
Creating DML and DDL Triggers
View , Alter and Drop the Triggers
Understand the Cyclic Cascading in a Triggers
Understand the Privileges Required to Use Triggers
MissionMCA.com
256
If the trigger issues SQL statements or calls procedures or
functions, then the owner of the trigger must have the privileges
necessary to perform these operations. These privileges must be
granted directly to the owner rather than acquired through roles.
In short we must have maximum privileges to deal with the triggers.
The following are the three criteria which must be kept in mind
before creating and using the DML trigger.
1. There can be three possible DML actions on data i.e. INSERT,
UPDATE or DELETE. These are events for the DML triggers.
2. A simultaneous action can be performed either before or after
an event. This serves as timing for DML triggers.
3. Whether the trigger action must be at DML statement level or at
affected row level, decides the level of a trigger.
After the timing, event and level are set, trigger body must be
created to implement the triggering logic.
Important Note: The size of a trigger cannot be greater than 32
KB.
Syntax of Triggers:
The Syntax for creating a trigger is:
MissionMCA.com
257
[FOR EACH ROW]
WHEN (condition)
BEGIN
--- The SQL Code // application logic goes here
END;
/
Understanding the Syntax:
CREATE [OR REPLACE ] TRIGGER trigger_name :- This line
creates a trigger with the given name or overwrites an existing
trigger with the same name. It is a compulsory part of syntax.
{BEFORE | AFTER | INSTEAD OF } - This line indicates at
what time the trigger should get fired. i.e. For example: before or
after updating a table. INSTEAD OF is used to create a trigger
on a view. Before and After cannot be used to create a trigger
on a view.
{INSERT [OR] | UPDATE [OR] | DELETE} - This line
determines the triggering event. More than one triggering events
can be used together separated by OR keyword. The trigger
gets fired at all the specified triggering event.
[OF col_name] - This statement is used with update triggers.
This statement is used when we want to trigger an event only
when a specific column is updated.
[ON table_name] - This statement identifies the name of the
table or view to which the trigger is associated.
[REFERENCING OLD AS o NEW AS n] - This statement is
used to reference the old and new values of the data being
changed. By default, we reference the values as
:old.column_name or :new.column_name. The reference names
can also be changed from old (or new) to any other user-defined
name. We cannot reference old values when inserting a record,
or new values when deleting a record, because they do not
exist.
[FOR EACH ROW] - This statement is used to determine
whether a trigger must fire when each row gets affected ( i.e. a
Row Level Trigger) or just once when the entire SQL statement
is executed(i.e.statement level Trigger).
WHEN (condition) - This statement is valid only for row level
triggers. The trigger is fired only for rows that satisfy the
condition specified.
For Example: If we want to avoid the duplicate entry of the field
other than primary field then we can create trigger to check that
particular entry. The price of a product changes constantly. It is
important to maintain the history of the prices of the products. We
MissionMCA.com
258
can create a trigger to update the 'price_trace' table when the price
of the product is updated in the 'product' table.
1) Create the 'product' table and 'price_trace ' table
SQL> CREATE TABLE price_trace
16 (product_id number(5), product_name varchar2(32),
17 supplier_name varchar2(32),
4 unit_price number(7,2) );
Table created.
SQL> CREATE TABLE product (product_id number(5),
product_name varchar2(32),
supplier_name varchar2(32), unit_price number(7,2)
);
Table created.
SQL> insert into product values(1312,'Wooden_Door','Galaxy',950);
1 row created.
SQL> insert into product values(1313,'Plastic_Door','Tanmay',1950);
1 row created.
SQL> insert into product values(1314,'Metal_Door','Sun',11450);
1 row created.
MissionMCA.com
259
Once the above update query is executed, the trigger fires
and updates the 'price_trace' table. We can view the result using
following statements.
SQL> select * from price_trace;
PRODUCT_ID
-------------------1312
PRODUCT_NAME
SUPPLIER_NAME UNIT_PRICE
-------------------------- ------------------------ -----------------Wooden_Door
Galaxy
950
MissionMCA.com
260
specify whether the trigger action is to be executed before or after
the triggering statement. BEFORE and AFTER apply to both
statement and row triggers.
MissionMCA.com
261
For example lets create a table 'product_chk' which we can use to
store messages when triggers are fired.
SQL> CREATE TABLE product_chk (Message varchar2(50),
Current_Date date );
Table created.
Let's create a BEFORE and AFTER statement and row level
triggers for the product table.
1) BEFORE UPDATE, Statement Level: This trigger will insert a
record into the table 'product_chk' before a SQL update statement
is executed, at the statement level.
SQL>
CREATE
or
REPLACE
TRIGGER
Before_Update_product
2 BEFORE
3 UPDATE ON product
4 Begin
5 INSERT INTO product_chk
6 Values('Before update, statement level trigger', sysdate);
7 END;
8 /
Trigger created.
2) BEFORE UPDATE, Row Level: This trigger will insert a record
into the table 'product_chk' before each row is updated.
SQL>
CREATE
or
REPLACE
TRIGGER
Before_Upddate_Row_product
2 BEFORE
3 UPDATE ON product
4 FOR EACH ROW
5 BEGIN
6 INSERT INTO product_chk
7 Values('Before update row level trigger',sysdate);
8 END;
9 /
Trigger created.
3) AFTER UPDATE, Statement Level: This trigger will insert a
record into the table 'product_chk' after a SQL update statement is
executed, at the statement level.
SQL> CREATE or REPLACE TRIGGER
After_Update_product
2 AFTER
MissionMCA.com
262
3
4
5
6
7
8
UPDATE ON product
BEGIN
INSERT INTO product_chk
Values('After update, statement level trigger', sysdate);
End;
/
Trigger created.
4) AFTER UPDATE, Row Level: This trigger will insert a record
into the table 'product_chk' after each row is updated.
SQL> CREATE or REPLACE TRIGGER
After_Update_Row_product
2 AFTER
3 insert On product
4 FOR EACH ROW
5 BEGIN
6 INSERT INTO product_chk
7 Values('After update, Row level trigger',sysdate);
8 END;
9 /
Trigger created.
Now lets execute a update statement on table item.
SQL> UPDATE product SET unit_price = 850
2 WHERE product_id in (1312,1314);
2 rows updated.
We can check the data in 'product_chk' table to see the order in
which the trigger is fired.
SQL> SELECT * FROM product_chk;
Output:
MESSAGE
CURRENT_DATE
-------------------------------------------------- ---------------------Before update, statement level
29-AUG-12
Before update row level trigger
29- AUG -12
Before update row level trigger
29- AUG -12
After update, statement level triggers
29- AUG -12
The above result shows 'before update' and 'after update'
row level events have occurred twice, since two records were
updated. But 'before update' and 'after update' statement level
events are fired only once per SQL statement.
MissionMCA.com
263
The above rules apply similarly for INSERT and DELETE
statements.
MissionMCA.com
264
SQL> create table trace_emp(empid int,status char(2),
2
actdate date,act char(20),remark char(100));
Table created.
SQL> CREATE OR REPLACE TRIGGER trace_employee
2 BEFORE INSERT ON employee
3 BEGIN
4 INSERT INTO trace_emp(empid, status, actdate, act, remark)
5 VALUES(2, 'P', SYSDATE, 'CREATE', 'you are with the
triggers');
6 END;
7 /
Trigger created.
SQL> INSERT INTO EMPLOYEE (empid, empname, empsal,
jobtitle)
2 VALUES(4, 'SONALI', 34500, 'manager');
1 row created.
SQL> SELECT * FROM trace_emp;
EMPID
---------2
ST
-----P
ACTDATE
ACT
REMARK
----------------------------------------------------------------29-AUG-12
CREATE you are with the triggers
ELSIF DELETING
END IF;
MissionMCA.com
265
END;
/
Output>>TRIGGER created.
TRIGGER
Output>>TRIGGER created.
MissionMCA.com
266
The procedure check_salary could be implemented in
PL/SQL, C, or Java. Also, we can specify: OLD values in the CALL
clause instead of: NEW values.
msg =>
Event
OLD value
INSERT NULL
NEW value
Current value
MissionMCA.com
267
UPDATE Old value of record
Record
value
before
DELETE
operation
NULL
Example:
The trigger below archives an employee record if salary is
incremented by more than 2000. Note that the increment is
checked by the WHEN clause condition.
SQL> CREATE OR REPLACE sal_incr
2 BEFORE UPDATE OF empsal ON employee
3 FOR EACH ROW
4 WHEN(OLD.empsal - NEW.empsal > 2000)
5 BEGIN
6 INSERT INTO EMP_ARCHIVE (id, empid, OLD_SAL,
NEW_SAL, REVISED_DT)
VALUES
7 (SQ_ARC.NEXTVAL, :OLD.EMPID,:OLD.SALARY,
:NEW.SALARY, SYSDATE);
8 END;
9/
MissionMCA.com
268
SELECT O.ID, O.QTY, O.ORD_DATE, P.SITE_ID,
P.RET_LIMIT
FROM ORDERS O, WAREHOUSE P
WHERE O.SITE_ID=P.SITE_ID
CREATE OR REPLACE TRIGGER T_UPD_ORDERVIEW
INSTEAD OF UPDATE ON ORD_VU
BEGIN
INSERT INTO WAREHOUSE_HISTORY
(SITE_ID, OLD_RET_LIMIT, NEW_RET_LIMIT,
UPD_DATE, UPD_USER)
VALUES
(:OLD.SITE_ID, :OLD_RET_LIMIT, :NEW.RET_LIMIT,
SYSDATE, USER);
UPDATE WAREHOUSE
SET RET_LIMIT = :NEW.RET_LIMIT
WHERE SITE_ID = :OLD.SITE_ID;
END;
/
MissionMCA.com
269
For example, lets say that with all our troubles, we still need to
modify the DOB of SONALI SAMBARE . We cannot do this since
we have a trigger on that table that prevents just that. So, we can
disable it...
ALTER TRIGGER PERSON_DOB DISABLE;
Now, we can go ahead and modify the DOB :-)
UPDATE PERSON SET DOB = SYSDATE WHERE NAME =
YASHASHREE SAMBARE;
We can then re-ENABLE the trigger.
ALTER TRIGGER PERSON_DOB ENABLE;
If we then try to do the same type of modification, the trigger kicks
and prevents us from modifying the DOB.
This view stores information about header and body of the trigger.
SELECT * FROM user_triggers
'Before_Update_product';
WHERE
trigger_name
MissionMCA.com
270
The above SQL query provides the header and body of the trigger
'Before_Update_Stat_product'.
We can drop a trigger using the following command.
DROP TRIGGER trigger_name;
MissionMCA.com
271
Triggers become part of the transaction of a statement,
which implies that it causes (or raises) any exceptions, the whole
statement is rolled back.
Think of an exception as a flag that is raised when an error
occurs. Sometimes, an error or exception is raised for a valid
reason. For example, to prevent some action that improperly
modifies the database. Lets say that our database should not allow
anyone to modify their DOB (after the person is in the database,
their DOB is assumed to be static). Anyway, we can create a trigger
that would prevent us from updating the DOB:
CREATE OR REPLACE
TRIGGER change_resist_id
BEFORE UPDATE OF empid ON employee
FOR EACH ROW
BEGIN
RAISE_APPLICATION_ERROR (-20000,CANNOT
CHANGE DATE OF BIRTH);
END;
/
Notice the format of the trigger declaration. We explicitly
specify that it will be called BEFORE UPDATE OF DOB ON
PERSON. The next thing we should notice is the procedure call
RAISE APPLICATION ERROR, which accepts an error code, and
an explanation string. This effectively halts our trigger execution,
and raises an error, preventing our DOB from being modified. An
error (exception) in a trigger stops the code from updating the DOB.
When we do the actual update for example
UPDATE PERSON SET DOB = SYSDATE;
We end up with an error, which says we CANNOT CHANGE DATE
OF BIRTH.
UPDATE PERSON SET DOB = SYSDATE;
UPDATE PERSON SET DOB = SYSDATE
*
ERROR at line 1:
ORA-20000: CANNOT CHANGE DATE OF BIRTH
ORA-06512: at "PARTICLE.PERSON_DOB", line 2
ORA-04088: error during execution of trigger
PARTICLE.PERSON_DOB
We should also notice the error code of ORA-20000. This is our 20000 parameter to RAISE APPLICATION ERROR.
MissionMCA.com
272
15.23 QUESTIONS:
1. Define Triggers. Explain the syntax of creating triggers in
PL/SQL.
2. Explain the types of triggers.
3. Distinguish between BEFORE and AFTER Triggers.
4. Write the execution hierarchy of Triggers.
5. Distinguish between Statement Level and Row Level Triggers.
6. How to create DML statement Triggers.
7. Explain the conditional predicate to detect the Trigger DML
operation.
8. Explain the creation of DML Row Level Trigger with example.
9. How to call procedure in Trigger body.
10. Explain the creation of Database Event Trigger with example.
MissionMCA.com
273
11. Explain the creation of SCHEMA Trigger with example.
12. Explain Triggers on view.
13. How to View, Alter and Remove Triggers? Explain with
examples.
14. Explain Cyclic Cascading in Triggers.
15. List and Explain the Restrictions on Trigger Conditions.
16. Write short note on Trigger Exceptions.
17. List and Explain Privileges Required to Use Triggers. Create a
student view for students personal information.
18. Create a view for Teacher and change it to select all teachers
having subject I.T...
Fifth
Edition
By
Steven
MissionMCA.com