更新前如何工作触发mysql

时间:2015-10-17 05:40:00

标签: mysql

我在更新触发器之前创建但是它不起作用请帮助

CREATE TABLE `employees` (
  `employeeNumber` int(11) NOT NULL,
  `lastName` varchar(50) NOT NULL,
  `firstName` varchar(50) NOT NULL,
  `extension` varchar(10) NOT NULL,
  `email` varchar(100) NOT NULL,
  `officeCode` varchar(10) NOT NULL,
  `reportsTo` int(11) DEFAULT NULL,
  `jobTitle` varchar(50) NOT NULL,
  PRIMARY KEY (`employeeNumber`),
  KEY `reportsTo` (`reportsTo`),
  KEY `officeCode` (`officeCode`),
  CONSTRAINT `employees_ibfk_2` FOREIGN KEY (`officeCode`) REFERENCES `offices` (`officeCode`),
  CONSTRAINT `employees_ibfk_1` FOREIGN KEY (`reportsTo`) REFERENCES `employees` (`employeeNumber`)
)

这是我的employees_audit

CREATE TABLE employees_audit ( 
    id int(11) NOT NULL AUTO_INCREMENT, 
    employeeNumber int(11) NOT NULL, 
    lastname varchar(50) NOT NULL, 
    changedon datetime DEFAULT NULL, 
    action varchar(50) DEFAULT NULL, 
    PRIMARY KEY (id) 
 )

我在更新触发器之前创建了

DELIMITER $$
CREATE TRIGGER before_employee_update 
    BEFORE UPDATE ON employees
    FOR EACH ROW BEGIN

    INSERT INTO employees_audit
    SET action = 'update',
     employeeNumber = OLD.employeeNumber,
        lastname = OLD.lastname,
        changedon = NOW(); 
END$$
DELIMITER ;

0 个答案:

没有答案