同一数据表可否设置多个触发器?MySQL触发器报错及版本兼容问题
Hey there! Let's work through your trigger-related questions one by one, since you're dealing with two different environments (local WampServer MySQL vs. school's MariaDB) and a couple of distinct problems.
1. Fixing Syntax Errors for Multi-Statement Triggers
You mentioned single-statement triggers work fine, but multi-statement ones throw syntax errors—this is a super common gotcha! By default, MySQL/MariaDB uses ; as the statement delimiter, which confuses the compiler when you have multiple ; inside the trigger body. It thinks the trigger ends at the first semicolon, cutting off the rest of your logic.
To fix this, temporarily change the delimiter before defining the trigger, then switch it back afterward. Here's a concrete example:
-- Switch delimiter to // so the trigger body's ; don't break the definition DELIMITER // CREATE TRIGGER parent_after_insert AFTER INSERT ON parent_table FOR EACH ROW BEGIN -- First action: log the insert to an audit table INSERT INTO parent_audit (parent_id, insert_time) VALUES (NEW.id, NOW()); -- Second action: update a related table's counter UPDATE child_summary SET total_parents = total_parents + 1 WHERE category_id = NEW.category_id; END // -- Switch delimiter back to the default ; DELIMITER ;
This tells the database to treat // as the end of the entire trigger definition, so it processes all statements inside the BEGIN...END block correctly.
2. "This version of MariaDB doesn't yet support multiple triggers with the same event"
The error message spells it out clearly—your school's MariaDB version is too old. MariaDB only added support for multiple triggers on the same table, same event (like AFTER INSERT), and same timing starting with version 10.1.2.
Your local WampServer setup is probably running a newer MySQL (which added this feature in 5.7.2) or a more recent MariaDB build, which is why you can create multiple triggers there without hitting this error.
3. Can a Single Table Have Multiple Triggers?
It all depends on your database version:
- MySQL: If you're on 5.7.2 or later, yes—you can create multiple triggers for the same event and timing (e.g., two
AFTER INSERTtriggers on the same table). They’ll execute in the order you created them. - MariaDB: Versions 10.1.2 and above support this. For these newer versions, you can even specify execution order using
FOLLOWSorPRECEDESwhen creating the trigger (e.g.,CREATE TRIGGER trigger2 AFTER INSERT ON table FOLLOWS trigger1). - Older versions of either database don’t support multiple triggers for the same event/timing on a single table—you’ll have to combine all your logic into one single trigger instead.
内容的提问来源于stack exchange,提问作者David Magnum

