Lecture 1.1 and 1.2 Procedures and Advantages of Procedures
Lecture 1.1 and 1.2 Procedures and Advantages of Procedures
Database
Management
System
UNIT-I [10h]
Overview of Databases: Database concepts, DBMS, Data Base System Architecture (Three
Level ANSI-SPARC Architecture), Advantages and Disadvantages of DBMS, Data Independence,
DBA and Responsibilities of DBA, Relational Data Structure, Keys, Relations, Attributes, Schema and
Instances, Referential integrity, Entity integrity.
Data Models: Relational Model, Network Model, Hierarchical Model, ER Model: Design,
issues, Mapping constraints, ER diagram, Comparison of Models.
UNIT-II [10h]
Functional dependencies and Normalization: Functional dependencies, Decomposition, Full
Functional Dependency (FFD), Transitive Dependency (TD), Join Dependency (JD), Multi-valued
Dependency (MVD), Normal Forms (1NF, 2NF, 3NF, BCNF), De-normalization.
Database Security: Introduction, Threats, Counter Measures.
Control Structures: Introduction to conditional control, Iterative control and sequential control
statements, Cursors, Views.
3
University Institute of Engineering (UIE)
Department of Computer Science and Engineering (CSE)
UNIT-III [10h]
Package, Procedures and Triggers: Parts of procedures, Parameter modes, Advantages of
procedures, Syntax for creating triggers, Types of triggers, package specification and package body,
developing a package, Bodiless package, Advantages of packages.
Transaction Management and Concurrency Control: Introduction to Transaction Processing,
Properties of Transactions, Serializability and Recoverability, Need for Concurrency Control, Locking
Techniques, Time Stamping Methods, Optimistic Techniques and Granularity of Data items.
Database Recovery of database: Introduction, Need for Recovery, Types of errors, Recovery
Techniques.
4
University Institute of Engineering (UIE)
Department of Computer and Communication Engineering (CCE)
Learning Objective
• Procedures with example
• Parameters and type of parameters
– Actual and Formal Parameters
Learning Outcome
• Understanding the concept of procedures with example.
• Discuss the various parameters and its types.
• Understanding the use of functions in Data Base Management
System.
• Discuss the concepts of Trigger and its various types.
• Discuss the concepts of packages and its advantages
PROCEDURES
• A procedure is a module performing one or more actions; it
does not need to return any values.
• The syntax for creating a procedure is as follows:
CREATE OR REPLACE PROCEDURE name
[(parameter[, parameter, ...])]
AS
[local declarations]
BEGIN
executable statements
[EXCEPTION
exception handlers]
END [name];
PROCEDURES
• A procedure may have 0 to many parameters.
• Every procedure has two parts:
1. The header portion, which comes before AS (sometimes you
will see IS—they are interchangeable), keyword (this contains
the procedure name and the parameter list),
2. The body, which is everything after the IS keyword.
• The word REPLACE is optional.
• When the word REPLACE is not used in the header of the procedure,
in order to change the code in the procedure, it must be dropped first
and then re-created.
Example
CREATE OR REPLACE PROCEDURE Discount
AS
CURSOR c_group_discount
IS
SELECT distinct s.course_no, c.description
FROM section s, enrollment e, course c
WHERE s.section_id = e.section_id
AND c.course_no = s.course_no
GROUP BY s.course_no, c.description,
e.section_id, s.section_id
HAVING COUNT(*) >=8;
BEGIN
Example
FOR r_group_discount IN c_group_discount
LOOP
UPDATE course
SET cost = cost * .95
WHERE course_no = r_group_discount.course_no;
DBMS_OUTPUT.PUT_LINE
('A 5% discount has been given to'||
r_group_discount.course_no||' '||
r_group_discount.description
);
END LOOP;
END;
Example
In order to execute a procedure in SQL*Plus use the following syntax:
EXECUTE Procedure_name
PARAMETERS
• Parameters are the means to pass values to and from the calling
environment to the server.
• These are the values that will be processed or returned via the
execution of the procedure.
• There are three types of parameters:
• IN, OUT, and IN OUT.
• Modes specify whether the parameter passed is read in or a receptacle
for what comes out.
Types of Parameters
Example
Example
Step 2:
1. Define a procedure called updateSalary which takes as
input a department number.
2. The body of the procedure is an SQL command to update
the totalsalary column of the deptsal table.
3. Terminate the procedure definition using the delimiter you
had defined in step 1 (//)
Example
Example
Example
• Reusable
– Stored procedures can be used by different applications and users.
They can be used instead of writing repeated queries from scratch.
• Secure
– Permissions can be granted to a stored procedure separate from
tables/columns, so restricted users can use them.
• Difficult to maintain
– Like triggers, stored procedures are less transparent and harder to
write, especially if the schema of the database changes.
FAQ
• What is procedures ? Explain with the help of examples.
• What is formal and actual parameter?
• Explain Row trigger with the help of example.
• How we can enable, disable and drop trigger?
• What do you understand by packages and write its advantages.
References
• https://docs.oracle.com/database/121/LNPLS/packages.htm
• https://docs.oracle.com/cd/B19306_01/appdev.102/b14261/p
ackages.htm
• https://www.guru99.com/packages-pl-sql.html
• https://docs.oracle.com/cd/B19306_01/server.102/b14200/st
atements_6009.htm
• http://searchoracle.techtarget.com/definition/stored-procedur
e
• https://www.w3resource.com/sql/sql-procedure.php
• https://docs.oracle.com/cd/B19306_01/server.102/b14220/tri
ggers.htm
• https://docs.oracle.com/cd/A57673_01/DOC/server/doc/SCN7
3/ch15.htm
• https://docs.microsoft.com/en-us/sql/t-sql/statements/create-
trigger-transact-sql
University Institute of Engineering (UIE)