55 lines
1.5 KiB
SQL
55 lines
1.5 KiB
SQL
|
|
ALTER TABLE `vn`.`ticketRequest`
|
|
DROP FOREIGN KEY `fgnAtender`;
|
|
ALTER TABLE `vn`.`ticketRequest`
|
|
CHANGE COLUMN `atenderFk` `attenderFk` INT(11) NULL DEFAULT NULL ;
|
|
ALTER TABLE `vn`.`ticketRequest`
|
|
ADD CONSTRAINT `fgnAtender`
|
|
FOREIGN KEY (`attenderFk`)
|
|
REFERENCES `vn`.`worker` (`id`)
|
|
ON UPDATE CASCADE;
|
|
|
|
|
|
|
|
|
|
DROP TRIGGER IF EXISTS `vn`.`ticketRequest_beforeInsert`;
|
|
|
|
DELIMITER $$
|
|
CREATE DEFINER=`root`@`%` TRIGGER `vn`.`ticketRequest_beforeInsert` BEFORE INSERT ON `ticketRequest` FOR EACH ROW
|
|
BEGIN
|
|
IF NEW.ticketFk IS NULL THEN
|
|
SET NEW.ticketFk = (SELECT s.ticketFk FROM sale s WHERE s.id = NEW.saleFk);
|
|
END IF;
|
|
|
|
IF NEW.requesterFk IS NULL THEN
|
|
SET NEW.requesterFk = (SELECT w.id FROM worker w WHERE w.code = NEW.salesPersonCode);
|
|
END IF;
|
|
|
|
IF NEW.attenderFk IS NULL THEN
|
|
SET NEW.attenderFk = (SELECT w.id FROM worker w WHERE w.code = NEW.buyerCode);
|
|
END IF;
|
|
END$$
|
|
DELIMITER ;
|
|
|
|
|
|
DROP TRIGGER IF EXISTS `vn`.`ticketRequest_beforeUpdate`;
|
|
|
|
DELIMITER $$
|
|
CREATE DEFINER=`root`@`%` TRIGGER `vn`.`ticketRequest_beforeUpdate` BEFORE UPDATE ON `ticketRequest` FOR EACH ROW
|
|
BEGIN
|
|
IF NEW.saleFk <> OLD.saleFk THEN
|
|
SET NEW.ticketFk = (SELECT s.ticketFk FROM sale s WHERE s.id = NEW.saleFk);
|
|
END IF;
|
|
|
|
IF NEW.salesPersonCode <> OLD.salesPersonCode THEN
|
|
SET NEW.requesterFk = (SELECT w.id FROM worker w WHERE w.code = NEW.salesPersonCode);
|
|
END IF;
|
|
|
|
IF NEW.buyerCode <> OLD.buyerCode THEN
|
|
SET NEW.attenderFk = (SELECT w.id FROM worker w WHERE w.code = NEW.buyerCode);
|
|
END IF;
|
|
END$$
|
|
DELIMITER ;
|
|
|
|
|