0% found this document useful (0 votes)
35 views

Dbms Unit 10

This document covers key concepts in PL/SQL including views, stored procedures, triggers, and cursors. Views are virtual tables based on a SELECT query. Stored procedures allow reusable code and improved security. Triggers automatically execute code in response to data changes. Cursors allow traversing result sets from a SELECT query. The document provides examples and explanations of each concept. Questions commonly asked about these topics on exams are also listed.

Uploaded by

Krish Patel
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views

Dbms Unit 10

This document covers key concepts in PL/SQL including views, stored procedures, triggers, and cursors. Views are virtual tables based on a SELECT query. Stored procedures allow reusable code and improved security. Triggers automatically execute code in response to data changes. Cursors allow traversing result sets from a SELECT query. The document provides examples and explanations of each concept. Questions commonly asked about these topics on exams are also listed.

Uploaded by

Krish Patel
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 28

3130703

Database Management
Systems

Unit-10
PL/SQL Concepts
Topics to be covered
• View
• Stored Procedures
• Database Triggers
• Cursors

Unit – 10: PL/SQL Concepts 2


View

Unit – 10: PL/SQL Concepts 3


View
 In PL/SQL, a VIEW is a virtual relation based on the result-set of a
SELECT statement.
 A view contains rows and columns. The fields in a view are fields
from one or more tables in the database.
 Syntax:-
CREATE VIEW view_name AS
SELECT column_name(s)
FROM table_name
WHERE condition;

Unit – 10: PL/SQL Concepts 4


Example of view
 Consider the CUSTOMERS table having the following records:
ID NAME AGE CITY SALARY
1 Ramesh 32 Ahmedabad 20000
2 Karan 35 Rajkot 15000
3 Mayur 30 Surat 22000
4 Dipak 35 Rajkot 20000
5 Nilesh 38 Surat 29000
6 Kalpesh 37 Ahmedabad 26000

 If we want to create a view that contain customer name and age


from CUSTOMERS table:

Unit – 10: PL/SQL Concepts 5


Example of view
 Query:-
CREATE VIEW CUSTOMERS_VIEW AS
SELECT name, age
FROM CUSTOMERS;

 Output :- NAME AGE


Ramesh 32
Karan 35
Mayur 30
Dipak 35
Nilesh 38
Kalpesh 37

Unit – 10: PL/SQL Concepts 6


Stored Procedure

Unit – 10: PL/SQL Concepts 7


Stored procedure
 A stored procedure is a group of PL/SQL statements that
performs specific task.
 A procedure has two parts, header and body.
 The header consists of the name of the procedure and the
parameters passed to the procedure.
 The body consists of declaration section, execution section and
exception section.

Unit – 10: PL/SQL Concepts 8


Stored procedure
 Create:-It will create a procedure.
 Replace :- It will re-create a procedure if it already exists.
 We can pass parameters to the procedures in three ways.
1. IN-parameters: - These types of parameters are used to send values to
stored procedures.
2. OUT-parameters: - These types of parameters are used to get values from
stored procedures.
3. IN OUT-parameters: - This type of parameter allows to pass values into a
procedure and get output values from the procedure.

Unit – 10: PL/SQL Concepts 9


Stored procedure (Syntax)
 CREATE PROCEDURE procedure_name (list of parameters)
IS
BEGIN
< body >
END ;

 REPLACE PROCEDURE procedure_name (list of parameters)


IS
BEGIN
< body >
END ;

Unit – 10: PL/SQL Concepts 10


Advantages of stored procedure
 Security:- We can improve security by giving rights to selected
persons only.
 Sharing of code:- Once procedure is created and stored, it can be
used by more than one user.
 Faster Execution:- It is precompiled so compilation of procedure
is not required every time you call it.
 Productivity:- Code written in procedure is shared by all
programmers. This eliminates redundant coding by multiple
programmers so overall improvement in productivity.

Unit – 10: PL/SQL Concepts 11


Simple Example of PL/SQL
 Example : 1
BEGIN
dbms_output.put_line('Hello World');
END;

Unit – 10: PL/SQL Concepts 12


Example of Stored Procedure
 Example : 2
DECLARE
a number;
b number;
c number;
PROCEDURE findMin (x IN number, y IN number, z OUT number) IS
BEGIN
IF x < y
THEN z:= x;
ELSE z:= y;
END IF; OUTPUT :
Minimum of (23, 45) : 23
END;
PL/SQL procedure
BEGIN successfully completed.
a:= 23;
b:= 45;
findMin(a, b,c);
dbms_output.put_line(' Minimum of (23, 45) : ' || c);
UnitEND;
– 10: PL/SQL Concepts 13
Example of Stored Procedure
 Example : 3
