-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathschema.sql
More file actions
64 lines (56 loc) · 1.86 KB
/
Copy pathschema.sql
File metadata and controls
64 lines (56 loc) · 1.86 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
CREATE TYPE sex AS enum ('F', 'M');
CREATE TABLE users (
id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
email VARCHAR(255) UNIQUE NOT NULL,
user_name varchar(100) UNIQUE NOT NULL,
pwd varchar(255) NOT NULL,
active boolean DEFAULT FALSE,
activation_code varchar(255),
activation_expiry timestamp,
admin boolean DEFAULT FALSE,
first_name VARCHAR(100),
last_name VARCHAR(100),
user_sex sex,
birthday date,
img_path varchar(255)
);
CREATE TABLE categories (
id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
name VARCHAR(100) NOT NULL UNIQUE,
description TEXT
);
CREATE TABLE products_categories (
category_id INTEGER REFERENCES categories(id) ON DELETE SET NULL,
product_id INTEGER REFERENCES products(id) ON DELETE SET NULL,
PRIMARY KEY(category_id, product_id)
);
CREATE TABLE products (
id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
sku VARCHAR(50) UNIQUE NOT NULL,
name VARCHAR(255) NOT NULL,
price DECIMAL(12, 2) NOT NULL,
description TEXT,
stock_quantity INTEGER DEFAULT 0,
img_path varchar(255)
);
CREATE TYPE order_status AS ENUM ('pending', 'processing', 'shipped', 'delivered', 'cancelled');
CREATE TABLE orders (
id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
total_amount DECIMAL(12, 2) NOT NULL,
status order_status DEFAULT 'pending',
shipping_address TEXT NOT NULL,
user_id INTEGER REFERENCES users(id) ON DELETE CASCADE
);
CREATE TABLE order_items (
order_id INTEGER REFERENCES orders(id) ON DELETE CASCADE,
product_id INTEGER REFERENCES products(id),
PRIMARY KEY(order_id, product_id),
quantity INTEGER NOT NULL CHECK (quantity > 0),
price_at_purchase DECIMAL(12, 2) NOT NULL
);
CREATE TABLE cart_items (
user_id INTEGER REFERENCES users(id) ON DELETE CASCADE,
product_id INTEGER REFERENCES products(id) ON DELETE CASCADE,
PRIMARY KEY(user_id, product_id),
quantity INTEGER DEFAULT 1
);