"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > How to Fix a Syntax Error in MySQL Trigger: Deleting from \"patron_info\" After Deleting from \"patrons\"?

How to Fix a Syntax Error in MySQL Trigger: Deleting from \"patron_info\" After Deleting from \"patrons\"?

Published on 2024-11-09
Browse:703

How to Fix a Syntax Error in MySQL Trigger: Deleting from \

MySQL Trigger: Delete from "patron_info" After Deleting from "patrons"

To establish a trigger that automatically deletes rows from the "patron_info" table when corresponding rows are removed from the "patrons" table:

Syntax Error Correction:

The original trigger syntax error stems from attempting to use both "patrons.id" and "old.id" in the "WHERE" clause. To correctly delete rows from "patron_info" based on the deleted "patron" ID, the trigger should use "old.id":

CREATE TRIGGER log_patron_delete AFTER DELETE on patrons
FOR EACH ROW
BEGIN
  DELETE FROM patron_info
    WHERE patron_info.pid = old.id;
END

Additional Considerations:

  • Ensure a semicolon (";") terminates the DELETE statement within the trigger.
  • Use delimiters when entering the trigger code through the console (e.g., "DELIMITER //").
Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3