A BEFORE INSERT trigger is a special procedure that runs automatically before a new row is added to a table. It lets you check or change data before it is saved.
Click to reveal answer
beginner
When exactly does a BEFORE INSERT trigger execute?
It executes right before the new data is inserted into the table, allowing you to modify or validate the data before it is saved.
Click to reveal answer
intermediate
Can a BEFORE INSERT trigger modify the data being inserted?
Yes, it can change the values of the new row before it is saved, for example, to add timestamps or enforce rules.
Click to reveal answer
beginner
How do you access the new row's data inside a BEFORE INSERT trigger?
You use the keyword NEW followed by the column name, like NEW.column_name, to read or change the value.
Click to reveal answer
intermediate
What happens if a BEFORE INSERT trigger signals an error?
The insert operation is stopped, and the new row is not added to the table. This helps keep data clean and valid.
Click to reveal answer
When does a BEFORE INSERT trigger run in MySQL?
ABefore a new row is inserted
BAfter a new row is inserted
CWhen a row is updated
DWhen a row is deleted
✗ Incorrect
A BEFORE INSERT trigger runs right before a new row is added to the table.
How do you refer to the new row's column 'age' inside a BEFORE INSERT trigger?
AOLD.age
BNEW.age
Cage
DNEW['age']
✗ Incorrect
You use NEW.age to access or modify the new row's 'age' value.
What happens if a BEFORE INSERT trigger raises an error?
AThe insert is canceled
BThe insert continues anyway
CThe trigger is ignored
DThe row is updated instead
✗ Incorrect
If the trigger signals an error, the insert is stopped and the row is not added.
Which of these is a valid use of a BEFORE INSERT trigger?
ADelete rows from another table
BUpdate existing rows after insert
CAutomatically set a creation timestamp
DDrop the table
✗ Incorrect
BEFORE INSERT triggers are often used to set or modify data before saving, like timestamps.
Can a BEFORE INSERT trigger modify multiple columns of the new row?
AOnly if the table has a primary key
BNo, it can only read data
CNo, it can only modify one column
DYes, it can modify any columns before insert
✗ Incorrect
BEFORE INSERT triggers can change any columns of the new row before it is saved.
Explain what a BEFORE INSERT trigger does and give an example of when you might use it.
Think about how you can change data before saving it.
You got /3 concepts.
Describe how to access and change the data of the new row inside a BEFORE INSERT trigger.
Remember the special keyword for new row data.
You got /3 concepts.
Practice
(1/5)
1. What is the main purpose of a BEFORE INSERT trigger in MySQL?
easy
A. To select data from another table before insertion
B. To delete rows after they are inserted
C. To modify or validate data before it is inserted into a table
D. To create a new table before inserting data
Solution
Step 1: Understand the trigger timing
A BEFORE INSERT trigger runs before the new row is saved to the table.
Step 2: Identify the trigger's purpose
It allows you to check or change the data before it is inserted, ensuring data integrity or applying rules.
Final Answer:
To modify or validate data before it is inserted into a table -> Option C
Quick Check:
BEFORE INSERT triggers run before insert = A [OK]
Hint: BEFORE INSERT triggers act before saving data [OK]
Common Mistakes:
Confusing BEFORE INSERT with AFTER INSERT triggers
Thinking triggers create or delete tables
Assuming triggers run after data is saved
2. Which of the following is the correct syntax to create a BEFORE INSERT trigger on a table named users?
easy
A. CREATE TRIGGER before_insert_users AFTER INSERT ON users FOR EACH ROW SET NEW.name = UPPER(NEW.name);
B. CREATE TRIGGER before_insert_users BEFORE INSERT ON users FOR EACH ROW SET NEW.name = UPPER(NEW.name);
C. CREATE TRIGGER before_insert_users BEFORE UPDATE ON users FOR EACH ROW SET NEW.name = UPPER(NEW.name);
D. CREATE TRIGGER before_insert_users BEFORE INSERT ON users FOR EACH STATEMENT SET NEW.name = UPPER(NEW.name);
Solution
Step 1: Identify correct trigger timing and event
The question asks for a BEFORE INSERT trigger, so timing is BEFORE and event is INSERT.
Step 2: Check syntax details
Triggers must be defined FOR EACH ROW to access NEW values. CREATE TRIGGER before_insert_users BEFORE INSERT ON users FOR EACH ROW SET NEW.name = UPPER(NEW.name); matches this exactly.
Final Answer:
CREATE TRIGGER before_insert_users BEFORE INSERT ON users FOR EACH ROW SET NEW.name = UPPER(NEW.name); -> Option B
Quick Check:
BEFORE INSERT + FOR EACH ROW = D [OK]
Hint: BEFORE INSERT triggers use FOR EACH ROW, not STATEMENT [OK]
Common Mistakes:
Using AFTER instead of BEFORE
Using BEFORE UPDATE instead of BEFORE INSERT
Using FOR EACH STATEMENT instead of FOR EACH ROW
3. Given this trigger and insert statement:
CREATE TRIGGER check_age BEFORE INSERT ON employees FOR EACH ROW BEGIN IF NEW.age < 18 THEN SET NEW.age = 18; END IF; END;
What will be the age value after running:
INSERT INTO employees (name, age) VALUES ('Alice', 16);
medium
A. 18
B. NULL
C. 16
D. Error: age cannot be less than 18
Solution
Step 1: Understand trigger logic
The trigger checks if NEW.age is less than 18, and if so, sets NEW.age to 18 before insert.
Step 2: Apply trigger to inserted data
The insert tries to add age 16, which is less than 18, so trigger changes it to 18.
Final Answer:
18 -> Option A
Quick Check:
Trigger sets age to 18 if less than 18 = 18 [OK]
Hint: BEFORE INSERT can modify NEW values before saving [OK]
Common Mistakes:
Assuming original value 16 is saved
Expecting an error instead of modification
Confusing NEW with OLD values
4. Consider this trigger code:
CREATE TRIGGER trg BEFORE INSERT ON orders FOR EACH ROW BEGIN IF NEW.quantity < 1 THEN SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'Quantity must be positive'; END IF; END;
What error will occur if you run:
INSERT INTO orders (product_id, quantity) VALUES (101, 0);
medium
A. Runtime error with message 'Quantity must be positive'
B. No error, row inserted with quantity 0
C. Syntax error in trigger definition
D. Trigger ignored, default quantity used
Solution
Step 1: Analyze trigger condition
The trigger checks if NEW.quantity is less than 1, which is true for 0.
Step 2: Understand SIGNAL statement effect
The SIGNAL statement raises a runtime error with the given message, stopping the insert.
Final Answer:
Runtime error with message 'Quantity must be positive' -> Option A
Quick Check:
SIGNAL raises error if condition met = B [OK]
Hint: SIGNAL in BEFORE INSERT stops invalid inserts [OK]
Common Mistakes:
Thinking insert succeeds with invalid data
Confusing SIGNAL with warning
Assuming syntax error instead of runtime error
5. You want to ensure that every new row inserted into the products table has a unique SKU code in uppercase. Which BEFORE INSERT trigger code correctly enforces this?
hard
A. CREATE TRIGGER sku_uppercase BEFORE UPDATE ON products FOR EACH ROW BEGIN SET NEW.sku = UPPER(NEW.sku); END;
B. CREATE TRIGGER sku_uppercase AFTER INSERT ON products FOR EACH ROW BEGIN SET NEW.sku = UPPER(NEW.sku); END;
C. CREATE TRIGGER sku_uppercase BEFORE INSERT ON products FOR EACH ROW BEGIN SET NEW.sku = LOWER(NEW.sku); END;
D. CREATE TRIGGER sku_uppercase BEFORE INSERT ON products FOR EACH ROW BEGIN SET NEW.sku = UPPER(NEW.sku); IF EXISTS (SELECT 1 FROM products WHERE sku = NEW.sku) THEN SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'Duplicate SKU'; END IF; END;
Solution
Step 1: Ensure SKU is uppercase before insert
CREATE TRIGGER sku_uppercase BEFORE INSERT ON products FOR EACH ROW BEGIN SET NEW.sku = UPPER(NEW.sku); IF EXISTS (SELECT 1 FROM products WHERE sku = NEW.sku) THEN SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'Duplicate SKU'; END IF; END; uses SET NEW.sku = UPPER(NEW.sku) in a BEFORE INSERT trigger, correctly modifying the new row.
Step 2: Check for duplicate SKU before insert
CREATE TRIGGER sku_uppercase BEFORE INSERT ON products FOR EACH ROW BEGIN SET NEW.sku = UPPER(NEW.sku); IF EXISTS (SELECT 1 FROM products WHERE sku = NEW.sku) THEN SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'Duplicate SKU'; END IF; END; checks if the SKU already exists and raises an error to prevent duplicates.
Step 3: Verify other options
CREATE TRIGGER sku_uppercase AFTER INSERT ON products FOR EACH ROW BEGIN SET NEW.sku = UPPER(NEW.sku); END; uses AFTER INSERT, too late to modify NEW. CREATE TRIGGER sku_uppercase BEFORE INSERT ON products FOR EACH ROW BEGIN SET NEW.sku = LOWER(NEW.sku); END; uses LOWER instead of UPPER. CREATE TRIGGER sku_uppercase BEFORE UPDATE ON products FOR EACH ROW BEGIN SET NEW.sku = UPPER(NEW.sku); END; triggers on UPDATE, not INSERT.
Final Answer:
CREATE TRIGGER sku_uppercase BEFORE INSERT ON products FOR EACH ROW BEGIN SET NEW.sku = UPPER(NEW.sku); IF EXISTS (SELECT 1 FROM products WHERE sku = NEW.sku) THEN SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'Duplicate SKU'; END IF; END; -> Option D
Quick Check:
Modify NEW and check duplicates BEFORE INSERT = A [OK]
Hint: Use BEFORE INSERT to modify and validate NEW row [OK]
Common Mistakes:
Using AFTER INSERT to modify NEW values
Not checking duplicates before insert
Modifying SKU to lowercase instead of uppercase
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.