5.7

Consider the bank database of Figure 5.21. Write an SQL trigger to carry out the following action: On delete of an account, for each customer-owner of the account, check if the owner has any remaining accounts, and if she does not, delete her from the depositor relation.


CREATE TRIGGER check_delete_trigger 
AFTER DELETE ON account
REFERENCING OLD ROW AS orow
FOR EACH ROW
DELETE FROM depositor
WHERE depositor.customer_name NOT IN (
    SELECT customer_name
    FROM depositor
    WHERE account_number <> orow.account_number
)
END