-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetupDB.sql
More file actions
55 lines (43 loc) · 1.71 KB
/
setupDB.sql
File metadata and controls
55 lines (43 loc) · 1.71 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
CREATE TABLE "users" (
"id" INTEGER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
"email" varchar UNIQUE,
"user_name" varchar UNIQUE NOT NULL,
"password" varchar NOT NULL
);
CREATE TABLE "products" (
"id" INTEGER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
"name" varchar NOT NULL,
"price" decimal NOT NULL
);
CREATE TABLE "orders" (
"id" INTEGER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
"status" varchar NOT NULL,
"user_id" integer NOT NULL,
"total" decimal NOT NULL,
"created" timestamp NOT NULL,
"modified" timestamp
);
CREATE TABLE "carts" (
"id" INTEGER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
"user_id" integer NOT NULL,
"created" timestamp
);
CREATE TABLE "carts_products" (
"id" INTEGER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
"cart_id" integer NOT NULL,
"product_id" integer NOT NULL,
"qty" integer NOT NULL
);
CREATE TABLE "orders_products" (
"id" INTEGER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
"product_id" integer NOT NULL,
"order_id" integer NOT NULL,
"qty" integer NOT NULL
);
ALTER TABLE "orders" ADD FOREIGN KEY ("user_id") REFERENCES "users" ("id") ON DELETE CASCADE;
ALTER TABLE "orders" ADD FOREIGN KEY ("cart_id") REFERENCES "carts" ("id") ON DELETE CASCADE;
ALTER TABLE "carts" ADD FOREIGN KEY ("user_id") REFERENCES "users" ("id") ON DELETE CASCADE;
ALTER TABLE "carts_products" ADD FOREIGN KEY ("cart_id") REFERENCES "carts" ("id") ON DELETE CASCADE;
ALTER TABLE "carts_products" ADD FOREIGN KEY ("product_id") REFERENCES "products" ("id");
ALTER TABLE "orders_products" ADD FOREIGN KEY ("product_id") REFERENCES "products" ("id");
ALTER TABLE "orders_products" ADD FOREIGN KEY ("order_id") REFERENCES "orders" ("id") ON DELETE CASCADE;