-
Notifications
You must be signed in to change notification settings - Fork 0
/
Workshop_9.sql
150 lines (119 loc) · 4.09 KB
/
Workshop_9.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
create database [BP2.Ispit.20.06.2017.Integralni] on (
name='[BP2.Ispit.20.06.2017.Integralni]mdf',
filename='D:\BP2\Data\6.2017.Integral.mdf',
size=20mb,
maxsize=30mb,
filegrowth=5%
)
log on(
name='[BP2.Ispit.20.06.2017.Integralni]ldf',
filename='D:\BP2\Log\6.2017.Integral.ldf',
size=20mb,
maxsize=30mb,
filegrowth=5%
)
use [BP2.Ispit.20.06.2017.Integralni]
create table Proizvodi(
ProizvodID int primary key,
Sifra nvarchar(25) unique not null,
Naziv nvarchar(50) unique not null,
Kategorija nvarchar(50) unique not null,
Cijena decimal not null
)
alter table Proizvodi
drop constraint UQ__Proizvod__1785E0D74E5BD86B
create clustered index ind_siff on StavkeNarudzbe(Kolicina)
alter index ind_sif on Proizvodi disable
create table Narudzbe(
NarudzbaID int primary key,
BrojNarudzbe nvarchar(25) unique not null,
Datum date not null,
Ukupno decimal not null
)
create table StavkeNarudzbe(
ProizvodID int not null constraint fk_ProizvodID foreign key references Proizvodi(ProizvodID),
NarudzbaID int not null constraint fk_NarudzbaID foreign key references Narudzbe(NarudzbaID),
constraint pk_PrNarID primary key (ProizvodID,NarudzbaID),
Kolicina int not null,
Cijena decimal not null,
Popust decimal not null,
Iznos decimal not null
)
insert into Proizvodi
select p.ProductID,p.ProductNumber,p.Name,pc.Name,p.ListPrice
from AdventureWorks2014.Production.Product as p join AdventureWorks2014.Production.ProductSubcategory as ps on p.ProductSubcategoryID=ps.ProductSubcategoryID
join AdventureWorks2014.Production.ProductCategory as pc on ps.ProductCategoryID=pc.ProductCategoryID
where year(p.ModifiedDate)=2014
insert into Narudzbe
select s.SalesOrderID,s.SalesOrderNumber,s.OrderDate,s.TotalDue
from AdventureWorks2014.Sales.SalesOrderHeader as s
where year(s.ModifiedDate)=2014
insert into StavkeNarudzbe
select distinct p.ProductID,s.SalesOrderID,s.OrderQty,s.UnitPrice,s.UnitPriceDiscount,s.LineTotal
from AdventureWorks2014.Sales.SalesOrderDetail as s join AdventureWorks2014.Sales.SalesOrderHeader as sh on s.SalesOrderID=sh.SalesOrderID
join AdventureWorks2014.Sales.SpecialOfferProduct as sp on sp.ProductID=s.ProductID join AdventureWorks2014.Production.Product as p
on sp.ProductID=p.ProductID
where year(s.ModifiedDate)=2014
create table Skladista(
SkladisteID int identity(1,1) primary key,
Naziv nvarchar(25) not null
)
create table SkladisteProizvod(
SkladisteID int not null constraint fk_SkladisteID foreign key references Skladista(SkladisteID),
ProizvodID int not null constraint fk_ProizvodsID foreign key references Proizvodi(ProizvodID),
constraint pk_sklprid primary key (SkladisteID,ProizvodID),
Kolicina int
)
insert into Skladista
values('Skladiste Mostar'),
('Skladiste Konjic'),
('Skladiste Sarajevo')
insert into SkladisteProizvod
select 3,p.ProizvodID,0
from Proizvodi as p
create procedure IzmjenaKolicine(
@ProizvodID int,
@SkladisteID int,
@Kolicina int
)
as
begin
update SkladisteProizvod
set Kolicina=@Kolicina
where ProizvodID=@ProizvodID and SkladisteID=@SkladisteID
end
select * from StavkeNarudzbe
exec IzmjenaKolicine 680,1,500
create nonclustered index ind_pro on Proizvodi(Sifra,Naziv)
create view pog
as
select p.Sifra,p.Naziv,p.Cijena,sum(sn.Kolicina) as 'Ukupna prodana kolicina',sum(sn.Iznos) as 'Ukupna zarada'
from Proizvodi as p join StavkeNarudzbe as sn on p.ProizvodID=sn.ProizvodID join Narudzbe as n on sn.NarudzbaID=n.NarudzbaID
group by p.Sifra,p.Naziv,p.Cijena
select * from pog
create procedure sifr(
@Sifra nvarchar(25)= null
)
as
begin
select p.[Ukupna prodana kolicina],p.[Ukupna zarada]
from pog as p
where p.Sifra=@Sifra or @Sifra is null
end
exec sifr 'LJ-0192-S'
backup database [BP2.Ispit.20.06.2017.Integralni]
to disk ='C:\Program Files\Microsoft SQL Server\MSSQL14.SQLA\MSSQL\Backup\fulo.bak'
backup database [BP2.Ispit.20.06.2017.Integralni]
to disk ='C:\Program Files\Microsoft SQL Server\MSSQL14.SQLA\MSSQL\Backup\fulodif.bak'
with differential
alter TRIGGER trig
ON Proizvodi instead of delete
AS
BEGIN
select d.Naziv
into #temp
from deleted as d
END
select * from Proizvodi
delete Proizvodi
select * from #temp