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

PPT of Chapter 3.1 -Package, Procedures and Triggers

The document outlines a course on Database Management Systems (DBMS) focusing on data structures, integrity, and SQL query construction. It details course outcomes, objectives, and topics such as PL/SQL packages, procedures, and triggers, emphasizing their modularity, reusability, and applications. Additionally, it covers the creation, modification, and advantages of procedures and triggers in database environments.

Uploaded by

premrawat9873
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)
28 views

PPT of Chapter 3.1 -Package, Procedures and Triggers

The document outlines a course on Database Management Systems (DBMS) focusing on data structures, integrity, and SQL query construction. It details course outcomes, objectives, and topics such as PL/SQL packages, procedures, and triggers, emphasizing their modularity, reusability, and applications. Additionally, it covers the creation, modification, and advantages of procedures and triggers in database environments.

Uploaded by

premrawat9873
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/ 36

UNIVERSITY INSTITUTE OF

ENGINEERING

Department of Computer Science & Engineering

Databases Management System


(23CSH-205/23ITH-205)
DR. GITANJALI
E16525
ASSISTANT PROFESSOR DISCOVER . LEARN . EMPOWER
CSE
ABOUT COURSE

• The main DBMS course, we learn about the structural formation of data, maintain data
integrity, multitasking with concurrent access and recovery without occurring crashes,
data structures, data models etc. and their working in which every organization is based.
• At the end of the course, the students may understand the concepts of data structures,
data models and design, construction of queries by using SQL, uses and applications of
database design etc
COURSE OUTCOMES

CO No. Title BT Level


Will be covered in this
lecture
Students will be able to understand the database system
CO1
concepts and design databases for different applications.
Memorize

Students will be able to identify different types of DDL, DML,


CO2 DCL and TCL commands and their usage. Understanding

Students will be able to classifying Normalization,


CO3 Dependencies and Denormalization along with their Apply
requirements.
Students will be able to analyse and Compare Transaction
CO4
Processing techniques and Recovery techniques.
Analyze
Students will be able to evaluate database performance after
CO5 implementing Triggers, procedures, packages, cursors and Evaluate
views.
COURSE OBJECTIVES
This course will enable students to,

1. To have good understanding of database system concepts and design databases for
different applications.
2. To learn how to use a DBMS and RDBMS.
3. To implement and understand different types of DDL, DML and DCL statements.
4. To understand transaction concepts related to databases and recovery/backup
techniques required for the proper storage of data.
Topics covered

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.
Package
PL/SQL packages are a way to organize and encapsulate related procedures,
functions, variables, triggers, and other PL/SQL items into a single item. Packages
provide a modular approach to write and maintain the code. It makes it easy to
manage large codes.
A package is compiled and then stored in the database, which then can be shared
with many applications. The package also has specifications, which declare an item
to be public or private. Public items can be referenced from outside of the package.

A PL/SQL package is a collection of related

Package
Procedures, Functions, Variables, and other
elements that are grouped for Modularity and
Reusability.
Package
Key Benefits of Using PL/SQL Packages

• Modularity: Packages provide a modular structure, allowing


developers to organise and manage code efficiently.
• Code Reusability: Procedures and functions within a
package can be reused across multiple programs, reducing
redundancy.
• Private Elements: Packages support private procedures and
functions, limiting access to certain code components.
• Encapsulation: Packages encapsulate related logic,
protecting internal details and promoting a clear interface to
other parts of the code.
Package
Developing a Package
Structure of a PL/SQL Package
A PL/SQL package consists of two parts: Package
• A package Specification
• A package Body

Package Package
Specification Body
Package
1. Package Specification
The package specification declares the public interface of the package. It includes
declarations of procedures, functions, variables, cursors, and other constructs that are
meant to be accessible from outside the package. The specification is like a header file that
defines what a package can do.
Example of Package Specification:
Package
2. Package Body
The package body contains the implementation of the details of the package. It includes the
coding of the procedures or functions which are decalared in the package specification.
The body can also contain private variables and procedures that are not exposed to outside
the code.
Example of Package Body:
Package
Bodiless Package
A bodiless package in PL/SQL refers to a package that does not require a package body. In such
cases, only the package specification is created, and it contains all the declarations of variables,
constants, cursors, or subprograms. However, the package body is omitted because no
implementation is required.
When to Use Bodiless Packages?
1. Public Declarations Only: If the package contains only public variables, constants, or
cursors and does not need any subprogram implementation.
2. Forward Declarations: When subprograms (procedures/functions) are declared but
implemented elsewhere (e.g., standalone procedures or functions).
3. Minimal Complexity: If the package is used as a simple interface to expose global
variables or cursors.
Package
Syntax of a Bodiless Package
Package
Example of a Bodiless Package
Package
Advantages of Bodiless Packages
• Simplified Structure: No need for a body when implementation is not required.
• Improved Readability: Only the interface (specification) is provided, making it easy to
understand.
• Reusability: Useful for sharing global variables, constants, or cursors across multiple
PL/SQL programs.
• Forward Compatibility: You can add a package body later if needed.

Disadvantages of Bodiless Packages


• No Implementation of Logic: Since a bodiless package only contains declarations, you
cannot implement procedures, functions, or complex logic within the package.
• No Encapsulation: Encapsulation of internal logic is not possible without a package body.
Procedures
PL/SQL procedures are reusable code blocks that perform specific actions or logic within a
database environment. They consist of two main components such as the procedure header which
defines the procedure name and optional parameters and the procedure body which contains the
executable statements implementing the desired business logic.

