-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreateTables.sql
More file actions
187 lines (167 loc) · 6.69 KB
/
Copy pathcreateTables.sql
File metadata and controls
187 lines (167 loc) · 6.69 KB
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
drop table if exists categoria cascade;
drop table if exists categoria_simples cascade;
drop table if exists super_categoria cascade;
drop table if exists tem_outra cascade;
drop table if exists produto cascade;
drop table if exists tem_categoria cascade;
drop table if exists IVM cascade;
drop table if exists ponto_de_retalho cascade;
drop table if exists instalada_em cascade;
drop table if exists prateleira cascade;
drop table if exists planograma cascade;
drop table if exists retalhista cascade;
drop table if exists responsavel_por cascade;
drop table if exists evento_reposicao cascade;
--------------------------------------------------
-- Table creation --
--------------------------------------------------
create table categoria (
nome_categoria varchar(80) not null,
constraint pk_categoria primary key(nome_categoria)
);
create table categoria_simples (
nome_categoria varchar(80) not null,
constraint pk_categoria_simples primary key(nome_categoria),
constraint fk_simp_cat foreign key(nome_categoria) references categoria(nome_categoria)
);
create table super_categoria (
nome_categoria varchar(80) not null,
constraint pk_super_categoria primary key(nome_categoria),
constraint fk_super_cat foreign key(nome_categoria) references categoria(nome_categoria)
);
create table tem_outra (
super_categoria varchar(80) not null,
nome_categoria varchar(80) not null,
constraint pk_tem_outra primary key(nome_categoria),
constraint fk_outra_sup foreign key(super_categoria) references super_categoria(nome_categoria),
constraint fk_outra_cat foreign key(nome_categoria) references categoria(nome_categoria),
constraint ck_cat_supcat check(super_categoria != nome_categoria)
);
create table produto (
ean bigint not null,
nome_categoria varchar(80) not null,
descricao varchar(200) not null,
constraint pk_produto primary key(ean),
constraint fk_prod_cat foreign key(nome_categoria) references categoria(nome_categoria),
constraint ck_prod check(ean <= 9999999999999 and ean >= 1000000000000)
);
create table tem_categoria (
ean bigint not null,
nome_categoria varchar(80) not null,
constraint pk_tem_cat primary key(ean, nome_categoria),
constraint fk_temcat_prod foreign key(ean) references produto(ean),
constraint fk_temcat_cat foreign key(nome_categoria) references categoria(nome_categoria)
);
create table IVM (
num_serie int not null,
fabricante varchar(80) not null,
constraint pk_ivm primary key(num_serie, fabricante)
);
create table ponto_de_retalho (
nome_retalho varchar(80) not null,
distrito varchar(80) not null,
concelho varchar(80) not null,
constraint pk_retalho primary key(nome_retalho)
);
create table instalada_em (
num_serie int not null,
fabricante varchar(80) not null,
ivm_local varchar(80) not null,
constraint pk_instalada primary key(num_serie, fabricante),
constraint fk_inst_ivm foreign key(num_serie, fabricante) references IVM(num_serie, fabricante),
constraint fk_inst_ret foreign key(ivm_local) references ponto_de_retalho(nome_retalho)
);
create table prateleira (
nro int not null,
num_serie int not null,
fabricante varchar(80) not null,
altura int not null,
nome_categoria varchar(80) not null,
constraint pk_prateleira primary key(nro, num_serie, fabricante),
constraint fk_prat_ivm foreign key(num_serie, fabricante) references IVM(num_serie, fabricante),
constraint fk_prat_cat foreign key(nome_categoria) references categoria(nome_categoria)
);
create table planograma (
ean bigint not null,
nro int not null,
num_serie int not null,
fabricante varchar(80) not null,
faces int not null,
unidades int not null,
constraint pk_plan primary key(ean, nro, num_serie, fabricante),
constraint fk_plan_prod foreign key(ean) references produto(ean),
constraint fk_plan_ivm foreign key(nro, num_serie, fabricante)
references prateleira(nro, num_serie, fabricante)
);
create table retalhista (
tin int not null,
nome_retalhista varchar(80) not null unique,
constraint pk_retalhista primary key(tin)
);
create table responsavel_por (
nome_categoria varchar(80) not null,
tin int not null,
num_serie int not null,
fabricante varchar(80) not null,
constraint pk_resppor primary key(nome_categoria, num_serie, fabricante),
constraint fk_resppor_ivm foreign key(num_serie, fabricante) references IVM(num_serie, fabricante),
constraint fk_resppor_ret foreign key(tin) references retalhista(tin),
constraint fk_resppor_cat foreign key(nome_categoria) references categoria(nome_categoria)
);
create table evento_reposicao (
ean bigint not null,
nro int not null,
num_serie int not null,
fabricante varchar(80) not null,
instante date not null,
unidades int not null,
tin int not null,
constraint pk_eventorep primary key(ean, nro, num_serie, fabricante, instante),
constraint fk_evrep_plan foreign key(ean, nro, num_serie, fabricante)
references planograma(ean, nro, num_serie, fabricante),
constraint fk_evrep_ret foreign key(tin) references retalhista(tin)
);
--------------------------------------------------
-- Procedure for category deletion --
--------------------------------------------------
drop procedure elimina_categoria(character varying);
create or replace procedure elimina_categoria(cat categoria.nome_categoria%type)
language plpgsql
as $$
declare
num produto.ean%type;
sub_cat categoria.nome_categoria%type;
begin
for num in
select p.ean
from tem_categoria p
where p.nome_categoria = cat
loop
delete from evento_reposicao e where e.ean = num;
delete from planograma p where p.ean = num;
delete from tem_categoria t where t.ean = num;
delete from produto p where p.ean = num;
end loop;
for sub_cat in
WITH RECURSIVE search_tree(super_categoria, nome_categoria) AS (
SELECT super_categoria, nome_categoria
FROM tem_outra
WHERE super_categoria = cat
UNION ALL
SELECT t.super_categoria, t.nome_categoria
FROM tem_outra t, search_tree st
WHERE t.super_categoria = st.nome_categoria
)
VALUES (cat)
UNION ALL
SELECT nome_categoria FROM search_tree
loop
delete from tem_outra t where t.super_categoria = sub_cat or t.nome_categoria = sub_cat;
delete from responsavel_por r where r.nome_categoria = sub_cat;
delete from prateleira p where p.nome_categoria = sub_cat;
delete from super_categoria c where c.nome_categoria = sub_cat;
delete from categoria_simples c where c.nome_categoria = sub_cat;
delete from categoria c where c.nome_categoria = sub_cat;
end loop;
end
$$;