好的,找到一個工作:
在文件中寫入完整的創建觸發器語句並使用PHPmyAdmin導入。
正如'影子'指出的那樣,在哪裏也有問題。
低於在我的方案中工作的更正的觸發器。
在網絡上有很多例子可以用mysql客戶端來完成觸發器。通過這種方式,您可以使用您輸入到客戶端的確切語法,並且在您無法使用命令外殼程序客戶端訪問服務器的情況下導入到PHPmyAdmin中。
希望這有助於其他新手。
乾杯,奧利弗
-- after_insert trigger for InvoiceItem to Calculate the Total in Invoice
------------------------------------------------------------------------
DELIMITER //
CREATE TRIGGER `InvoiceItem_After_Insert` AFTER INSERT ON `invoiceItem`
FOR EACH ROW
BEGIN
SET @InvoiceNumber = NEW.InvoiceFK;
SET @ItemTotal = (SELECT SUM(Extended) FROM InvoiceItem WHERE InvoiceFk = @InvoiceNumber);
UPDATE Invoice SET Total = @ItemTotal WHERE id = @InvoiceNumber;
END
//
DELIMITER ;
-- after_update trigger for InvoiceItem to Calculate the Total in Invoice
------------------------------------------------------------------------
DELIMITER //
CREATE TRIGGER `InvoiceItem_After_Update` AFTER UPDATE ON `invoiceitem`
FOR EACH ROW
BEGIN
SET @InvoiceNumber = NEW.InvoiceFK;
SET @ItemTotal = (SELECT SUM(Extended) FROM InvoiceItem WHERE InvoiceFk = @InvoiceNumber);
UPDATE Invoice SET Total = @ItemTotal WHERE id = @InvoiceNumber;
END
//
DELIMITER ;
-- after_delete trigger for InvoiceItem to Calculate the Total in Invoice
----------------------------------------------------------------------
DELIMITER //
CREATE TRIGGER `InvoiceItem_After_Delete` AFTER DELETE ON `invoiceItem`
FOR EACH ROW
BEGIN
SET @InvoiceNumber = OLD.InvoiceFK;
SET @ItemTotal = (SELECT SUM(Extended) FROM InvoiceItem WHERE InvoiceFk = @InvoiceNumber);
UPDATE Invoice SET Total = @ItemTotal WHERE id = @InvoiceNumber;
END
//
DELIMITER ;
-----------------------------------------------------------------
接下來的2個觸發器只是單獨的語句,並計算「擴展」列在表InvoceItem
---------------------------------------------------------------------
-- Calculate new 'Extended' price before insert
CREATE TRIGGER `InvoiceItem_Before_Insert` BEFORE INSERT ON `invoiceitem`
SET NEW.Extended = ROUND(NEW.Quantity * NEW.Price,2)
-------------------------------------------------------------------
-- Calculate new 'Extended' price before update
CREATE TRIGGER `InvoiceItem_Before_Update` BEFORE UPDATE ON `InvoiceItem`
FOR EACH ROW
SET NEW.Extended = ROUND(NEW.Quantity * NEW.Price,2);
不改變分隔符。 –