-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathschema.sql
More file actions
173 lines (158 loc) · 6.06 KB
/
schema.sql
File metadata and controls
173 lines (158 loc) · 6.06 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
-- DDL para um sistema de E-commerce e Gestão de Estoque
-- 1. Tabela de Clientes
CREATE TABLE Customers (
customer_id INT PRIMARY KEY AUTO_INCREMENT,
first_name VARCHAR(50) NOT NULL,
last_name VARCHAR(50) NOT NULL,
email VARCHAR(100) UNIQUE NOT NULL,
phone_number VARCHAR(20),
registration_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
is_active BOOLEAN DEFAULT TRUE
);
-- 2. Tabela de Endereços
CREATE TABLE Addresses (
address_id INT PRIMARY KEY AUTO_INCREMENT,
customer_id INT NOT NULL,
street_address VARCHAR(255) NOT NULL,
city VARCHAR(100) NOT NULL,
state_province VARCHAR(100) NOT NULL,
postal_code VARCHAR(20) NOT NULL,
country VARCHAR(100) NOT NULL,
address_type ENUM('SHIPPING', 'BILLING') NOT NULL,
FOREIGN KEY (customer_id) REFERENCES Customers(customer_id)
);
-- 3. Tabela de Categorias de Produtos
CREATE TABLE Categories (
category_id INT PRIMARY KEY AUTO_INCREMENT,
category_name VARCHAR(100) UNIQUE NOT NULL,
description TEXT
);
-- 4. Tabela de Produtos
CREATE TABLE Products (
product_id INT PRIMARY KEY AUTO_INCREMENT,
product_name VARCHAR(255) NOT NULL,
description TEXT,
price DECIMAL(10, 2) NOT NULL,
stock_quantity INT NOT NULL DEFAULT 0,
category_id INT NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
FOREIGN KEY (category_id) REFERENCES Categories(category_id)
);
-- 5. Tabela de Pedidos
CREATE TABLE Orders (
order_id INT PRIMARY KEY AUTO_INCREMENT,
customer_id INT NOT NULL,
order_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
total_amount DECIMAL(10, 2) NOT NULL,
status ENUM('PENDING', 'PROCESSING', 'DELIVERED', 'CANCELLED') NOT NULL,
shipping_address_id INT,
billing_address_id INT,
FOREIGN KEY (customer_id) REFERENCES Customers(customer_id),
FOREIGN KEY (shipping_address_id) REFERENCES Addresses(address_id),
FOREIGN KEY (billing_address_id) REFERENCES Addresses(address_id)
);
-- 6. Tabela de Itens do Pedido (detalhes de cada produto em um pedido)
CREATE TABLE Order_Items (
order_item_id INT PRIMARY KEY AUTO_INCREMENT,
order_id INT NOT NULL,
product_id INT NOT NULL,
quantity INT NOT NULL,
unit_price DECIMAL(10, 2) NOT NULL,
subtotal DECIMAL(10, 2) NOT NULL,
FOREIGN KEY (order_id) REFERENCES Orders(order_id),
FOREIGN KEY (product_id) REFERENCES Products(product_id)
);
-- 7. Tabela de Pagamentos
CREATE TABLE Payments (
payment_id INT PRIMARY KEY AUTO_INCREMENT,
order_id INT NOT NULL,
payment_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
amount DECIMAL(10, 2) NOT NULL,
payment_method VARCHAR(50) NOT NULL,
transaction_id VARCHAR(100) UNIQUE NOT NULL,
status ENUM('SUCCESS', 'FAILED', 'PENDING') NOT NULL,
FOREIGN KEY (order_id) REFERENCES Orders(order_id)
);
-- 8. Tabela de Fornecedores
CREATE TABLE Suppliers (
supplier_id INT PRIMARY KEY AUTO_INCREMENT,
supplier_name VARCHAR(255) NOT NULL,
contact_person VARCHAR(100),
email VARCHAR(100) UNIQUE,
phone_number VARCHAR(20),
address TEXT
);
-- 9. Tabela de Entradas de Estoque (Recebimento de produtos de fornecedores)
CREATE TABLE Stock_Receipts (
receipt_id INT PRIMARY KEY AUTO_INCREMENT,
supplier_id INT NOT NULL,
product_id INT NOT NULL,
quantity INT NOT NULL,
receipt_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
cost_per_unit DECIMAL(10, 2),
FOREIGN KEY (supplier_id) REFERENCES Suppliers(supplier_id),
FOREIGN KEY (product_id) REFERENCES Products(product_id)
);
-- 10. Tabela de Devoluções de Produtos
CREATE TABLE Returns (
return_id INT PRIMARY KEY AUTO_INCREMENT,
order_item_id INT NOT NULL,
return_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
quantity INT NOT NULL,
reason TEXT,
status ENUM('PENDING', 'APPROVED', 'REJECTED', 'COMPLETED') NOT NULL,
refund_amount DECIMAL(10, 2),
FOREIGN KEY (order_item_id) REFERENCES Order_Items(order_item_id)
);
-- 11. Tabela de Avaliações de Produtos
CREATE TABLE Product_Reviews (
review_id INT PRIMARY KEY AUTO_INCREMENT,
product_id INT NOT NULL,
customer_id INT NOT NULL,
rating INT CHECK (rating >= 1 AND rating <= 5) NOT NULL,
comment TEXT,
review_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (product_id) REFERENCES Products(product_id),
FOREIGN KEY (customer_id) REFERENCES Customers(customer_id)
);
-- 12. Tabela de Carrinhos de Compras (Itens no carrinho de um cliente)
CREATE TABLE Shopping_Carts (
cart_id INT PRIMARY KEY AUTO_INCREMENT,
customer_id INT UNIQUE NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
FOREIGN KEY (customer_id) REFERENCES Customers(customer_id)
);
-- 13. Tabela de Itens do Carrinho
CREATE TABLE Cart_Items (
cart_item_id INT PRIMARY KEY AUTO_INCREMENT,
cart_id INT NOT NULL,
product_id INT NOT NULL,
quantity INT NOT NULL,
added_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (cart_id) REFERENCES Shopping_Carts(cart_id),
FOREIGN KEY (product_id) REFERENCES Products(product_id),
UNIQUE (cart_id, product_id) -- Garante que um produto só aparece uma vez no carrinho
);
-- 14. Tabela de Descontos/Cupons
CREATE TABLE Discounts (
discount_id INT PRIMARY KEY AUTO_INCREMENT,
code VARCHAR(50) UNIQUE NOT NULL,
type ENUM('PERCENTAGE', 'FIXED') NOT NULL,
value DECIMAL(5, 2) NOT NULL,
min_order_amount DECIMAL(10, 2) DEFAULT 0.00,
start_date DATE NOT NULL,
end_date DATE NOT NULL,
is_active BOOLEAN DEFAULT TRUE
);
-- 15. Tabela de Histórico de Descontos Aplicados (quais descontos foram usados em quais pedidos)
CREATE TABLE Order_Discounts (
order_discount_id INT PRIMARY KEY AUTO_INCREMENT,
order_id INT NOT NULL,
discount_id INT NOT NULL,
amount_applied DECIMAL(10, 2) NOT NULL,
FOREIGN KEY (order_id) REFERENCES Orders(order_id),
FOREIGN KEY (discount_id) REFERENCES Discounts(discount_id),
UNIQUE (order_id, discount_id) -- Evita aplicar o mesmo desconto duas vezes no mesmo pedido
);