0% found this document useful (0 votes)
35 views53 pages

DSC Chapter4 Updated

Uploaded by

vaidyashardul445
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views53 pages

DSC Chapter4 Updated

Uploaded by

vaidyashardul445
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 53

Database Management System

Unit 4 : PL/SQL Programming

Presented By :
Mrs. Samidha Chavan
Lecturer -Department of Information Tech.
Vidyalankar Polytechnic, Wadala (Mumbai)
Unit 4 : PL /SQL Programming

1.Introduction to PL/SQL
2.Control Structure
3.Exception handling
4.Cursor
5.Procedure
6.Function
7.Trigger
PL/SQL data types
PL/SQL data types
CHARACTER Data Type:
This data type basically stores alphanumeric characters in
string format.
This character data type is further classified as
follows:
• CHAR Data type (fixed string size)
• VARCHAR2 Data type (variable string size)
• VARCHAR Data type
• NCHAR (native fixed string size)
• NVARCHAR2 (native variable string size)
• LONG and LONG RAW
PL/SQL data types
NUMBER Data Type:
This data type stores fixed or floating point numbers up to 38 digits
of precision. This data type is used to work with fields which will
contain only number data.

BOOLEAN Data Type:


This data type stores the logical values. It represents either TRUE or
FALSE and mainly used in conditional statements.
PL/SQL data types
DATE Data Type:
This data type stores the values in date format, as date,
month, and year.

LOB Data Type:


This data type is mainly used to store and manipulate large
blocks of unstructured data's like images, multimedia files, etc.
They can store up to the size of 128 terabytes.
PL/SQL data types
DATE Data Type:
This data type stores the values in date format, as date,
month, and year.

LOB Data Type:


This data type is mainly used to store and manipulate large
blocks of unstructured data's like images, multimedia files, etc.
• They can store up to the size of 128 terabytes.
1. BLOB
2. CLOB and NCLOB
3. BFILE
PL/SQL data types
• This data type stores the LOB data in
the binary file format up to the
BLOB maximum size of 128 TB.
• it can store the unstructured data
such as multimedia objects, images,
etc.

• CLOB data type stores the LOB data


CLOB and NCLOB into the character set, whereas
NCLOB stores the data in the native
character set.

• BFILE are the data types that stored


BFILE the unstructured binary format data
outside the database as an operating-
system file.
PL/SQL Variables
A variable is a meaningful name which facilitates a
programmer to store data temporarily during the execution of
code.
Declare the variable first in the declaration section of a PL/SQL block
before using it.

1.DECLARE
2. a integer := 30;
3. b integer := 40;
4. c integer;
5. f real;
6.BEGIN
7. c := a + b; OUTPUT
8. dbms_output.put_line('Value of c: ' || c); Value of c: 70
9. f := 100.0/3.0; Value of f: 33.333333333333333333
10. dbms_output.put_line('Value of f: ' || f);
11.END; PL/SQL procedure successfully completed.
PL/SQL Constants
A constant is a value used in a PL/SQL block that remains unchanged
throughout the program.

1.DECLARE
2. -- constant declaration
3. pi constant number := 3.141592654;
4. -- other declarations
5. radius number(5,2);
6. dia number(5,2);
7. circumference number(7, 2);
8. area number (10, 2);
Exception Handling in PL/SQL
 An exception is an error condition during a program execution.
 PL/SQL supports programmers to catch such conditions
using EXCEPTION block in the program and an appropriate action is taken
against the error condition.

There are two types of exceptions −

•System-defined exceptions :( Pre Defined Exceptions )


They are executed when any database rule is violated by a program.
For example, the predefined exception NO_DATA_FOUND is raised when a
SELECT INTO statement returns no rows.
•User-defined exceptions :
PL/SQL allows you to define your own exceptions according to the need of
your program.
Pre-defined Exceptions/ System Defined Exception
Pre-defined Exceptions/ System Defined Exception
Example -System Defined Exception – ZERO_DIVIDE

declare
v_sum number := 10;
v_divide number := 0;
v_result number;
begin
v_result := v_sum / v_divide;
dbms_output.put_line('v_result: '||v_result);

exception
when zero_divide then
dbms_output.put_line(‘ZERO_DIVIDE ERROR’);
end;

Output:
ZERO_DIVIDE ERROR
Example -System Defined Exception – NO_DATA_FOUND

OUTPUT

Since there is no customer with ID value 8 in our


database, the program raises the run-time
exception NO_DATA_FOUND, which is captured in
the EXCEPTION block.
User-defined Exceptions

• PL/SQL allows you to define your own exceptions according to the need of your
program.
• A user-defined exception must be declared and then raised explicitly, using
either a RAISE statement
OR
procedure DBMS_STANDARD.RAISE_APPLICATION_ERROR.

