简体   繁体   中英

SQL trigger before delete not properly inserting rows into audit table

I've been staring at this code and looking online to see what's been going on but I can't seem to find what is wrong.

When I run the code below the update trigger works as intended and inserts data into audit table. When I try to test the delete trigger the delete happens as intended but no rows are added to the audit table.

None of my google searching is getting me a concrete answer.

Here is the code for the triggers and testing the triggers:

--14. PART 1: write stored triggers for update and deletion (idk how many I'm assuming 2)
CREATE TRIGGER Before_Person_Update
    BEFORE UPDATE ON Person
    FOR EACH ROW
INSERT INTO PersonHistoryTracker
SET action = 'update',
    personNumber_FK = OLD.personNumber,
    firstName = OLD.firstName,
    lastName = OLD.lastName,
    DOB = OLD.DOB,
    phoneNumber = OLD.phoneNumber,
    email = OLD.email,
    streetNumber = OLD.streetNumber,
    streetName = OLD.streetName,
    city = OLD.city,
    state = OLD.state,
    zipcode = OLD.zipcode,
    isACustomer = OLD.isACustomer,
    isAnEmployee = OLD.isAnEmployee,
    isAPublisherRep = OLD.isAPublisherRep,
    changedat = NOW();


CREATE TRIGGER Before_Person_Delete
BEFORE DELETE ON Person
FOR EACH ROW
INSERT INTO PersonHistoryTracker
SET action = 'delete',
    personNumber_FK = OLD.personNumber,
    firstName = OLD.firstName,
    lastName = OLD.lastName,
    DOB = OLD.DOB,
    phoneNumber = OLD.phoneNumber,
    email = OLD.email,
    streetNumber = OLD.streetNumber,
    streetName = OLD.streetName,
    city = OLD.city,
    state = OLD.state,
    zipcode = OLD.zipcode,
    isACustomer = OLD.isACustomer,
    isAnEmployee = OLD.isAnEmployee,
    isAPublisherRep = OLD.isAPublisherRep,
    changedat = NOW();


SHOW TRIGGERS;


SELECT personNumber, firstName, lastName, streetNumber, streetName, city, state
FROM Person
WHERE personNumber = "007";

UPDATE Person
SET
    lastName = "Johnson"
WHERE personNumber = "007";



SELECT personNumber, firstName, lastName, streetNumber, streetName, city, state
FROM Person
WHERE personNumber = "007";



SELECT * 
FROM Person;
DELETE FROM Person
WHERE personNumber = "006";
SELECT * 
FROM Person;

--14. PART 2: SHOW THAT TRIGGERS WORK AS INTENDED
SELECT * 
FROM PersonHistoryTracker;

Here is the code for the audit table:

-- -- -- -- -- -- -- -- -- PersonHistoryTracker -- -- -- -- -- -- -- -- -- 

-- Creating the PersonHistoryTracker Table
-- Primary Keys: newChangeStartDate, personNumber_FK
-- Foreign Key: personNumber_FK

CREATE TABLE PersonHistoryTracker
(
    changeNum INT DEFAULT 0,
    personNumber_FK VARCHAR(5),
    firstName VARCHAR(30) NOT NULL,
    lastName VARCHAR(30) NOT NULL,
    DOB DATE,
    phoneNumber VARCHAR(10),
    email VARCHAR(50),
    streetNumber VARCHAR(10),
    streetName VARCHAR(30),
    city VARCHAR(30),
    state CHAR(2),
    zipcode CHAR(5),
    isACustomer BOOLEAN,
    isAnEmployee BOOLEAN,
    isAPublisherRep BOOLEAN,
    action VARCHAR(30),
    changedat DATETIME,

    CONSTRAINT PersonHistoryTracker_PK PRIMARY KEY (changeNum, personNumber_FK),

    CONSTRAINT PersonHistoryTracker_FK FOREIGN KEY (personNumber_FK)
    REFERENCES Person(personNumber) ON DELETE CASCADE
    
);

*****UPDATE:

I have tried to create a new delete trigger that follows another table and audits that tables information when deleted both the old and new triggers don't work on delete and I'm not sure why I have seen that it could potentially relate to a bug where cascade operations are not handled properly in mySQL but i'm not sure any help would be appreciated.

This problem was caused by a bug that has been in mySQL for the better part of a decade. Delete triggers that are "triggered" by a foreign key cascade for some reason don't track in MySQL. My solution was to just take out the foreign key constraint in the audit table and "manually import" the foreign key through the trigger statement itself.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM