I have a code that already works, but i'm looking for a way to write an sql statement that inserts into a table when a row has been deleted from another table without my having to list out every column as a value under the insert statement. For cases where by a table has many columns it would really come in handy. Here's my current code, I hope someone could help me modifiy it.
CREATE TABLE `history_tblvendorpayment` LIKE `tblvendorpayment`;
ALTER TABLE `history_tblvendorpayment`
MODIFY COLUMN `id` INT UNSIGNED NOT NULL;
ALTER TABLE `history_tblvendorpayment` DROP PRIMARY KEY;
ALTER TABLE `history_tblvendorpayment`
ADD COLUMN `history_id` INT UNSIGNED NOT NULL;
ALTER TABLE `history_tblvendorpayment`
ADD CONSTRAINT PRIMARY KEY (`history_id`);
ALTER TABLE `history_tblvendorpayment`
MODIFY `history_id` INT UNSIGNED NOT NULL AUTO_INCREMENT;
DELIMITER $$
DROP TRIGGER `delete_tblvendorpayment`$$
CREATE TRIGGER `delete_tblvendorpayment` BEFORE DELETE on `tblvendorpayment`
FOR EACH ROW
BEGIN
INSERT INTO history_tblvendorpayment (id, created_by,
vendor_id, amount_payable,
amount_payed, description,
depositor_name, gl_code_credit,
gl_code_debit, txn_date,
pay_date, ref_id,
bank, currency,
date, md_approval,
status, sage_status,
memo_id)
VALUES (
OLD.id, OLD.created_by,
OLD.vendor_id, OLD.amount_payable,
OLD.amount_payed, OLD.description,
OLD.depositor_name, OLD.gl_code_credit,
OLD.gl_code_debit, OLD.txn_date,
OLD.pay_date, OLD.ref_id,
OLD.bank, OLD.currency,
OLD.date, OLD.md_approval,
OLD.status, OLD.sage_status,
OLD.memo_id
);END$$
DELIMITER ;
I'm hoping for something like this:
DELIMITER $$
DROP TRIGGER `delete_tblvendorpayment`$$
CREATE TRIGGER `delete_tblvendorpayment` BEFORE DELETE on `tblvendorpayment`
FOR EACH ROW
BEGIN
INSERT INTO history_tblvendorpayment
SELECT * FROM OLD;
END$$
DELIMITER ;
If you are inserting all the columns from the original table you could simplify the trigger to:
CREATE TRIGGER `delete_tblvendorpayment` BEFORE DELETE on `tblvendorpayment`
FOR EACH ROW
BEGIN
INSERT INTO history_tblvendorpayment
SELECT *
FROM tblvendorpayment
WHERE id = OLD.id;
END
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.