0% found this document useful (0 votes)
45 views29 pages

Lecture 1.1 and 1.2 Procedures and Advantages of Procedures

The document discusses database concepts like procedures, parameters, triggers and packages. It explains what procedures and triggers are, the different types of parameters and how they are used. It also provides examples of creating and executing procedures. The document is intended to provide an overview and learning outcomes for these database topics.

Uploaded by

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

Lecture 1.1 and 1.2 Procedures and Advantages of Procedures

The document discusses database concepts like procedures, parameters, triggers and packages. It explains what procedures and triggers are, the different types of parameters and how they are used. It also provides examples of creating and executing procedures. The document is intended to provide an overview and learning outcomes for these database topics.

Uploaded by

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

Department of Computer Science and Engineering (CSE)

Database
Management
System

Course Outcome Will be covered in


CO Title Level this lecture
Numbe
r
CO1 To perceive the significance and Remember
implementation of a commercial
relational database system (Oracle)
by writing SQL using the system.
CO2 To understand the relational database Understand
theory, and be able to write
relational algebra expressions for
queries

CO3 To identify the basic issues of Analysis and


transaction processing and application
concurrency control and find out its
solutions.
2
University Institute of Engineering (UIE)
Department of Computer Science and Engineering (CSE)

Contents of the Syllabus

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.

Relational Algebra & Relational Calculus: Introduction, Syntax, Semantics, Additional


operators, Grouping and Ungrouping, Relational comparisons, Tuple Calculus, Domain Calculus,
Calculus Vs Algebra, Computational capabilities.

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)

Contents of the Syllabus

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)

(Package, Procedures and Triggers)

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.

University Institute of Engineering (UIE) 5


Department of Computer and Communication Engineering (CCE)

Learning Objective
• Procedures with example
• Parameters and type of parameters
– Actual and Formal Parameters

• Functions and its uses.


• Triggers
– Statement trigger
– Row trigger
– Enabling, disabling and dropping trigger

• Packages, its specifications and body

University Institute of Engineering (UIE)


Department of Computer and Communication Engineering (CCE)

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

University Institute of Engineering (UIE)


Department of Computer and Communication Engineering (CCE)

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];

University Institute of Engineering (UIE)


Department of Computer and Communication Engineering (CCE)

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.

University Institute of Engineering (UIE)


Department of Computer and Communication Engineering (CCE)

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

University Institute of Engineering (UIE)


Department of Computer and Communication Engineering (CCE)

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;

University Institute of Engineering (UIE)


Department of Computer and Communication Engineering (CCE)

Example
In order to execute a procedure in SQL*Plus use the following syntax:

EXECUTE Procedure_name

SQL> EXECUTE Discount

University Institute of Engineering (UIE)


Department of Computer and Communication Engineering (CCE)

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.

University Institute of Engineering (UIE)


Department of Computer and Communication Engineering (CCE)

Types of Parameters

University Institute of Engineering (UIE)


Department of Computer and Communication Engineering (CCE)

FORMAL AND ACTUAL


PARAMETERS
• Formal parameters are the names specified within parentheses as part
of the header of a module.
• Actual parameters are the values—expressions specified within
parentheses as a parameter list—when a call is made to the module.
• The formal parameter and the related actual parameter must be of the
same or compatible data types.

University Institute of Engineering (UIE)


Department of Computer and Communication Engineering (CCE)

MATCHING ACTUAL AND FORMAL


PARAMETERS
• Two methods can be used to match actual and formal parameters:
positional notation and named notation.
• Positional notation is simply association by position: The order of the
parameters used when executing the procedure matches the order in
the procedure’s header exactly.
• Named notation is explicit association using the symbol =>
– Syntax: formal_parameter_name => argument_value

• In named notation, the order does not matter.


• If you mix notation, list positional notation before named notation.

University Institute of Engineering (UIE)


Department of Computer and Communication Engineering (CCE)

MATCHING ACTUAL AND FORMAL


PARAMETERS

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

Stored Procedures in MySQL

• A stored procedure is a program that is stored within the database and is


compiled when used.
• Stored procedures can receive input parameters and they can return
results
• Stored procedures are declared using the following syntax:
Create Procedure <proc-name>
(param_spec1, param_spec2, …, param_specn )
begin
-- execution code
end;

where each param_spec is of the form:


[in | out | inout] <param_name> <param_type>
– in mode: allows you to pass values into the procedure,
– out mode: allows you to pass value back from procedure to the calling program

University Institute of Engineering (UIE)


Example
● Click to edit Master text styles
– Second Level
◆ Third Level

We need to write a procedure


to update the salaries in
the deptsal table

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

Example

Step 1: Change the delimiter (i.e., terminating character) of


SQL statement from semicolon (;) to something else (e.g., //)
So that you can distinguish between the semicolon of the
SQL statements in the procedure and the terminating
character of the procedure definition

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

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 (//)

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

Example

Step 3: Change the delimiter back to semicolon (;)

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

Example

Step 4: Call the procedure to update the totalsalary for each


department

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

Example

Step 5: Show the updated total salary in the deptsal table

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

Stored Procedures in MySQL


• Use show procedure status to display the list of stored
procedures you have created

• Use drop procedure to remove a stored procedure

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

Stored Procedures in MySQL


• You can declare variables in stored procedures

• You can use flow control statements (conditional IF-


THEN-ELSE or loops such as WHILE and REPEAT)

• MySQL also supports cursors in stored procedures.


– A cursor is used to iterate through a set of rows returned by a query
so that we can process each individual row.

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

Advantages of Stored Procedures


• Performance
– Stored procedures are compiled and stored in the database. This allows
caching and faster performance on repeated calls.
• Less traffic
– Instead of sending many lengthy SQL queries, the connection can call
the stored procedure and get the results in one go.

• 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.

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

Disadvantages of Stored Procedures


• Memory and CPU usage
– Stored procedures add overhead to each connection, which means
the database can't support as many connections.
• Difficult to debug
– Most DBMSs don't have good support for traceback and error
reporting for stored procedures.

• Difficult to maintain
– Like triggers, stored procedures are less transparent and harder to
write, especially if the schema of the database changes.

University Institute of Engineering (UIE)


Department of Computer and Communication Engineering (CCE)

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.

University Institute of Engineering (UIE)


Department of Computer and Communication Engineering (CCE)

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)

You might also like