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

Trigger Questions

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

Trigger Questions

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

DDL Trigger Questions

1. Basic DDL Trigger


a. Create a DDL trigger that logs the table name, date, and user who created it
into a log table whenever a new table is created in the production or sales
schema.
2. DDL Trigger for DROP Statements
a. Write a DDL trigger that prevents the DROP command on the sales.orders
table and raises an error if the drop is attempted.
3. Advanced DDL Trigger for Schema Modification
a. Write a trigger to detect changes to columns that are part of foreign key
relationships (e.g., store_id, staff_id), preventing dropping them if they
are referenced by other tables.
4. DDL Trigger to Track ALTER Commands
a. Whenever an ALTER operation (like adding/removing a column) is executed on
a table, log the event in a custom log table with details such as table name,
column(s) modified, and date/time.

DDL Trigger Questions (Advanced)

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.

DML Trigger Questions

These triggers focus on manipulating data, such as INSERT, UPDATE, and DELETE.

1. Basic DML Trigger for INSERT

Create an AFTER INSERT trigger to automatically add a record to the production.stocks


table whenever a new order is placed:

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.

2. DML Trigger for DELETE

Create a BEFORE DELETE trigger to prevent deletion of a sales.staffs record if that staff
member is managing active stores:

a. If an employee has a non-null manager_id and is managing an active store


(where active = 1), prevent the deletion of that employee.

3. Intermediate DML Trigger for UPDATE

Create an AFTER UPDATE trigger that updates the sales.orders status when a change is
made to the order_status field:

a. After an update to the order_status of an order in the sales.orders table,


check if the status has changed to 4 (Completed), and if so, update the
shipped_date to the current date.

4. Advanced DML Trigger for Complex Business Logic


Create a trigger to update sales.stores inventory when an order item is deleted from the
sales.order_items table:

a. If an ORDER_ITEM is deleted, adjust the quantity in the production.stocks


table by adding back the quantity of the product that was just removed from
the order.

5. Advanced DML Trigger for Nested Updates

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:

a. If the quantity in an order_item is above a certain threshold, apply a


discount to the product by modifying the list_price in
production.products.

6. DML Trigger for Handling Discounts

Create a trigger on sales.order_items that automatically applies a discount if the quantity


of a product exceeds a certain threshold (e.g., 10 units):

a. When an item is inserted into sales.order_items, if the quantity exceeds


10, set the discount for that item to a fixed percentage (e.g., 5%).

7. Trigger for Preventing Invalid Data Insertion

Create a trigger to prevent inserting an order in sales.orders if the required date is earlier
than today:

a. If someone attempts to insert an order with a required_date before today’s


date, the trigger raises an error.

8. DML Trigger for Cascading Updates on Stock Quantity


Create a trigger that updates the stock quantity in the production.stocks table whenever
an order_item is updated in the sales.order_items table:

a. Whenever the quantity or list_price of an order item is updated, adjust


the stocks table’s quantity field based on the difference.

9. Complex DML Trigger for Inventory Management

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:

a. When an order is rejected, the corresponding products should have their


stock quantities increased back in the stocks table based on the quantity in
the rejected order.

10. Complex Trigger for Auditing Updates

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.

DML Trigger Questions (Advanced)

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.

2. Update Order Status Based on Payment Confirmation Create an AFTER INSERT


trigger on a hypothetical payments table to update the status of the corresponding
order:
a. When a new record is inserted into a payments table (assuming it tracks
payments made for orders), the trigger updates the order_status of the
corresponding order in the sales.orders table to “Processing” (status 2)
once the payment is confirmed.

3. Tracking Sales Revenue per Product Create a trigger that updates a


sales_revenue field in production.products every time a sale occurs:
a. After an INSERT into the sales.order_items table, calculate the total price
for that item (quantity * (list_price - discount)) and update the
sales_revenue column in the production.products table to reflect the
new sales revenue.

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.

6. Prevent Negative Stock Levels Create a trigger on the production.stocks table


to prevent stock levels from going negative:
a. Before updating the stock quantity, the trigger checks if the new stock quantity
after the update would go below zero. If it would, raise an error, preventing the
update from occurring.

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.

2. Cascade Stock Adjustments on Order Cancellations Create an AFTER DELETE


trigger on sales.orders that adjusts inventory when an order is canceled (status =
3):
a. If an order is canceled, the trigger ensures that the corresponding products in
production.stocks have their quantities updated (restocking the products).

3. Enforce Product Category Constraints Create a trigger that ensures products


cannot be added to production.products unless the associated category exists in
production.categories:
a. Before inserting a product, the trigger verifies that the category_id exists in
production.categories. If not, the insertion is blocked.

4. Automatically Populate Staff’s Default Store Create a trigger to automatically


assign a default store to new staff when their store_id is not specified:
a. When a new staff member is added to the sales.staffs table, if the
store_id is null, the trigger assigns them to a default store (e.g., store with
store_id = 1).

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.

You might also like