触发器语法格式:

    1. -- 增加村民记录的触发器
    2. DROP TRIGGER if EXISTS `insert_villager`;
    3. DELIMITER $
    4. CREATE TRIGGER `insert_villager`
    5. BEFORE INSERT
    6. ON `villager`
    7. FOR EACH ROW
    8. BEGIN
    9. -- 根据出生年月日计算村民年龄
    10. SET new.villager_age = DATEDIFF(NOW(),new.villager_birthday)/365;
    11. -- 村民所在的家庭人口数+1
    12. UPDATE `family` SET people_amount = people_amount + 1
    13. WHERE `family`.`F_ID` = new.`FK_FID`;
    14. END $
    15. DELIMITER;
    1. drop TRIGGER if EXISTS `tri_returnbook`;
    2. delimiter //
    3. create TRIGGER `tri_returnbook`
    4. after delete
    5. on `Loan`
    6. for each row
    7. Begin
    8. declare _ISBN char(13);
    9. declare _loanNo varchar(16);
    10. insert into LoanHist(loanNo,bookNo,borrowDate,returnDate) value(old.loanNo,old.bookNo,old.borrowDate,now());
    11. SELECT r.ISBN,r.loanNo into _ISBN,_loanNo
    12. from Reservation as r,Books as b
    13. where r.ISBN = b.ISBN and b.bookNo = old.bookNo
    14. order by r.reservationDate
    15. limit 0,1;
    16. if(_ISBN is null) then
    17. update Books set bstatus = 0
    18. where bookNo = old.bookNo;
    19. else
    20. update Reservation set rstatus = 'T'
    21. where ISBN = _ISBN and loanNo = _loanNo;
    22. update Books set bstatus = 3
    23. where bookNo = old.bookNo;
    24. end if;
    25. end//
    26. delimiter ;