DELIMITER $$ CREATE OR REPLACE DEFINER=`vn`@`localhost` TRIGGER `vn`.`awbInvoiceIn_afterDelete` AFTER DELETE ON `awbInvoiceIn` FOR EACH ROW BEGIN IF (NOT(SELECT COUNT(*) FROM invoiceInTax where invoiceInFk = OLD.invoiceInFk)) AND (NOT(SELECT COUNT(*) FROM invoiceInDueDay where invoiceInFk = OLD.invoiceInFk)) AND (NOT(SELECT isBooked FROM invoiceIn WHERE id = OLD.invoiceInFk) ) THEN DELETE FROM invoiceIn WHERE id = OLD.invoiceInFk; END IF; END$$ DELIMITER ;