0
0
MySQLquery~20 mins

BEFORE INSERT triggers in MySQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
BEFORE INSERT Trigger Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Output of BEFORE INSERT trigger modifying data

Consider a table users with columns id (auto-increment), username, and created_at. A BEFORE INSERT trigger sets created_at to the current date if not provided.

What will be the created_at value after running this insert?

MySQL
CREATE TABLE users (id INT AUTO_INCREMENT PRIMARY KEY, username VARCHAR(50), created_at DATE);
DELIMITER $$
CREATE TRIGGER before_insert_users
BEFORE INSERT ON users
FOR EACH ROW
BEGIN
  IF NEW.created_at IS NULL THEN
    SET NEW.created_at = CURDATE();
  END IF;
END$$
DELIMITER ;

INSERT INTO users (username) VALUES ('alice');

SELECT created_at FROM users WHERE username = 'alice';
AAn error occurs because created_at is not nullable
BNULL because no value was provided explicitly
CThe current date on the day of insertion (e.g., '2024-06-15')
DThe default date '0000-00-00'
Attempts:
2 left
💡 Hint

Think about what the trigger does before the row is inserted.

🧠 Conceptual
intermediate
1:30remaining
Purpose of BEFORE INSERT triggers

What is the main purpose of a BEFORE INSERT trigger in a database?

ATo automatically delete rows after insertion
BTo modify or validate data before it is inserted into the table
CTo create indexes on the table
DTo backup the database before insertion
Attempts:
2 left
💡 Hint

Think about when the trigger runs in relation to the insert operation.

📝 Syntax
advanced
2:30remaining
Identify the syntax error in BEFORE INSERT trigger

Which option contains a syntax error in the BEFORE INSERT trigger definition?

MySQL
CREATE TABLE products (id INT PRIMARY KEY, price DECIMAL(10,2));
ACREATE TRIGGER trg_before_insert_products BEFORE INSERT ON products FOR EACH ROW BEGIN SET NEW.price = NEW.price - 2; END;
BCREATE TRIGGER trg_before_insert_products BEFORE INSERT ON products FOR EACH ROW BEGIN SET NEW.price = NEW.price + 5; END;
CCREATE TRIGGER trg_before_insert_products BEFORE INSERT ON products FOR EACH ROW BEGIN SET NEW.price = NEW.price * 1.1; END;
DCREATE TRIGGER trg_before_insert_products BEFORE INSERT ON products FOR EACH ROW BEGIN SET price = price * 1.1; END;
Attempts:
2 left
💡 Hint

Remember how to refer to the new row's columns inside triggers.

🔧 Debug
advanced
3:00remaining
Why does this BEFORE INSERT trigger cause an error?

Given the following trigger, why does inserting a row cause an error?

MySQL
CREATE TABLE orders (id INT PRIMARY KEY, quantity INT);
DELIMITER $$
CREATE TRIGGER trg_before_insert_orders
BEFORE INSERT ON orders
FOR EACH ROW
BEGIN
  IF NEW.quantity < 0 THEN
    SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'Quantity cannot be negative';
  END IF;
END$$
DELIMITER ;

INSERT INTO orders (id, quantity) VALUES (1, -5);
AThe trigger raises a custom error because quantity is negative
BThe trigger syntax is invalid and causes a syntax error
CThe insert succeeds and the trigger does nothing
DThe trigger causes a deadlock error
Attempts:
2 left
💡 Hint

Look at the SIGNAL statement inside the trigger.

optimization
expert
3:00remaining
Optimizing BEFORE INSERT trigger for bulk inserts

You have a BEFORE INSERT trigger that sets a timestamp column to the current time if NULL. You notice bulk inserts are slow. Which optimization can improve performance?

AUse a DEFAULT value for the timestamp column instead of a trigger
BAdd an index on the timestamp column to speed up inserts
CChange the trigger to AFTER INSERT to reduce overhead
DRemove the trigger and set the timestamp in the application before insert
Attempts:
2 left
💡 Hint

Think about how to avoid running code for every row during insert.