Mastering MySQL Stored Routines: Procedures, Triggers, and Functions in Action #52
akash-coded
started this conversation in
Tasks
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Exercise Title:
"Mastering MySQL Stored Routines: Procedures, Triggers, and Functions in Action"
Introduction:
This advanced exercise is designed for experienced SQL users to deepen their understanding and skills in creating and using MySQL stored routines, including stored procedures, triggers, and functions. You'll explore different parameter types in stored procedures, various triggers, and the strategic applications of stored functions. The scenarios will be based on the
classicmodelsdatabase, providing a realistic context for applying these concepts.Part 1: Stored Procedures
Scenario: Order Management System Enhancement
Enhance the order management system by creating stored procedures for common operations.
Task 1.1: Creating a New Order
CREATE PROCEDURE,BEGIN ... END,DECLARE, transaction control (START TRANSACTION,COMMIT,ROLLBACK)Task 1.2: Updating Order Status
CREATE PROCEDURE,IF ... THEN,UPDATEPart 2: Triggers
Scenario: Automated Audit Trail
Create triggers to automatically record changes to critical tables for audit purposes.
Task 2.1: Trigger on Order Updates
AFTER UPDATEtrigger on theorderstable to record changes in order status.order_audittable.CREATE TRIGGER,AFTER UPDATE,NEW,OLDTask 2.2: Trigger on New Customers
BEFORE INSERTtrigger on thecustomerstable to validate new customer data.CREATE TRIGGER,BEFORE INSERT,SIGNAL SQLSTATE '45000'Part 3: Stored Functions
Scenario: Sales Reporting Enhancements
Use stored functions to support dynamic sales reporting.
Task 3.1: Calculate Total Order Value
CREATE FUNCTION,RETURNS,SELECT,SUM()Task 3.2: Determine Customer Tier
CREATE FUNCTION,RETURNS,CASEPart 4: Advanced Stored Routine Applications
Scenario: Data Integrity and Business Logic Enforcement
Leverage stored routines to enforce business rules and maintain data integrity across the database.
Task 4.1: Enforcing Minimum Stock Levels
BEFORE INSERTorBEFORE UPDATEtrigger onorderdetailsto ensure that the order quantity does not exceed the available stock.CREATE TRIGGER,BEFORE INSERT,BEFORE UPDATE,SIGNAL SQLSTATETask 4.2: Automated Customer Tier Updates
CREATE PROCEDURE, cursors,UPDATE,CASEPart 5: Combining Stored Routines for Complex Workflows
Scenario: End-to-End Order Processing System
Create an integrated system of stored procedures, triggers, and functions to manage the entire lifecycle of orders, from creation to delivery, including inventory management and customer notifications.
Task 5.1: Automated Order Processing Workflow
notificationstable).CREATE PROCEDURE,BEGIN ... END, transaction control, error handling (DECLARE ... HANDLER),CALLTask 5.2: Inventory Replenishment Alert Trigger
AFTER UPDATEtrigger on theproductstable that checks the new stock level (quantityInStock) after an update.inventory_alertstable with the product ID, current stock level, and timestamp.CREATE TRIGGER,AFTER UPDATE, conditional logic (IF)Part 6: Reporting and Analysis with Stored Functions
Scenario: Dynamic Business Intelligence Reporting
Enhance the reporting capabilities to provide dynamic, on-demand insights into sales performance, product popularity, and customer satisfaction.
Task 6.1: Monthly Sales Performance Function
orderdetailstable that match the given year and month and return the total sales figure.CREATE FUNCTION,RETURNS, date functions (YEAR(),MONTH()),SUM()Task 6.2: Product Popularity Score Function
CREATE FUNCTION,RETURNS,COUNT(),AVG()Deliverables:
For each task:
Part 7: Error Handling in Stored Procedures
Scenario: Robust Order Processing System
Implement error handling in the order processing system to manage exceptions and ensure the system's reliability and accuracy.
Task 7.1: Error Handling in New Order Creation
DECLARE ... HANDLERfor specific error conditions, such as invalid data or insertion failures.ROLLBACKon errors to maintain data integrity.CREATE PROCEDURE,DECLARE ... HANDLER,IF ... THEN,ROLLBACK,START TRANSACTION,COMMITTask 7.2: Graceful Handling of Update Anomalies
CREATE PROCEDURE,DECLARE ... HANDLER,SIGNAL SQLSTATE '45000',SET MESSAGE_TEXT = '...'Part 8: Dynamic SQL in Stored Procedures
Scenario: Flexible Reporting System
Create stored procedures that utilize dynamic SQL to generate flexible reports based on input parameters.
Task 8.1: Dynamic Sales Report Generation
CASEorIFstatements to construct different SQL queries based on the report type.PREPAREandEXECUTEstatements to run the constructed SQL queries.CREATE PROCEDURE, dynamic SQL (PREPARE,EXECUTE),CASETask 8.2: Customizable Product Inventory Report
CREATE PROCEDURE, dynamic SQL (PREPARE,EXECUTE), conditional logic (IF ... THEN)Part 9: Using Triggers for Real-Time Notifications
Scenario: Instant Alert System for Critical Stock Levels
Implement a trigger-based notification system that alerts when product stock levels fall below a critical threshold.
Task 9.1: Trigger for Low Stock Alerts
AFTER UPDATEtrigger on theproductstable to check stock levels after any update operation and generate an alert if the stock is critically low.NEW.stock) after an update.alertstable with details of the product and the current stock level.CREATE TRIGGER,AFTER UPDATE,IF ... THEN,INSERTSubmission Guidelines:
This exercise aims to simulate real-world scenarios where stored routines can optimize database operations, enforce business logic, and enhance data integrity. It's an opportunity to showcase your ability to integrate advanced SQL techniques into practical solutions.
Beta Was this translation helpful? Give feedback.
All reactions