我有一个没有错误安装的触发器,但没有进行预期的更新和插入。
一点背景。有四个表:订单,客户,customer_purchases和uflex_users。这是最后3个表格。
CREATE TABLE `customers` (
`customerID` int(7) NOT NULL,
`name` varchar(255) NOT NULL,
`email` varchar(50) DEFAULT NULL,
`company` varchar(255) DEFAULT NULL,
`billingAddress` varchar(255) DEFAULT NULL,
`billingAddress1` varchar(255) DEFAULT NULL,
`billingCity` varchar(100) DEFAULT NULL,
`billingState` varchar(3) DEFAULT NULL,
`billingZip` varchar(10) DEFAULT NULL,
`billingCountry` varchar(100) DEFAULT NULL,
`billingPhone` varchar(50) DEFAULT NULL,
`shippingName` varchar(255) DEFAULT NULL,
`shippingCompany` varchar(100) DEFAULT NULL,
`shippingAddress` varchar(255) DEFAULT NULL,
`shippingAddress1` varchar(255) DEFAULT NULL,
`shippingCity` varchar(100) DEFAULT NULL,
`shippingState` varchar(100) DEFAULT NULL,
`shippingZip` varchar(10) DEFAULT NULL,
`shippingCountry` varchar(100) DEFAULT NULL,
`shippingPhone` varchar(50) DEFAULT NULL,
PRIMARY KEY (`customerID`),
CONSTRAINT `CUSTOMERS_ibfk_1` FOREIGN KEY (`customerID`) REFERENCES `uFlex_users` (`user_id`) ON DELETE CASCADE,
FOREIGN KEY (`email`) REFERENCES `uFlex_users` (email)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
CREATE TABLE `customer_purchases` (
`customerID` int(7) NOT NULL,
`orderNumber` int(11) NOT NULL DEFAULT '0',
PRIMARY KEY (`customerID`),
CONSTRAINT `CUSTOMER_PURCHASES_ibfk_1` FOREIGN KEY (`customerID`) REFERENCES `uFlex_users` (`user_id`) ON DELETE CASCADE,
FOREIGN KEY (`orderNumber`) REFERENCES `orders` (`orderNumber`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
CREATE TABLE IF NOT EXISTS `uFlex_users` (
`user_id` int(7) NOT NULL AUTO_INCREMENT,
`username` varchar(15) NOT NULL,
`password` varchar(35) NOT NULL,
`email` varchar(35) NOT NULL,
`activated` int(1) NOT NULL DEFAULT '0',
`confirmation` varchar(35) NOT NULL,
`reg_date` int(11) NOT NULL,
`last_login` int(11) NOT NULL DEFAULT '0',
`group_id` int(2) NOT NULL DEFAULT '1',
PRIMARY KEY (`user_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
有一个触发器在插入uflex_users之后将CustomerID和电子邮件插入客户,并且工作正常。
这是有问题的触发器。
DELIMITER //
CREATE TRIGGER sstage_ord.ORDERS_TO_CUSTOMER_TABLES_CHECK_INSERT AFTER INSERT ON sstage_ord.orders
FOR EACH ROW BEGIN
IF NEW.name != '' AND
NEW.email != '' AND
NEW.address1 != '' AND
NEW.city != '' AND
NEW.state != '' AND
NEW.zip != '' AND
NEW.phone != '' AND
NEW.country != '' AND
NEW.shipAddress1 != '' AND
NEW.shipCity != '' AND
NEW.shipState != '' AND
NEW.shipZip != '' AND
NEW.shipPhone != '' AND
NEW.shipCountry != ''
THEN
SET @myEmail = (SELECT email FROM sstage_ord.customers WHERE email = NEW.email);
SET @myCity = (SELECT billingCity FROM sstage_ord.customers WHERE email = NEW.email);
IF @myEmail != NULL && @myCity IS NULL
THEN
UPDATE sstage_ord.customers
SET
name = NEW.name, company = NEW.company, billingAddress = NEW.address1, billingAddress1 = NEW.address2, billingCity = NEW.city,
billingState = NEW.state, billingZip = NEW.zip, billingCountry = NEW.country, billingPhone = NEW.phone, shippingName = NEW.shipName,
shippingCompany = NEW.shipCompany, shippingAddress = NEW.shipAddress1, shippingAddress1 = NEW.address2, shippingCity = NEW.shipCity,
shippingState = NEW.shipState, shippingZip = NEW.shipZip, shippingCountry = NEW.shipCountry, shippingPhone = NEW.shipPhone
WHERE
email = NEW.email;
END IF;
SET @myID = (SELECT user_id FROM uFlex_users WHERE email = NEW.email);
IF @myID != NULL
THEN
INSERT INTO sstage_ord.customer_purchases (customerID, orderNumber)
VALUES (@myID, NEW.orderNumber);
END IF;
END IF;
END//
DELIMITER ;
有没有人看到这个问题?
提前致谢!
答案 0 :(得分:0)
解决!
这是工作版本:
DROP TRIGGER IF EXISTS sstage_ord.ORDERS_TO_CUSTOMER_TABLES_CHECK_INSERT;
DELIMITER //
CREATE TRIGGER sstage_ord.ORDERS_TO_CUSTOMER_TABLES_CHECK_INSERT AFTER INSERT ON sstage_ord.orders
FOR EACH ROW
BEGIN
DECLARE email_field varchar(50);
DECLARE city_field varchar(50);
DECLARE id_field integer;
IF NEW.orderNumber != '' AND
NEW.name != '' AND
NEW.email != '' AND
NEW.address1 != '' AND
NEW.city != '' AND
NEW.state != '' AND
NEW.zip != '' AND
NEW.phone != '' AND
NEW.country != '' AND
NEW.shipAddress1 != '' AND
NEW.shipCity != '' AND
NEW.shipState != '' AND
NEW.shipZip != '' AND
NEW.shipPhone != '' AND
NEW.shipCountry != ''
THEN
SELECT email FROM sstage_ord.customers WHERE email = NEW.email INTO email_field;
SELECT billingCity FROM sstage_ord.customers WHERE email = NEW.email INTO city_field;
IF email_field IS NOT NULL AND city_field IS NULL THEN
UPDATE sstage_ord.customers
SET
name = NEW.name, company = NEW.company, billingAddress = NEW.address1, billingAddress1 = NEW.address2, billingCity = NEW.city,
billingState = NEW.state, billingZip = NEW.zip, billingCountry = NEW.country, billingPhone = NEW.phone, shippingName = NEW.shipName,
shippingCompany = NEW.shipCompany, shippingAddress = NEW.shipAddress1, shippingAddress1 = NEW.address2, shippingCity = NEW.shipCity,
shippingState = NEW.shipState, shippingZip = NEW.shipZip, shippingCountry = NEW.shipCountry, shippingPhone = NEW.shipPhone
WHERE
email = NEW.email;
END IF;
SELECT user_id FROM sstage_ord.uFlex_users WHERE email = NEW.email INTO id_field;
IF id_field IS NOT NULL THEN
INSERT INTO sstage_ord.customer_purchases (customerID, orderNumber)
VALUES (id_field, NEW.orderNumber);
END IF;
END IF;
END//
DELIMITER ;