DECLARE
a number;
PROCEDURE squareNum(x IN OUT number) IS
BEGIN
x := x * x;
END; OUTPUT :
Square of (23): 529
BEGIN PL/SQL procedure successfully
completed.
a:= 23;
squareNum(a);
dbms_output.put_line(' Square of (23): ' || a);
END;
Unit – 10: PL/SQL Concepts 14
Triggers

Unit – 10: PL/SQL Concepts 15


Database triggers
 A trigger is a PL/SQL block structure which is triggered (executed)
automatically when DML statements like Insert, Delete, and
Update is executed on a table.
 In SQL Server we can create the following 3 types of triggers:
1. Data Definition Language (DDL) triggers
2. Data Manipulation Language (DML) triggers
3. Logon triggers

Unit – 10: PL/SQL Concepts 16


Triggers (syntax)
 CREATE [OR ALTER] TRIGGER trigger_name
ON table_name
{ FOR | AFTER | INSTEAD OF }
{ [ INSERT ] [ , ] [ UPDATE ] [ , ] [ DELETE ] }
AS
BEGIN
Executable statements
END;

Unit – 10: PL/SQL Concepts 17


Triggers
 CREATE [OR ALTER ] TRIGGER trigger_name:-
• This clause creates a trigger with the given name or overwrites an existing
trigger.
 ON table_name:-
• This clause identifies the name of the table to which the trigger is related.
 { FOR | AFTER | INSTEAD OF }:-
• This clause indicates at what time the trigger should be fired. Before
executing DML statements or after executing DML statements.

Unit – 10: PL/SQL Concepts 18


Triggers
 { [ INSERT ] [ , ] [ UPDATE ] [ , ] [ DELETE ] } :-
• This clause determines on which kind of statement the trigger should be
fired.
• Either on insert or update or delete or combination of any or all.
• More than one statement can be used together separated by comma.

Unit – 10: PL/SQL Concepts 19


Example of triggers
 Trigger to display a message when we perform insert operation on
student table.

CREATE TRIGGER student_msg


on Student
AFTER INSERT
AS
BEGIN
print ‘Record inserted successfully'
END

Unit – 10: PL/SQL Concepts 20


Example of triggers
 OUTPUT:- Trigger is created.
 Now when you perform insert operation on student table.
 SQL:> Insert into student values (101, ‘Raj’, ‘CE’);
 It displays following message after executing insert statement.
 Output:- Record inserted successfully
 We get message that “Record inserted successfully” it indicates
that trigger has executed after the insertion operation.

Unit – 10: PL/SQL Concepts 21


Example of triggers
 Trigger to display a message when we perform insert, update or
delete operation on student table.

CREATE TRIGGER student_msg


on Student
AFTER INSERT, UPDATE, DELETE
AS
BEGIN
print ‘One record is affected'
END

Unit – 10: PL/SQL Concepts 22


Example of triggers
 OUTPUT:- Trigger is created.
 Now when you perform insert, update or delete operation on
student table.
 SQL:> Insert into student values (102, ‘Raj’, ‘CE’); OR
Update student set Dept=‘EC’ where Rno=101 OR
Delete from student where Rno=101
 It displays following message after executing insert, update or
delete statement.
 Output:- One record is affected
 We get message that “One record is affected” it indicates that
trigger has executed after the insertion operation.

Unit – 10: PL/SQL Concepts 23


Cursor

Unit – 10: PL/SQL Concepts 24


Cursor
 Cursors are database objects used to traverse the results of a
select SQL query.
 It is a temporary work area created in the system memory when
a select SQL statement is executed.
 This temporary work area is used to store the data retrieved from
the database, and manipulate this data.

Unit – 10: PL/SQL Concepts 25


Types of cursor
 There are two types of cursors in PL/SQL:
1. Implicit cursors:
• These are created by default by SQL itself when DML statements like, insert,
update, and delete statements are executed.
• We cannot use implicit cursors for user defined work.
2. Explicit cursors:
• Explicit cursors are user defined cursors written by the developer.
• They can be created when a SELECT statement returns more than one row.
• Even though the cursor stores multiple records, only one record can be
processed at a time, which is called as current row.

Unit – 10: PL/SQL Concepts 26


Questions asked in GTU
1. Write a PL/SQL block to print the sum of Numbers from 1 to 100.
2. Write a PL/SQL block to print the given number is prime or not.
3. Write a PL/SQL program for inserting even numbers in EVEN table
and odd number in ODD table from number 1 to 50.
4. Explain Cursor in PL/SQL.
5. Explain stored procedure with proper example.
6. What are triggers? write advantages of stored procedures.

Unit – 10: PL/SQL Concepts 27 Darshan Institute of Engineering & Technology


Database Management Systems
(DBMS)
GTU # 3130703

Thank
You

You might also like