存储函数 (1)创建一个存储数,返回图书表中所有图书的金额总和。 (2)创建个存储,定读者姓名,判断其类别,若是学生,则返回其可借天数。 程 不是则返回“-1”。 下 3.触发器 (1)创建触发器,在读者表中删除某读者记录的同时将借阅表中与该读者有关的借阅据全部删除。 (2)创建触发器,实现当向借阅表插入一行数据时,将库存表中该条码的图书的库存态改为“借出”。 (3)创建触发器,实现若修改借阅表中借阅状态为“已还”,则同时修改库存表中库有
时间: 2024-03-17 16:40:24 浏览: 110
答:
(1) 存储函数1:
```sql
CREATE FUNCTION get_total_book_price()
RETURNS FLOAT
BEGIN
DECLARE total_price FLOAT;
SELECT SUM(price) INTO total_price FROM book;
RETURN total_price;
END;
```
(2) 存储函数2:
```sql
CREATE FUNCTION get_borrow_days(reader_name VARCHAR(50))
RETURNS INT
BEGIN
DECLARE borrow_days INT;
SELECT IFNULL(student_borrow_days, -1) INTO borrow_days FROM reader WHERE name = reader_name;
RETURN borrow_days;
END;
```
(1) 触发器1:
```sql
CREATE TRIGGER delete_reader_trigger
AFTER DELETE ON reader
FOR EACH ROW
BEGIN
DELETE FROM borrow WHERE reader_id = OLD.id;
END;
```
(2) 触发器2:
```sql
CREATE TRIGGER borrow_book_trigger
AFTER INSERT ON borrow
FOR EACH ROW
BEGIN
UPDATE stock SET state = '借出' WHERE book_id = NEW.book_id;
END;
```
(3) 触发器3:
```sql
CREATE TRIGGER return_book_trigger
AFTER UPDATE ON borrow
FOR EACH ROW
BEGIN
IF NEW.state = '已还' THEN
UPDATE stock SET state = '在库' WHERE book_id = NEW.book_id;
END IF;
END;
```
阅读全文