The syntax for declaring an exception is −


Syntax for Exception Handling
Example -User Defined Exception – ex_invalid_id

Exception will get called if we


enter any negative number for
c_id
Employee
eid ename Salary
1 sita 3000
2 nita 4000
3 gita 5000
4 rita 2000
5 babita 6000
6 smita 7000
7 neha 8000
ORACLE
HDD Select * from Employee
5 babita 6000 where Salary >5000;
6 smita 7000
7 neha 8000
5 babita 6000
CURSOR 6 smita 7000
7 neha 8000
Active set
Cursors in PL/SQL
 A cursor is a pointer to this context area.
 PL/SQL controls the context area through a cursor.
 A cursor holds the rows (one or more) returned by a SQL statement. The set
of rows the cursor holds is referred to as the active set.

There are two types of cursors −

1. Implicit cursors
2. Explicit cursors
Implicit Cursors

1. Implicit cursors are automatically created by Oracle whenever an SQL


statement is executed.
2. Programmers cannot control the implicit cursors and the information in
it.
3. Whenever a DML statement (INSERT, UPDATE and DELETE) is issued,
an implicit cursor is associated with this statement.
Explicit Cursors
1. Explicit cursors are programmer-defined cursors for gaining more
control over the context area.
2. An explicit cursor should be defined in the declaration section of the
PL/SQL Block.
3. It is created on a SELECT Statement which returns more than one
row.

The syntax for creating an explicit cursor is −


Example -Explicit Cursors

Output :
Trigger: A trigger is a stored procedure in database which
automatically invokes whenever a special event in the database
occurs. For example, a trigger can be invoked when a row is
inserted into a specified table or when certain table columns are
being updated.
Types of triggers
Row Triggers
A row trigger is fired each time the table is affected by the triggering
statement.
For example, if an UPDATE statement updates multiple rows of a table, a
row trigger is fired once for each row affected by the UPDATE statement.
Statement Triggers
A statement trigger is fired once on behalf of the triggering statement, regardless of the
number of rows in the table that the triggering statement affects, even if no rows are
affected.
For example, if a DELETE statement deletes several rows from a table, a statement-level
DELETE trigger is fired only once

BEFORE Triggers
BEFORE triggers run the trigger action before the triggering statement is run.

AFTER Triggers
AFTER triggers run the trigger action after the triggering statement is run.

INSTEAD OF Triggers
These triggers are called INSTEAD OF triggers because, unlike other types of triggers,
Oracle fires the trigger instead of executing the triggering statement.
Syntax for trigger creation :

create trigger [trigger_name]


[before | after]
{insert | update | delete}
on [table_name]
[for each row]
[trigger_body]
Example : how to create a trigger

Output
Create Function: Syntax
CREATE [OR REPLACE] FUNCTION function_name
[(argument1 [mode1] datatype1,
argument2 [mode2] datatype2,
. . .)]
RETURN datatype
IS|AS
function_body;
Procedure: Syntax
CREATE [OR REPLACE] PROCEDURE procedure_name
[(argument1 datatype1,
argument2 datatype2,
. . .)]
IS|AS
procedure_body
{
BEGIN
………
EXCEPTION
……….
END;
}
Q1. Write a function circle_area () to find area of circle, use
radius as input parameter.

i)PL/SQL code for defining circle_area() function:

CREATE OR REPLACE FUNCTION circle_area (radius


NUMBER)
RETURN NUMBER IS
pi CONSTANT NUMBER (7,2): =3.14;
area NUMBER (7,2);
BEGIN
area: = pi * (radius * radius);
RETURN area;
END;
PL/SQL code for calling circle_area() function:

DECLARE
c number (5,2);
BEGIN
c: = circle_area(1);
dbms_output.put_line('Area of circle: ' || c);
END;
Q2. Write a procedure emp_count ( ) to count number of employees in
department, use dept_no as input parameter (W-22)
CREATE OR REPLACE PROCEDURE emp_count(dept_no IN NUMBER)
IS
BEGIN
DECLARE
CURSOR c
IS SELECT * FROM emp where deptno=dept_no;
TYPE emp_tab IS TABLE OF c%ROWTYPE INDEX BY BINARY_INTEGER;
v_emp_tab emp_tab;

BEGIN
OPEN c;
FETCH c BULK COLLECT INTO v_emp_tab;
DBMS_OUTPUT.PUT_LINE(v_emp_tab.COUNT);
CLOSE c;
END;
END;
Q2. Write a procedure emp_count ( ) to count number of employees in
department, use dept_no as input parameter (W-22)

To execute procedure
exec emp_count(10);

You might also like