Skip to content

Commit

Permalink
Adiciona trigger de integridade.
Browse files Browse the repository at this point in the history
  • Loading branch information
ShaineOliveira committed Sep 8, 2024
1 parent ad6d6ed commit 204ab7a
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 1 deletion.
47 changes: 47 additions & 0 deletions docs/sql/trigger_procedure/trigger.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,57 @@ WHEN (NEW.vida <= 0)
EXECUTE FUNCTION verifica_vida_jogador();
~~~~


~~~~sql

CREATE OR REPLACE FUNCTION check_npc() RETURNS trigger
AS
$$
BEGIN
PERFORM * FROM npc WHERE id_npc = NEW.id_npc;
IF FOUND THEN
RAISE EXCEPTION 'Este pokemon já é um npc';
END IF;
RETURN NEW;
END;
$$ LANGUAGE plpgsql;

DROP TRIGGER trigger_check_npc ON jogador;
CREATE TRIGGER trigger_check_npc
BEFORE UPDATE OR INSERT ON jogador
FOR EACH ROW EXECUTE PROCEDURE check_npc();


CREATE OR REPLACE FUNCTION check_jogador() RETURNS trigger
AS
$$
BEGIN
PERFORM * FROM jogador WHERE id_jogador = NEW.id_jogador;
IF FOUND THEN
RAISE EXCEPTION 'Este pokemon já é um jogador';
END IF;
RETURN NEW;
END;
$$ LANGUAGE plpgsql;


DROP TRIGGER trigger_check_jogador ON npc;
CREATE TRIGGER trigger_check_jogador
BEFORE UPDATE OR INSERT ON npc
FOR EACH ROW EXECUTE PROCEDURE check_jogador();

~~~~






## Histórico de Versão

| Versão | Data | Descrição | Autor(es) |
| :----: | :--------: | :-----------------: | :------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------: |
| `1.0` | 07/09/2024 | Adicionando triggers | [Gabriel Marcolino](https://github.com/GabrielMR360) e [Shaíne Oliveira](ttps://github.com/ShaineOliveira)|
| `1.1` | 07/09/2024 | Adiciona trigger de vida | [Gabriel Marcolino](https://github.com/GabrielMR360) e [Shaíne Oliveira](ttps://github.com/ShaineOliveira) |
| `1.2` | 08/09/2024 | Adiciona trigger de integridade | [Gabriel Marcolino](https://github.com/GabrielMR360) e [Shaíne Oliveira](ttps://github.com/ShaineOliveira) |

44 changes: 43 additions & 1 deletion docs/sql/trigger_procedure/trigger.sql
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,46 @@ CREATE OR REPLACE TRIGGER trigger_verifica_vida_jogador
BEFORE UPDATE OF vida ON jogador
FOR EACH ROW
WHEN (NEW.vida <= 0)
EXECUTE FUNCTION verifica_vida_jogador();
EXECUTE FUNCTION verifica_vida_jogador();


--Garante a integridade da total exclusiva

CREATE OR REPLACE FUNCTION check_npc() RETURNS trigger
AS
$$
BEGIN
PERFORM * FROM npc WHERE id_npc = NEW.id_npc;
IF FOUND THEN
RAISE EXCEPTION 'Este pokemon já é um npc';
END IF;
RETURN NEW;
END;
$$ LANGUAGE plpgsql;

DROP TRIGGER trigger_check_npc ON jogador;
CREATE TRIGGER trigger_check_npc
BEFORE UPDATE OR INSERT ON jogador
FOR EACH ROW EXECUTE PROCEDURE check_npc();


CREATE OR REPLACE FUNCTION check_jogador() RETURNS trigger
AS
$$
BEGIN
PERFORM * FROM jogador WHERE id_jogador = NEW.id_jogador;
IF FOUND THEN
RAISE EXCEPTION 'Este pokemon já é um jogador';
END IF;
RETURN NEW;
END;
$$ LANGUAGE plpgsql;


DROP TRIGGER trigger_check_jogador ON npc;
CREATE TRIGGER trigger_check_jogador
BEFORE UPDATE OR INSERT ON npc
FOR EACH ROW EXECUTE PROCEDURE check_jogador();



0 comments on commit 204ab7a

Please sign in to comment.