Advertisement
nantunes

Ex 3

May 6th, 2021
942
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PL/SQL 0.65 KB | None | 0 0
  1. -- 3. Implemente um trigger que, ao remover um autor da tabela autores,
  2. -- apague da tabela livros todos os livros publicados pelo autor removido.
  3.  
  4.  
  5. CREATE OR REPLACE FUNCTION autores_cascade_livros_function() returns TRIGGER
  6. language plpgsql
  7. AS $$
  8. BEGIN
  9.     -- old -- a linha toda a ser apagada dos autores
  10.     DELETE FROM livros
  11.      WHERE id_autor = old.id_autor; -- cujo id_autor seja igual ao id_autor do livro apagado
  12.    
  13.     RETURN old;
  14. END;
  15. $$;
  16.  
  17.  
  18.  
  19. CREATE TRIGGER autores_cascade_livros
  20. BEFORE DELETE ON autores -- se for after o delete borrega
  21.  FOR EACH ROW -- ja ca voltamos
  22. EXECUTE PROCEDURE autores_cascade_livros_function();
  23.  
  24.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement