USE `vn`; 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; USE `vn2008`; CREATE OR REPLACE ALGORITHM = UNDEFINED DEFINER = `root`@`%` SQL SECURITY DEFINER VIEW `vn2008`.`Ordenes` AS SELECT `tr`.`id` AS `Id_ORDEN`, `tr`.`description` AS `ORDEN`, `tr`.`requesterFk` AS `requesterFk`, `tr`.`attenderFk` AS `attenderFk`, `tr`.`quantity` AS `CANTIDAD`, `tr`.`itemFk` AS `Id_ARTICLE`, `tr`.`price` AS `PRECIOMAX`, `tr`.`isOk` AS `isOk`, `tr`.`saleFk` AS `Id_Movimiento`, `tr`.`ticketFk` AS `ticketFk`, `tr`.`response` AS `COMENTARIO`, `tr`.`created` AS `odbc_date`, `tr`.`ordered` AS `datORDEN`, `tr`.`shipped` AS `datTICKET`, `tr`.`salesPersonCode` AS `CodVENDEDOR`, `tr`.`buyerCode` AS `CodCOMPRADOR`, `tr`.`price__` AS `PREU`, `tr`.`clientFk` AS `Id_CLIENTE`, `tr`.`ok__` AS `OK`, `tr`.`total` AS `TOTAL`, `tr`.`buyed` AS `datCOMPRA`, `tr`.`ko__` AS `KO` FROM `vn`.`ticketRequest` `tr`; USE `vn`; DROP TRIGGER IF EXISTS `vn`.`ticketRequest_beforeInsert`; DELIMITER $$ USE `vn`$$ 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 $$ USE `vn`$$ 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 ;