为数据库Librarymanage中的Bookinfo表创建触发器“tri_Bookdeleted”。执行DELETE操作时,触发器会显示“被删除图书总本数”和“被删除图书价格”
时间: 2023-09-12 14:06:29 浏览: 118
实现删除图书功能
以下是创建触发器 tri_Bookdeleted 的 SQL 语句:
```sql
CREATE TRIGGER tri_Bookdeleted
AFTER DELETE ON Bookinfo
FOR EACH ROW
BEGIN
DECLARE deleted_count INT;
DECLARE deleted_price FLOAT;
SELECT SUM(amount) INTO deleted_count FROM Bookinfo WHERE BookID = OLD.BookID;
SELECT SUM(price) INTO deleted_price FROM Bookinfo WHERE BookID = OLD.BookID;
IF deleted_count IS NULL THEN
SET deleted_count = 0;
END IF;
IF deleted_price IS NULL THEN
SET deleted_price = 0;
END IF;
SELECT CONCAT('被删除图书总本数:', deleted_count, ',被删除图书价格:', deleted_price) AS 'Delete Info';
END
```
这个触发器会在执行 DELETE 操作后自动触发,计算被删除图书的总本数和价格,并将结果显示出来。触发器使用了两个 SELECT 语句来计算总本数和总价格,然后使用 IF 语句检查结果是否为 NULL,并在必要时将其设置为 0。最后,触发器使用 CONCAT 函数将结果连接成一个字符串,并将其显示出来。
阅读全文