1. Procedure Header
• The procedure header includes the procedure name and optional parameter list.
• It is the first part of the procedure and specifies the name and parameters
2. Procedure Body
• The procedure body contains the executable statements that implement the specific
business logic.
• It can include declarative statements, executable statements, and exception-handling
statements
Procedures
Create Procedures in PL/SQL
Syntax
CREATE PROCEDURE syntax is:
Procedures Example
In this example, we will create a procedure in PL/SQL
Procedures
Parameters in Procedures
In PL/SQL, parameters are used to pass values into procedures. There are three types of parameters used in
procedures:
1. IN parameters
Used to pass values into the procedure
Read-only inside the procedure
Can be a variable, literal value, or expression in the calling statement.
2. OUT parameters
Used to return values from the procedure to the calling program
Read-write inside the procedure
Must be a variable in the calling statement to hold the returned value
3. IN OUT parameters
Used for both passing values into and returning values from the procedure
Read-write inside the procedure
Must be a variable in the calling statement
Procedures
Modify Procedures in PL/SQL
To modify an existing procedures in
PL/SQL use the ALTER
PROCEDURE command:
Syntax
Procedures
Example
In this example, we will
modify a procedure in PL/SQL
Procedures
Drop Procedure in PL/SQL
To drop a procedure in PL/SQL use the DROP PROCEDURE command
Syntax:
DROP PROCEDURE procedure_name

PL/SQL DROP PROCEDURE Example


In this example, we will delete a procedure in PL/SQL
Procedures
Advantages of Procedures
• They result in performance improvement of the application.
If a procedure is being called frequently in an application in
a single connection, then the compiled version of the
procedure is delivered.
• They reduce the traffic between the database and the
application since the lengthy statements are already fed into
the database and need not be sent again and again via the
application.
• They add to code reusability, similar to how functions and
methods work in other languages such as C/C++ and Java.
Procedures
Disadvantages of Procedures
• Stored procedures can cause a lot of memory usage. The
database administrator should decide an upper bound as to
how many stored procedures are feasible for a particular
application.
• MySQL does not provide the functionality of debugging the
stored procedures.
Procedures
Example
In this example, we will
modify a procedure in
PL/SQL
Procedures
Trigger
A trigger in a database is a set of SQL statements that are automatically executed
(fired) in response to specific events on a particular table or view. Triggers help
enforce business rules, maintain data integrity, and automate tasks. Events include
INSERT, UPDATE, or DELETE operations on a table or view. Triggers are often used
to validate data, log changes, or automate responses to changes in the database.

Key Features of SQL Triggers:


• Automatic Execution: Triggers fire automatically when the defined event occurs
(e.g., INSERT, UPDATE, DELETE).
• Event-Driven: Triggers are tied to specific events that take place within the
database.
• Table Association: A trigger is linked to a specific table or view, and operates
whenever changes are made to the table’s data.
Trigger
Key Terms
• Create trigger [trigger_name]: Creates or replaces an existing trigger with the
trigger_name.
• [before | after]: This specifies when the trigger will be executed.
• {insert | update | delete}: This specifies the DML operation.
• On [table_name]: This specifies the name of the table associated with the trigger.
• [for each row]: This specifies a row-level trigger, i.e., the trigger will be executed
for each affected row.
• [trigger_body]: This provides the operation to be performed as the trigger is fired
Types of Trigger
We can define 6 types of triggers for each table:
1. AFTER INSERT activated after data is inserted into the
table.
2. AFTER UPDATE: activated after data in the table is
modified.
3. AFTER DELETE: activated after data is deleted/removed
from the table.
4. BEFORE INSERT: activated before data is inserted into
the table.
5. BEFORE UPDATE: activated before data in the table is
modified.
6. BEFORE DELETE: activated before data is
deleted/removed from the table.
Trigger
Applications of Triggers:
a. Audit Trails: Automatically log changes to data.
b. Enforcing Constraints: Prevent invalid data modifications.
c. Synchronous Replication: Maintain a copy of data in another table.
d. Business Logic: Implement rules like updating inventory after a
sale.
Syntax of Trigger
Steps to create a Trigger
1.Understand the Event: Decide on the event that will
activate the trigger (e.g., INSERT, UPDATE,
DELETE).
2.Define the Timing: Specify whether the trigger
should execute BEFORE or AFTER the event.
3.Specify the Table: Choose the table the trigger will
be associated with.
4.Write the Trigger Logic: Define the operations to be
executed when the trigger is activated.
5.Test the Trigger: Ensure it works correctly and does
not cause unintended side effects.
Types of SQL Triggers
1. DDL Triggers : The Data Definition Language (DDL) command events such as
Create_table, Create_view, drop_table, Drop_view, and Alter_table cause the DDL
triggers to be activated.
Types of SQL Triggers
2. DML Triggers: The Data manipulation Language (DML) command events that
begin with Insert, Update, and Delete set off the DML triggers. corresponding to
insert_table, update_view, and delete_table.
Types of SQL Triggers
3. Logon Triggers: These triggers are fired in response to logon events. Logon triggers are
useful for monitoring user sessions or restricting user access to the database. As a result, the
PRINT statement messages and any errors generated by the trigger will all be visible in the SQL
Server error log. Authentication errors prevent logon triggers from being used. These triggers can
be used to track login activity or set a limit on the number of sessions that a given login can have
in order to audit and manage server sessions.
Trigger
Viewing Triggers Using SQL Query: If we are working with many tables across multiple
databases, we can use a simple query to list all available triggers in our SQL Server instance. This
is helpful for tracking and managing triggers, especially when dealing with tables that have
similar names across databases.
Syntax:

Trigger
Key Terms
name: The name of the trigger.
is_instead_of_trigger: Whether the trigger is an INSTEAD OF trigger.
type = ‘TR’: This filters the results to show only triggers .
THANK YOU

You might also like