-
Notifications
You must be signed in to change notification settings - Fork 1
/
sql.sql~
93 lines (76 loc) · 2.21 KB
/
sql.sql~
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
DROP TABLE users CASCADE;
DROP TABLE categories CASCADE;
DROP TABLE products CASCADE;
DROP TABLE sales CASCADE;
/**table 1: [entity] users**/
CREATE TABLE users (
id SERIAL PRIMARY KEY,
name TEXT NOT NULL UNIQUE,
role TEXT NOT NULL,
age INTEGER NOT NULL,
state TEXT NOT NULL
);
/**table 2: [entity] category**/
CREATE TABLE categories (
id SERIAL PRIMARY KEY,
name TEXT NOT NULL UNIQUE,
description TEXT
);
/**table 3: [entity] product**/
CREATE TABLE products (
id SERIAL PRIMARY KEY,
cid INTEGER REFERENCES categories (id) ON DELETE CASCADE,
name TEXT NOT NULL,
SKU TEXT NOT NULL UNIQUE,
price INTEGER NOT NULL
);
/**table 4: [relation] carts**/
CREATE TABLE sales (
id SERIAL PRIMARY KEY,
uid INTEGER REFERENCES users (id) ON DELETE CASCADE,
pid INTEGER REFERENCES products (id) ON DELETE CASCADE,
quantity INTEGER NOT NULL,
price INTEGER NOT NULL
);
SELECT * FROM sales order by id desc;
CREATE TABLE carts (
id SERIAL PRIMARY KEY,
uid INTEGER REFERENCES users (id) ON DELETE CASCADE,
pid INTEGER REFERENCES products (id) ON DELETE CASCADE,
quantity INTEGER NOT NULL,
price INTEGER NOT NULL
);
CREATE TABLE ProdStatePrecomp (
pid INTEGER REFERENCES products (id) ON DELETE CASCADE,
state TEXT NOT NULL,
sumamt INTEGER
);
CREATE TABLE UsersCatPrecomp (
uid INTEGER REFERENCES users (id) ON DELETE CASCADE,
category TEXT NOT NULL,
sumamt INTEGER
);
CREATE TABLE StateCatPrecomp (
state TEXT NOT NULL,
category TEXT NOT NULL,
sumamt INTEGER
);
CREATE TABLE UsersCatProdStatePrecomp (
uid INTEGER REFERENCES users (id) ON DELETE CASCADE,
category TEXT NOT NULL,
pid INTEGER REFERENCES products (id) ON DELETE CASCADE,
state TEXT NOT NULL,
sumamt INTEGER
);
CREATE TABLE UsersPrecomp (
uid INTEGER REFERENCES users (id) ON DELETE CASCADE,
sumamt INTEGER
);
CREATE TABLE ProdPrecomp (
pid INTEGER REFERENCES products (id) ON DELETE CASCADE,
sumamt INTEGER
);
CREATE TABLE StatesPrecomp (
state TEXT NOT NULL,
sumamt INTEGER
);