Trigger executes if a column value is 'Database I'

Asked

Viewed 21 times

0

Create a trigger that automatically enrolls a student in "BD Laboratory I" as soon as it is registered in "Database I".

    use Universidade;

insert into Realiza_matricula (RGAacad, Nomedisc) values(20179474911, 'Banco de Dados I');

Delimiter $$
create trigger matricula
after insert on Realiza_matricula
for each row
    BEGIN
        IF (select * From Realiza_matricula Where Nomedisc = 'Banco de Dados I') THEN
            insert into Realiza_matricula values (NEW.RGAacad, 'Laboratorio de BD I', OLD.Date);
        end if;
    END $$
Delimiter;
  • It would not be more practical to just check if NEW.Nomedisc is 'Database I' and in this case insert the discipline 'BD Laboratory I', without the need to make the query?

1 answer

0

Uses the EXISTS in front of the if, "If there are Rows...". Note: Use the NOT EXISTS for when not returning Rows:

 IF EXISTS(select * From Realiza_matricula Where Nomedisc = 'Banco de Dados I') THEN
            insert into Realiza_matricula values (NEW.RGAacad, 'Laboratorio de BD I', OLD.Date);
        end if;

Browser other questions tagged

You are not signed in. Login or sign up in order to post.