-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsql.sql
More file actions
257 lines (228 loc) · 8.34 KB
/
sql.sql
File metadata and controls
257 lines (228 loc) · 8.34 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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
CREATE TABLE usuarios(
id int PRIMARY KEY AUTO_INCREMENT,
nome varchar(150),
email varchar(250),
cpf varchar(11),
senha varchar(32)
);
CREATE TABLE categorias(
id int PRIMARY KEY AUTO_INCREMENT,
nome varchar(150),
);
CREATE TABLE subcategorias(
id int PRIMARY KEY AUTO_INCREMENT,
nome varchar(150),
id_categoria int,
FOREIGN KEY id_categoria REFERENCES categorias(id)
);
CREATE TABLE Produtos(
id int PRIMARY KEY AUTO_INCREMENT,
nome varchar(150),
id_categoria int,
id_subcategoria int,
descricao varchar(250),
imagem varchar(250),
valor double(17,2),
quantidade int,
FOREIGN KEY (id_categoria) REFERENCES categorias(id),
FOREIGN KEY (id_subcategoria) REFERENCES subcategorias(id)
);
CREATE TABLE itens_carrinho(
id int PRIMARY KEY AUTO_INCREMENT,
id_produtos int,
id_usuario int,
data_hora TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (id_produtos) REFERENCES produtos(id),
FOREIGN KEY (id_usuario) REFERENCES usuarios(id)
);
ALTER TABLE itens_carrinho
ADD transportadora INT NOT NULL AFTER `quantidade`,
ADD CONSTRAINT FK_transportadora FOREIGN KEY(transportadora)
REFERENCES transportadoras(id);
CREATE TABLE transportadoras(
id int PRIMARY KEY AUTO_INCREMENT,
nome VARCHAR(250),
valor DOUBLE(16,2)
);
CREATE TABLE endereco(
id int PRIMARY KEY AUTO_INCREMENT,
id_usuario int,
nome varchar(250),
sobrenome varchar(250),
telefone varchar(15),
cep varchar(20),
id_cidade int,
id_estado int,
rua varchar(250),
bairro varchar(250),
numero int,
complemento varchar(250),
pais varchar(60),
FOREIGN KEY (id_usuario) REFERENCES usuarios(id),
FOREIGN KEY (id_cidade) REFERENCES cidades(id),
FOREIGN KEY (id_estado) REFERENCES estados(id)
);
CREATE TABLE pedidos(
id int PRIMARY KEY AUTO_INCREMENT,
id_usuario int,
id_endereco int,
total DOUBLE(16,2),
estatus varchar(15),
data TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (id_usuario) REFERENCES usuarios(id),
FOREIGN KEY (id_endereco) REFERENCES enderecos(id)
);
CREATE TABLE itens_pedido(
id int PRIMARY KEY AUTO_INCREMENT,
id_pedido int,
id_produto int,
quantidade DOUBLE(16,2),
data TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (id_pedido) REFERENCES pedidos(id),
FOREIGN KEY (id_produto) REFERENCES produtos(id)
);
$query = "
SELECT
i.id,
i.id_produtos,
i.id_usuario,
i.quantidade,
i.total,
i.data_hora,
p.imagem,
p.nome,
p.valor
FROM
itens_carrinho AS i
INNER JOIN
produtos AS p
ON
i.id_produtos = p.id
WHERE
id_usuario = :id_usuario
ORDER BY id
";
DELIMITER $$
CREATE PROCEDURE add_carrinho (IN id_produto int, IN id_usuario int, IN tamanho varchar(3), IN quantidade int, IN valor_unit decimal(10,2))
BEGIN
INSERT INTO `itens_carrinho` (`id`,
`id_produtos`,
`id_usuario`,
`tamanho`,
`quantidade`,
`valor_unit`,
`total`,
`data_hora`) VALUES (NULL,
id_produto,
id_usuario,
tamanho,
quantidade,
valor_unit,
valor_unit * quantidade,
current_timestamp());
END $$
DELIMITER ;
/*Select com os valores do carrinho*/
SELECT SUM(i.total) AS total_carrinho, t.valor AS frete, SUM(i.total) + t.valor AS total
FROM itens_carrinho AS i
INNER JOIN
transportadoras AS t
WHERE i.id_usuario = 6 AND t.id = i.transportadora
CALL add_carrinho (id_produto, id_usuario, tamanho, quantidade, transportadora, valor_unit)
/*Insert endereço*/
DELIMITER $$
CREATE PROCEDURE add_endereco (IN id_usuario int, IN nome varchar(250), IN sobrenome varchar(250), IN telefone varchar(15), IN cep varchar(20), IN cidade varchar(150), IN id_estado int, IN rua varchar(250), IN bairro varchar(250), IN numero int, IN complemento varchar(250), IN id_pais int)
BEGIN
SET @ordem = (SELECT CASE WHEN MAX(ordem) IS NULL THEN 1 ELSE MAX(ordem)+1 END FROM enderecos);
INSERT INTO `enderecos` (`id`,
`id_usuario`,
`nome`,
`sobrenome`,
`telefone`,
`cep`,
`cidade`,
`id_estado`,
`rua`,
`bairro`,
`numero`,
`complemento`,
`id_pais`,
`ordem`,
`status`) VALUES (NULL,
id_usuario,
nome,
sobrenome,
telefone,
cep,
cidade,
id_estado,
rua,
bairro,
numero,
complemento,
id_pais,
@ordem,
'ativo');
END $$
DELIMITER ;
CALL add_endereco (6, 'Thiago', 'Augusto', '986539475', '86200000', 'Ibiporã', 1, 'Rua das amoreiras', 'Centro', 1580, 'Casa azul', 'Brasil')
/*add pedido*/
DELIMITER $$
CREATE PROCEDURE add_pedido (IN id_usuario int)
BEGIN
DECLARE id_usuario INT;
DECLARE endereco INT;
DECLARE total DOUBLE(16,2);
DECLARE id_transportadora INT;
DECLARE valor_t DOUBLE(16,2);
SET FOREIGN_KEY_CHECKS = OFF;
SELECT id_usuario = id_usuario, endereco = endereco, total = total, id_transportadora = transportadora FROM itens_carrinho WHERE id_usuario = id_usuario GROUP BY id_usuario limit 1;
SELECT valor_t = valor from transportadoras WHERE id = id_transportadora;
INSERT INTO pedidos (id_usuario, id_endereco, id_transportadora, total, status) VALUES (@id_usuario, @endereco, @id_transportadora, @total + @valor_t, 'pago');
SET FOREIGN_KEY_CHECKS = ON;
END $$
DELIMITER ;
CALL add_pedido(6)
SET FOREIGN_KEY_CHECKS = OFF;
ALTER TABLE enderecos ADD CONSTRAINT fk_id_pais FOREIGN KEY (id_pais) REFERENCES pais(id)
/*Seleciona todas as foregin key do banco*/
select *
from information_schema.referential_constraints
where constraint_schema = 'ecommerce'
/*Exclui a procedure*/
DROP PROCEDURE nome_da_procedure
SELECT e.id as id,
e.id_usuario as id_usuario,
e.nome as nome,
e.sobrenome as sobrenome,
e.telefone as telefone,
e.cep as cep,
e.cidade as cidade,
e.id_estado as id_estado,
t.nome as estado,
e.rua as rua,
e.bairro as bairro,
e.numero as numero,
e.complemento as complemento,
e.id_pais as id_pais,
p.nome AS pais,
e.ordem as ordem,
e.status as status
FROM
enderecos AS e
INNER JOIN
estados AS t ON e.id_estado = t.id
INNER JOIN
pais AS p ON e.id_pais = p.id
WHERE
e.id_usuario LIKE 6
ORDER BY e.id
/*adiciona pedido*/
DELIMITER $$
CREATE PROCEDURE add_pedido (IN id_usuario int, IN id_endereco int, IN id_transportadora int, IN total DOUBLE(16,2))
BEGIN
SET @id = (SELECT CASE WHEN MAX(id) IS NULL THEN 1 ELSE MAX(id)+1 END FROM pedidos);
INSERT INTO pedidos (id, id_usuario, id_endereco, id_transportadora, total, status) VALUES (@id, id_usuario, id_endereco, id_transportadora, total, '');
END $$
DELIMITER ;
call add_pedido (6, 11, 1, 756.20)