DELIMITER $$
CREATE OR REPLACE DEFINER=`vn-admin`@`localhost` TRIGGER `vn`.`ticketDms_beforeDelete`
	BEFORE DELETE ON `ticketDms`
	FOR EACH ROW
BEGIN
    UPDATE dms 
        SET dmsTypeFk = (SELECT id 
							FROM dmsType 
                            WHERE `code` = 'trash'
						)
		WHERE id = OLD.dmsFk AND ( SELECT IF(COUNT(*) > 0, FALSE, TRUE) 
										FROM ticketDms 
										WHERE dmsFk = OLD.dmsFk
								  ) ;
END$$
DELIMITER ;
