Module-10.-Lesson-Proper
Module-10.-Lesson-Proper
I. LESSON PROPER
Purpose
• Encapsulate Logic: Group SQL statements and business rules into a single callable unit.
• Improve Performance: Reduce network traffic and increase execution speed by minimizing the
number of calls made to the database.
• Enhance Security: Control access to data and operations, allowing users to execute procedures
without direct access to the underlying tables.
• Ensure Consistency: Standardize database operations and enforce business rules consistently.
5. Dependency Issues
• Changes in database schema can affect stored procedures, leading to potential failures.
Syntax Overview
Example
Let's look at a simple example that inserts a new record into a "Customers" table:
Execution
For the GetCustomerCount procedure with an output parameter, the execution would look like this (syntax
may vary slightly based on the RDBMS):
Page 3 of 12
Advance Database System
Stored Procedure and Triggers
Error Handling
Error handling in stored procedures is important to ensure that exceptions are managed gracefully.
Many RDBMSs support constructs for error handling.
You can use DECLARE ... HANDLER to define how to handle errors:
In this example:
• If an error occurs during the INSERT, the procedure will catch it and return a message instead of
failing outright.
In some RDBMS, you can use try-catch blocks for error handling.
Page 4 of 12
Advance Database System
Stored Procedure and Triggers
To create a stored procedure, use the CREATE PROCEDURE statement. Here’s the basic syntax:
Example: Let’s create a simple stored procedure that adds a new customer:
Using Parameters
Control Flow
Stored procedures can include control flow statements, such as IF, CASE, and loops (WHILE, REPEAT,
FOR). This allows for more complex logic.
To modify an existing stored procedure, use the ALTER PROCEDURE statement. Note that the
exact syntax can depend on the RDBMS you are using. Here’s a general approach:
Example:
To remove a stored procedure from the database, use the DROP PROCEDURE statement:
Example:
Page 7 of 12
Advance Database System
Stored Procedure and Triggers
Introduction to Triggers
A trigger is a set of SQL statements automatically executed (or fired) in response to specific events
on a particular table or view in a database. Triggers help maintain data integrity and enforce business rules.
Types of Triggers
1. BEFORE Trigger
A BEFORE Trigger is executed before an insert, update, or delete operation on a table. It is useful
for validating or modifying the data before it is committed to the database.
2. AFTER Trigger
An AFTER Trigger is executed after an insert, update, or delete operation. This is useful for actions
that should happen after the main operation, such as logging or cascading changes.
3. INSTEAD OF Trigger
An INSTEAD OF Trigger replaces the standard action for an insert, update, or delete operation. This is
commonly used with views, allowing for customized handling of actions.
Page 10 of 12
Advance Database System
Stored Procedure and Triggers
2. Data Validation: Triggers can enforce data integrity by validating data before it’s inserted or updated in
the database.
3. Cascading Actions: Triggers can automate cascading changes to related tables when a record is
modified or deleted.
Page 11 of 12
Advance Database System
Stored Procedure and Triggers
4. Enforcing Business Rules: Triggers can enforce specific business logic and rules directly in the
database.
Example: Prevent a user from being assigned to more than one department.
Advantages of Triggers
1. Automatic Execution:
➢ Triggers are automatically executed in response to specific events, ensuring that business
logic is applied consistently without manual intervention.
2. Data Integrity:
➢ They help maintain data integrity by enforcing rules and constraints, preventing invalid data
from being inserted or updated.
➢ Triggers can be used for auditing changes to the database, providing a historical record of
actions taken on data.
Page 12 of 12
Advance Database System
Stored Procedure and Triggers
4. Cascading Actions:
➢ They can automate cascading updates or deletes across related tables, ensuring referential
integrity without additional application logic.
➢ Business rules can be implemented directly in the database, making them easier to manage
and enforce consistently across multiple applications.
6. Performance Optimization:
Disadvantages of Triggers
1. Complexity:
➢ Triggers can make the database logic more complex and harder to understand, especially if
there are multiple triggers on the same table.
2. Debugging Difficulty:
➢ Triggers can be difficult to debug because they operate behind the scenes. Errors may not
surface immediately and can lead to unexpected behavior.
3. Performance Overhead:
4. Unintended Consequences:
➢ Triggers can lead to unintended side effects, such as recursive calls if a trigger modifies
data that causes the same trigger to fire again.
5. Portability Issues:
➢ Different database systems may have varying support for triggers, leading to potential
portability issues if applications need to be migrated.
6. Hidden Logic:
➢ Business logic implemented in triggers may not be immediately visible to developers and
users, leading to confusion about where certain rules are enforced.