Anda di halaman 1dari 3

EXEMPLO COMANDOS TRIGGER DB2

CRIAR O BANCO DE DADOS

Create database LOJA;

CRIAR AS TABELAS

Create table artistas


(numartista int not null primary key,
nomeartista varchar(50),
classif char(1) not null);

Create table discos


(numdisco int not null primary key,
nomedisco varchar(50),
numartista int,
foreign key (numartista) references artistas);

Create table estoque


(numdisco int not null,
preco decimal (5,2) not null,
qtd int,
foreign key (numdisco) references discos);

Create table pedidos


(numdisco int not null,
qtd_atual int,
datapedido date,
foreign key (numdisco) references discos);

Adiciona CHECK CONSTRAINTS na tabela ARTISTAS


alter table artistas
add constraint cc_classif check ( classif in ('C','R','B','N','O'));

Criar uma TRIGGER para ficar verificando o saldo dos produtos


em estoque, e se ele estiver abaixo de cinco unidades, inserir uma linha
na tabela pedidos.

CREATE TRIGGER estoquebaixo


AFTER UPDATE OF qtd ON estoque
REFERENCING NEW AS n
FOR EACH ROW MODE DB2SQL
WHEN ( n.qtd <= 5 )
insert into pedidos
values( n.numdisco, n.qtd, current date);

Criar uma view para calcular o inventario

drop view inventario;


create view inventario
("Numero do disco", "Valor total", "Quant total" ) as
SELECT numdisco, sum (preco * qtd), sum(qtd)
FROM estoque
GROUP BY numdisco;
Inserir dados na tabela artistas

insert into artistas values


(1,'Pink Floyd', 'R'),
(2,'Deep Purple', 'R'),
(3,'Uriah Heep', 'R'),
(4,'Raul Seixas', 'R'),
(5,'Bach, Johann Sebastian', C'),
(6,'Sergio Mendes', 'B'),
(7,'Tim Maia', 'B');

Inserir dados na tabela discos

Insert into discos values


(724, 'Dark Side of the Moon', 1 ),
(725, 'Relics', 1 ),
(759, 'Machine Head', 2 ),
(501, 'Return to Fantasy', 3 ),
(834, 'A arte de Raul Seixas', 4 );

Insere dados na tabela estoque

insert into estoque values


(724, 34.90, 8 ),
(725, 24.90, 9 ),
(759, 22.50, 3 ),
(501, 19.90, 7 );

Ler os dados das tabelas ARTISTAS, DISCOS e ESTOQUE

select a.nomeartista,
d.nomedisco,
e.qtd
from artistas a,
discos d,
estoque e
where a.numartista = d.numartista
and d.numdisco = e.numdisco ;

Ler os dados das tabelas ARTISTAS e DISCOS

select a.classif,
substr ( a.nomeartista, 1, 22) as Artista,
substr ( d.nomedisco, 1, 30) as Disco ,
e.qtd
from artistas a,
discos d,
estoque e
where a.numartista = d.numartista
and d.numdisco = e.numdisco
order by a.classif, a.nomeartista, d.nomedisco ;

Ler os dados das tabelas ARTISTAS e DISCOS

select CASE a.classif


WHEN 'B' THEN 'Brasileira '
WHEN 'C' THEN 'Cl�ssica'
WHEN 'N' THEN 'New Age'
WHEN 'O' THEN 'Outros'
WHEN 'R' THEN 'Rock'
ELSE 'Erro'
END as Tipo,
substr ( a.nomeartista, 1, 24) as Artista,
substr ( d.nomedisco, 1, 30) as Disco ,
e.qtd as qtd_estoque
from artistas a,
discos d,
estoque e
where a.numartista = d.numartista
and d.numdisco = e.numdisco
order by a.classif, a.nomeartista, d.nomedisco ;

Ler os dados da tabela ARTISTAS

select substr ( a.nomeartista, 1, 24) as artista,


SUM ( e.qtd ) as quant,
SUM ( e.preco ) as pre�o
from artistas a,
discos d,
estoque e
where a.numartista = d.numartista
and d.numdisco = e.numdisco
group by a.nomeartista ;

Verificar qual o estoque do disco Dark Side of the Moon

select * from estoque where numdisco = 724;

Alterar o estoque do album Dark Side of the Moon

update estoque set qtd = qtd -4 where numdisco = 724;

Verificar qual o estoque do disco Dark Side of the Moon

select * from loja.estoque where numdisco = 724;

Anda mungkin juga menyukai