Trigger that inserts row into log table on insert or delete in a persons table compiled with errors

后端 未结 1 1473
无人及你
无人及你 2021-01-27 13:01

I have a persons table with a name and id as well as a logs table with attributes who and what. I want to insert into the logs table when I delete or insert into the persons tab

相关标签:
1条回答
  • 2021-01-27 13:26

    Since you didn't post the error, I have to guess. My guess is that the problem is that name is not a valid identifier in this context. You need to reference either :new.name or :old.name. :old.name will be NULL on an insert while :new.name will be NULL on a delete so I'm assuming you want something like

    CREATE OR REPLACE TRIGGER add_del
      BEFORE INSERT OR DELETE ON persons
      FOR EACH ROW
    BEGIN
      IF INSERTING THEN
        INSERT INTO logs (who, what) VALUES (:new.name, 'Insert into persons');
      ELSE
        INSERT INTO logs (who, what) VALUES (:old.name, 'Delete from persons');
      END IF;
    END;
    
    0 讨论(0)
提交回复
热议问题