Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.66 KB | None | 0 0
  1. /*
  2. -- SUBCONSULTA: Nome dos funcionários que tiveram o salário alterado a partir de 2016:
  3. SELECT nome FROM funcionario WHERE EXISTS (SELECT * FROM historico_pagamento_funcionario WHERE data > '2016-01-01');
  4.  
  5. -- SUBCONSULTA: Nome dos funcionários que estavam trabalhando no dia '2016-02-01':
  6. SELECT nome FROM funcionario WHERE EXISTS (SELECT * FROM ponto_horas WHERE data_entrada = '2016-02-01 08:00:00');
  7.  
  8. -- VIEW: EQUIPE + CLIENTE:
  9. CREATE VIEW equipe_para_cliente (cliente, nome, area) AS SELECT cliente.nome, funcionario.nome, area_atuacao.area FROM funcionario, area_atuacao, cliente WHERE funcionario.id_area = area_atuacao.id AND funcionario.id = cliente.id;
  10.  
  11. -- VIEW: CLIENTE + PAGAMENTOS:
  12. CREATE VIEW cliente_pagamentos (cliente, valor, data) AS SELECT cliente.nome, historico_pagamento_cliente.valor, historico_pagamento_cliente.data FROM cliente, historico_pagamento_cliente WHERE cliente.id = historico_pagamento_cliente.id_cliente;
  13.  
  14. -- ÍNDICE: PONTO DE HORAS:
  15. CREATE INDEX indice_ponto_horas ON ponto_horas USING HASH (id);
  16.  
  17. -- ÍNDICE: HISTÓRICO DE PAGAMENTOS DOS CLIENTES:
  18. CREATE INDEX indice_pagamentos_cliente ON historico_pagamento_cliente USING HASH (id);
  19.  
  20. -- TRIGGER: Criar histórico de mudança de e-mails dos clientes (empresas)
  21.  
  22. CREATE OR REPLACE FUNCTION historico_email_cliente() RETURNS TRIGGER AS $$
  23. BEGIN
  24. IF NEW.email <> OLD.email THEN
  25. INSERT INTO historico_email_cliente (empresa,email,data_mudanca) VALUES (empresa.nome,empresa.email,now());
  26. END IF;
  27. RETURN NEW;
  28. END;
  29. $$ Language plpgsql;
  30.  
  31. CREATE TRIGGER historico_email_cliente
  32. AFTER UPDATE ON cliente
  33. FOR EACH ROW
  34. EXECUTE PROCEDURE historico_email_cliente();
  35.  
  36. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement