- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtables.sql
More file actions
Latest commit
112 lines (105 loc) · 2.66 KB
/
Copy pathtables.sql
File metadata and controls
112 lines (105 loc) · 2.66 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
-- Creating tables/entities
-- Clients entity
CREATETABLEIF NOT EXISTS clientes(
id BIGSERIALPRIMARY KEY,
nome VARCHAR(50) NOT NULL,
lealdade INTEGERNOT NULL
);
-- Addresses entity
CREATETABLEIF NOT EXISTS enderecos(
id BIGSERIALPRIMARY KEY,
cep VARCHAR(9) NOT NULL,
rua VARCHAR(50) NOT NULL,
numero INTEGERNOT NULL,
bairro VARCHAR(50) NOT NULL,
complemento VARCHAR(100),
cliente_id INTEGER UNIQUE NOT NULL,
FOREIGN KEY (cliente_id) REFERENCES clientes (id) ON DELETE CASCADE
);
-- Orders entity
CREATETABLEIF NOT EXISTS pedidos(
id BIGSERIALPRIMARY KEY,
status VARCHAR(50) NOT NULL,
cliente_id INTEGER UNIQUE NOT NULL,
FOREIGN KEY (cliente_id) REFERENCES clientes (id) ON DELETE CASCADE
);
-- Products entity
CREATETABLEIF NOT EXISTS produtos(
id BIGSERIALPRIMARY KEY,
nome VARCHAR(100) NOT NULL,
tipo VARCHAR(30) NOT NULL,
preco FLOAT(8) NOT NULL,
pts_de_lealdade INTEGERNOT NULL
);
-- Order/Products pivot entity
CREATETABLEIF NOT EXISTS produtos_pedidos(
id BIGSERIALPRIMARY KEY,
pedido_id INTEGERNOT NULL,
produto_id INTEGERNOT NULL,
FOREIGN KEY (pedido_id) REFERENCES pedidos (id) ON DELETE CASCADE,
FOREIGN KEY (produto_id) REFERENCES produtos (id) ON DELETE CASCADE
);
-- Populating
-- clients
INSERT INTO
clientes (nome, lealdade)
VALUES
('Paula', 64),
('Francisco', 168),
('Elise', 24),
('Marcelo', 48),
('Laura', 36);
-- addresses
INSERT INTO
enderecos (cep, rua, numero, bairro, complemento, cliente_id)
VALUES
('09092-909', 'Rua 1', 121, 'Bairro 1', 'apto. 302', 1),
('12345-995', 'Rua 2', 254, 'Bairro 2', null, 2),
('54625-845', 'Rua 3', 1050, 'Bairro 3', null, 3),
('65655-321', 'Rua 4', 79, 'Bairro 4', 'APTO. 505', 4),
('15484-565', 'Rua 5', 32, 'Bairro 5', null, 5);
-- products
INSERT INTO
produtos (nome, tipo, preco, pts_de_lealdade)
VALUES
('Big Serial', 'Burguer', 24.99, 12),
('Varchapa', 'Burguer', 32.99, 16),
('Update sem WHERE', 'Burguer', 42.99, 20),
('Um pra Dois', 'Burguer', 49.99, 24),
('DELETE sem WHERE', 'Burguer', 54.99, 32),
('Fritas', 'Acompanhamento', 14.99, 8),
('Cebola', 'Acompanhamento', 19.99, 12),
('Coca-Cola', 'Bebida', 5.99, 6),
('Fanta', 'Bebida', 5.99, 6),
('Guaraná', 'Bebida', 5.99, 6);
-- orders
INSERT INTO
pedidos (status, cliente_id)
VALUES
('Em preparo', 1),
('Finalizado', 2),
('Recebido', 3),
('Entregue', 4),
('Recebido', 5);
-- order_products
INSERT INTO
produtos_pedidos (pedido_id, produto_id)
VALUES
(1, 4),
(1, 2),
(1, 6),
(2, 2),
(2, 4),
(2, 7),
(2, 9),
(3, 5),
(3, 6),
(3, 9),
(4, 1),
(4, 1),
(4, 6),
(4, 10),
(5, 3),
(5, 2),
(5, 7),
(5, 8);