MySQL Trigger: Delete From Table AFTER DELETE

后端 未结 3 1468
失恋的感觉
失恋的感觉 2020-12-01 18:17

Scope: Two tables. When a new patron is created, they have some information about them stored into a 2nd table (This was done using a trigger as well, it works as expected)

相关标签:
3条回答
  • 2020-12-01 18:29

    I think there is an error in the trigger code. As you want to delete all rows with the deleted patron ID, you have to use old.id (Otherwise it would delete other IDs)

    Try this as the new trigger:

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

    Dont forget the ";" on the delete query. Also if you are entering the TRIGGER code in the console window, make use of the delimiters also.

    0 讨论(0)
  • 2020-12-01 18:38

    Why not set ON CASCADE DELETE on Foreign Key patron_info.pid?

    0 讨论(0)
  • 2020-12-01 18:41
    create trigger doct_trigger
    after delete on doctor
    for each row
    delete from patient where patient.PrimaryDoctor_SSN=doctor.SSN ;
    
    0 讨论(0)
提交回复
热议问题