If you have your hands tied so tightly that you are unable to change the table structure, then you probably won't be able to use a trigger, but here goes anyway.
This is a sample table structure:
DROP TABLE IF EXISTS `products`;
CREATE TABLE `products` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`product` VARCHAR(255),
`quantity` INT(11) NOT NULL,
`cost` DECIMAL(6,2) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB;
Create a trigger which updates the cost prior to it being inserted into the products
table:
DELIMITER //
DROP TRIGGER IF EXISTS update_cost//
CREATE TRIGGER update_cost BEFORE INSERT ON products
FOR EACH ROW BEGIN
SET NEW.cost = NEW.cost * NEW.quantity;
END;
//
DELIMITER ;
Insert a couple of products into the table:
INSERT INTO `products` (`product`, `quantity`, `cost`) VALUES
('Acme portable hole', 10, 20),
('Acme jet pack', 1000, 2);
The cost is updated automatically:
SELECT * FROM prodcuts;
+----+--------------------+----------+---------+
| id | product | quantity | cost |
+----+--------------------+----------+---------+
| 1 | Acme portable hole | 10 | 200.00 |
| 2 | Acme jet pack | 1000 | 2000.00 |
+----+--------------------+----------+---------+