Goiy De3
Goiy De3
BAØI SOÁ 1
PHẦN 1:
Câu 1:
select tennv
from nhanvien
where manv in(select manv
from tiepthi
group by manv
having count(tensp)=(select count(distinct tensp)
from cthoadon
where tiepthi.manv=manv)
)
Câu 2:
select tennv,sp.tensp, sum(soluong) as 'tong luong'
from nhanvien as nv, sanpham as sp, cthoadon as ct
where nv.manv=ct.manv and sp.tensp=ct.tensp
group by nv.manv,tennv,sp.tensp
having sum(soluong)>=all(select sum(soluong)
from cthoadon
where nv.manv=manv
group by tensp)
Câu 3:
select tennv, t.tensp
from nhanvien as nv, sanpham as sp, tiepthi as t
where (nv.manv=t.manv and sp.tensp=t.tensp)
and t.tensp not in( select tensp
from cthoadon
where nv.manv=manv)
PHẦN 2:
Câu 1:
create proc Sp_Min
as
select tensp
from sanpham
where dongia<=all(select dongia from sanpham)
Câu 2:
create proc TienTheoNgay @ngay datetime, @TongTien float OUTPUT
as
if not exists (select *
from cthoadon as ct, hoadon as hd
where (ct.sohd=hd.sohd and ct.manv=hd.manv)
and ngay=@ngay
)
set @TongTien=0
else
select @TongTien=sum(soluong*dongia)
from sanpham as sp, hoadon as hd, cthoadon as ct
where sp.tensp=ct.tensp and (ct.sohd=hd.sohd and ct.manv=hd.manv)
and ngay=@ngay
Câu 3:
create proc Spham_of_Nvien(@manvX char(10))
as
select tensp
from tiepthi
where manv=@manvX
PHẦN 3:
create trigger R1 create trigger R3
on tiepthi on tiepthi
for insert,update for insert, update
as as
if update(manv) or update(tensp) declare @sp char(20)
begin if update(tensp)
if (select count(*) begin
from tiepthi, inserted select @sp=tensp
where tiepthi.manv=inserted.manv)>5 from inserted
begin if @sp not in(select tensp from sanpham)
print 'khong them duoc ! moi nhan vien begin
duoc tiep thi toi da 5 sp' rollback tran
rollback transaction print 'Loi ve rang buoc tham chieu'
end end
end end
-- --
create trigger R2 create trigger R4
on hoadon on sanpham
for insert,update for insert,update
as as
declare @kv char(20) declare @gia float
if update(khuvuc) if update(dongia)
begin begin
select @kv=khuvuc select @gia=dongia
from inserted from inserted
if @kv not in('Q1','Q3','Q10') if @gia<=0
begin begin
rollback tran rollback tran
print 'Khu vuc khong hop le' print 'don gia phai >0'
end end
end end
--