-
Notifications
You must be signed in to change notification settings - Fork 1
/
sql.sql
54 lines (42 loc) · 1.31 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
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
);