Trigger Questions
Trigger Questions
1. Prevent Schema Dropping Create a trigger to prevent dropping the sales schema:
a. Write a DDL trigger to prevent anyone from dropping the sales schema. If a
DROP SCHEMA sale; command is issued, the trigger should raise an error,
ensuring the schema cannot be dropped.
2. Prevent Truncating Tables in production Schema Create a trigger that prevents
truncating tables in the production schema:
a. Implement a trigger to disallow the TRUNCATE TABLE command for any table
within the production schema. You should raise an error if anyone attempts
to truncate tables like categories, brands, products, etc.
3. Track Alterations to Primary Key Constraints Create a trigger that tracks changes
to the primary key on any table and logs the old and new keys:
a. This trigger will monitor any changes to primary key constraints across all
tables in the database, logging details to a custom log table whenever a ALTER
TABLE operation affects primary key constraints.
4. Prevent Modification of a Column if It Contains Data Create a trigger to prevent the
modification (ALTER) of a column if there’s any data present in the table:
a. The trigger will examine if a column (e.g., brand_name in
production.brands) has any data. If it does, prevent the ALTER COLUMN
command from being executed.
These triggers focus on manipulating data, such as INSERT, UPDATE, and DELETE.
a. When a new row is inserted into the sales.orders table, the trigger adds the
relevant product and store into the stocks table, initializing the stock quantity
for that product.
Create a BEFORE DELETE trigger to prevent deletion of a sales.staffs record if that staff
member is managing active stores:
Create an AFTER UPDATE trigger that updates the sales.orders status when a change is
made to the order_status field:
Create an AFTER INSERT trigger on the sales.order_items table that adjusts the
list_price of a product in production.products based on the order item’s quantity:
Create a trigger to prevent inserting an order in sales.orders if the required date is earlier
than today:
Create a trigger that monitors when an order's status changes to "Rejected" (order_status =
3) and updates the production.stocks table to restock the products:
Create a trigger that logs changes to the sales.staffs table (e.g., changes in email, phone,
etc.) into a separate audit table:
a. Every time there’s an update to a staff member’s record (e.g., email or phone
changes), log the old and new values in an audit_staffs table along with the
timestamp and the user who made the change.
1. Prevent Insertion of Products with Zero Price Create a trigger that prevents
inserting a product with a price of zero into the production.products table:
a. Before inserting a new product, the trigger checks if the list_price is zero.
If so, it raises an error to avoid inserting products with no price.
4. Handling Stock Updates After Bulk Order Create a trigger that adjusts stock in
production.stocks based on the quantity of products ordered in
sales.order_items:
a. When an order is placed and items are inserted into order_items, the stock
quantity in production.stocks is updated. If an order contains multiple
products in bulk (e.g., more than 100 units of a product), apply a special
discount or reorder threshold logic.
5. Notify When Large Orders Are Placed Create a trigger that sends an email or logs
an alert if an order exceeds a certain threshold in the sales.order_items table:
a. After inserting or updating an order_item, check if the quantity exceeds a
threshold (e.g., 50 units). If it does, log a notification message or trigger an
email to alert the staff or managers about the large order.
7. Tracking Changes to Product Prices Create an AFTER UPDATE trigger that logs price
changes in the production.products table:
a. Whenever a product’s list_price is updated, the trigger will create a record
in an audit table (price_changes_log) to log the old price, new price, and
date of change.
8. Update Customer Order History Create a trigger that updates a
customer_order_history table every time an order is inserted:
a. After inserting a new order into the sales.orders table, the trigger adds a
record to a customer_order_history table with the customer_id,
order_id, order_date, and order_status.
9. Enforce Foreign Key Integrity Across Multiple Tables Create a trigger that checks
for referential integrity before deleting a record from sales.orders:
a. Before deleting a record from sales.orders, the trigger ensures there are no
related records in the sales.order_items table. If related order_items
exist, the deletion is prevented.
10. Implement Discount Based on Order Quantity Create a trigger that applies a
discount on the total order price based on the total quantity ordered in
sales.order_items:
a. When inserting into the order_items table, the trigger calculates the total
quantity of products in the order_id. If the total quantity exceeds a
threshold (e.g., 100), a global discount (e.g., 5%) is applied to all items in that
order.
11. Refund on Rejected Orders Create a trigger to refund a customer when an order is
rejected:
a. When an order’s order_status is updated to “Rejected” (status = 3), the
trigger checks if the order has been paid for. If it has, it inserts a record into a
refunds table with the refund amount and customer details.
12. Audit Deleted Data Create a trigger that logs deletions from the sales.orders
table:
a. Before deleting an order, the trigger copies the deleted row’s information into
an order_deletions_log table, along with the deletion time and the user
who deleted the order.
Mixed DDL and DML Trigger Questions (Advanced)
1. Prevent Deleting Customers with Open Orders Create a BEFORE DELETE trigger
that prevents the deletion of a sales.customers record if they have open orders:
a. Before deleting a customer, the trigger checks if there are any sales.orders
with an order status of 1 (Pending) or 2 (Processing) associated with that
customer. If so, the deletion is blocked.
5. Dynamic Pricing Based on Store Location Create a trigger that adjusts the
list_price of products based on the store's location:
a. When a product is added to an order in the sales.order_items table, the
trigger checks the store’s location (e.g., city, state) from the sales.stores
table and adjusts the list_price accordingly.