Advertisement
DarknessRdg

banco de dados escola

Jun 15th, 2019
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. create table aluno(
  2.     id_aluno serial not null primary key,
  3.     nome_aluno varchar(255) not null,
  4.     nascimento_aluno date not null,
  5.     matricula_aluno varchar(10) not null
  6. );
  7.  
  8. create table escola(
  9.     id_escola serial not null primary key,
  10.     nome varchar(255) not null,
  11.     numero int not null,
  12.     complemento varchar(255),
  13.     cep varchar(8) not null
  14. );
  15.  
  16. create table turma(
  17.     id_turma serial not null primary key,
  18.     ano int not null,
  19.     sigla char(1) not null,
  20.     e_medio bool not null,
  21.     id_escola int not null references escola(id_escola)
  22. );
  23.  
  24. create table aluno_turma(
  25.     id_aluno_turma serial not null primary key,
  26.     id_aluno int not null references aluno(id_aluno),
  27.     id_turma int not null references turma(id_turma),
  28.     data date default now()
  29. );
  30.  
  31. create table professor(
  32.     id_professor serial not null primary key,
  33.     nascimento_professor date not null,
  34.     cpf varchar(11) not null,
  35.     matricula_professor varchar(10) not null
  36. );
  37.  
  38. create table disciplina(
  39.     id_disciplina serial not null primary key,
  40.     descricao varchar(255) not null,
  41.     ano int not null,
  42.     e_medio bool not null
  43. );
  44.  
  45. create table aula(
  46.     id_aula serial not null primary key,
  47.     id_turma int not null references turma(id_turma),
  48.     id_professor int not null references professor(id_professor),
  49.     id_disciplina int not null references disciplina(id_disciplina),
  50.     ano_letivo int default extract(YEAR from now())
  51. );
  52.  
  53. create table prova(
  54.     id_prova serial not null primary key,
  55.     id_disciplina int references disciplina(id_disciplina),
  56.     finalizada bool default false
  57. );
  58.  
  59.  
  60. create table aplicacao(
  61.     id_aplicacao serial not null primary key,
  62.     id_aula int not null references aula(id_aula),
  63.     id_prova int not null references prova(id_prova),
  64.     data_aplicacao date not null
  65. );
  66.  
  67. create table questao(
  68.     id_questao serial not null primary key,
  69.     enunciado varchar(500) not null,
  70.     id_disciplina int not null references disciplina(id_disciplina),
  71.     finalizada bool default false
  72. );
  73.  
  74. create table alternativa(
  75.     id_alternativa serial not null primary key,
  76.     descricao varchar(255)
  77. );
  78.  
  79. create table gabarito (
  80.     id_gabarito serial not null primary key,
  81.     id_prova int not null references prova(id_prova),
  82.     id_questao int not null references questao(id_questao),
  83.     id_alternativa int not null references alternativa(id_alternativa),
  84.     e_certo bool not null
  85. );
  86.  
  87. create table resposta(
  88.     id_resposta serial not null primary key,
  89.     id_aluno int not null references aluno(id_aluno),
  90.     id_gabarito int not null references gabarito(id_gabarito)
  91. );
  92.  
  93.  
  94. -- TRIGGRERS
  95.  
  96. create function validate_aluno_turma() returns trigger as $$
  97. begin
  98.     if extract(year from new.data) in
  99.     (select extract(year from data) from aluno_turma where new.id_aluno = id_aluno) then
  100.         if TG_OP = 'UPDATE' then
  101.             if extract(year from new.data) = extract(year from old.data) then
  102.                 return new;
  103.             end if;
  104.         end if;
  105.        
  106.         raise exception 'um aluno so pode ser matriculado uma vez por ano';
  107.     end if;
  108.    
  109.     return new;
  110. end;
  111. $$ language plpgsql;
  112.  
  113. create trigger verifiar_aluno_turma before insert or update on aluno_turma
  114.     for each row execute procedure validate_aluno_turma();
  115.    
  116. insert into aluno values(default, 'luan', '2000-09-26', '123');
  117. insert into escola values(default, 'ifpi', 1, null, '2234');
  118. insert into turma values(default, 1, 'a', true, 1);
  119.  
  120. -- test trigger um ano por ano
  121. insert into aluno_turma values(default, 1, 1, now());
  122. insert into aluno_turma values(default, 1, 1, '2019-06-5'); -- esperado error !
  123. insert into aluno_turma values(default, 1, 1, '2018-06-5');
  124. update aluno_turma set data='2019-05-1' where id_aluno_turma=1; -- espeardo: passar !
  125. update aluno_turma set data='2018-12-31' where id_aluno_turma=1; -- espado: error !
  126.  
  127. -- AULA
  128. create or replace function validate_aula() returns trigger as $$
  129. declare
  130.     ano_turma int;
  131.     ano_disciplina int;
  132.     e_medio_turma boolean;
  133.     e_medio_disciplina boolean;
  134. begin
  135.     select ano, e_medio into ano_turma, e_medio_turma from turma where id_turma = new.id_turma;
  136.     select ano, e_medio into ano_disciplina, e_medio_disciplina from disciplina where id_disciplina = new.id_disciplina;
  137.  
  138.     if ano_turma != ano_disciplina then
  139.         raise exception 'Ano da disciplina tem que ser igual ao da turma';
  140.     elsif e_medio_disciplina != e_medio_turma then
  141.         raise exception 'Grau (medio / fundamental) da disciplina tem que ser igual ao da turma';
  142.     end if;
  143.     new.ano_letivo := ano_turma;
  144.     return new;
  145.  
  146. end;
  147. $$ language plpgsql;
  148.  
  149.  
  150. create trigger verificar_aula before insert or update on aula
  151.     for each row execute procedure validate_aula();
  152.  
  153. -- tests
  154. insert into disciplina values
  155.     (1, 'matemtaica', 2, false)
  156.     (2, 'matemtaica', 1, false)
  157.     (3, 'matemtaica', 1, true);
  158.  
  159. insert into professor values(1, '1993-02-10', '00132101', '123prof');
  160.  
  161. insert into aula values(1, 1, 1, 1); -- error ano;
  162. insert into aula values(1, 1, 1, 2); -- error medio;
  163. insert into aula values(1, 1, 1, 3); -- ok!
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement