Unguided Exercises - Triggers - Usage of triggers in MySQL using the classicmodels database #46
Replies: 1 comment
-
|
DELIMITER // DELIMITER // END; |
Beta Was this translation helpful? Give feedback.
-
|
DELIMITER // DELIMITER // END; |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Certainly! Triggers in MySQL can be extremely useful for automating actions that should take place when data is modified. Below are some advanced exercises based on the
classicmodelsdatabase, intended to challenge experienced data engineers on the myriad usages of triggers.Exercise 1: Audit Table for
customersContext and Problem Statement:
Create an
AFTER UPDATEtrigger on thecustomerstable that logs changes to a separateCustomerAudittable. The audit table should capture who made the change and what data was altered.Trigger Type:
Columns to Log:
customerNamephonesalesRepEmployeeNumberExercise 2: Inventory Control for
productsContext and Problem Statement:
Create an
AFTER INSERTtrigger on theorderdetailstable to update a separateInventorytable. If the quantity in the inventory falls below a minimum threshold, mark the product as needing restock.Trigger Type:
Actions:
Exercise 3: Automatic Discounts on
ordersContext and Problem Statement:
For orders above a certain total value, an automatic discount should be applied. Create an
AFTER INSERTtrigger on theorderstable that modifies the order to include a discount if the total amount is above $10,000.Trigger Type:
Actions:
Exercise 4: Employee Performance Metrics
Context and Problem Statement:
When an employee makes a sale (an entry in the
orderstable), anAFTER INSERTtrigger should update the employee's performance metrics in anEmployeeMetricstable.Trigger Type:
Actions:
Exercise 5: Cascade Delete for
productsandorderdetailsContext and Problem Statement:
When a product is deleted from the
productstable, ensure that all related entries in theorderdetailstable are also removed to maintain data integrity.Trigger Type:
Actions:
orderdetailsThese exercises cover various real-world scenarios where triggers can come in handy to automate processes and maintain data integrity.
Beta Was this translation helpful? Give feedback.
All reactions