-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCreate-DB-query.txt
67 lines (67 loc) · 2.96 KB
/
Create-DB-query.txt
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
DROP TABLE IF EXISTS "public"."bank_user";
DROP TABLE IF EXISTS "public"."bank_role";
DROP TABLE IF EXISTS "public"."bank_branch";
DROP TABLE IF EXISTS "public"."bank_loan";
DROP TABLE IF EXISTS "public"."bank_currency";
DROP TABLE IF EXISTS "public"."bank_account";
DROP TABLE IF EXISTS "public"."bank_transaction";
CREATE TABLE "public"."bank_user" (
"id" SERIAL,
"first_name" VARCHAR(50) NOT NULL,
"last_name" VARCHAR(50) NOT NULL,
"pin_code" INTEGER NOT NULL,
"role_id" INTEGER NOT NULL,
"branch_id" INTEGER NOT NULL,
"bank_email" VARCHAR(250) NOT NULL,
CONSTRAINT "user_pkey" PRIMARY KEY ("id"),
CONSTRAINT "unique_email" UNIQUE ("bank_email")
);
CREATE TABLE "public"."bank_role" (
"id" SERIAL,
"name" VARCHAR(20) NOT NULL,
"is_admin" BOOLEAN NOT NULL,
"is_client" BOOLEAN NOT NULL,
CONSTRAINT "bank_role_pkey" PRIMARY KEY ("id")
);
CREATE TABLE "public"."bank_branch" (
"id" SERIAL,
"name" VARCHAR(20) NOT NULL,
CONSTRAINT "bank_branch_pkey" PRIMARY KEY ("id")
);
CREATE TABLE "public"."bank_loan" (
"id" SERIAL,
"name" VARCHAR(20) NOT NULL,
"interest_rate" NUMERIC NOT NULL,
"user_id" INTEGER NOT NULL,
CONSTRAINT "bank_loan_pkey" PRIMARY KEY ("id")
);
CREATE TABLE "public"."bank_currency" (
"id" SERIAL,
"name" VARCHAR(3) NOT NULL,
"exchange_rate" NUMERIC NOT NULL,
CONSTRAINT "bank_currency_pkey" PRIMARY KEY ("id")
);
CREATE TABLE "public"."bank_account" (
"id" SERIAL,
"name" VARCHAR(50) NOT NULL,
"balance" NUMERIC NOT NULL DEFAULT 0 ,
"interest_rate" NUMERIC NOT NULL DEFAULT 0 ,
"user_id" INTEGER NOT NULL,
"currency_id" INTEGER NOT NULL DEFAULT 2 ,
CONSTRAINT "account_pkey" PRIMARY KEY ("id")
);
CREATE TABLE "public"."bank_transaction" (
"id" SERIAL,
"name" VARCHAR(50) NOT NULL,
"from_account_id" INTEGER NULL,
"to_account_id" INTEGER NULL,
"timestamp" TIMESTAMP NOT NULL DEFAULT now() ,
"amount" NUMERIC NOT NULL,
CONSTRAINT "transactions_pkey" PRIMARY KEY ("id")
);
ALTER TABLE "public"."bank_user" ADD CONSTRAINT "fk_user_role" FOREIGN KEY ("role_id") REFERENCES "public"."bank_role" ("id") ON DELETE NO ACTION ON UPDATE NO ACTION;
ALTER TABLE "public"."bank_user" ADD CONSTRAINT "fk_user_branch" FOREIGN KEY ("branch_id") REFERENCES "public"."bank_branch" ("id") ON DELETE NO ACTION ON UPDATE NO ACTION;
ALTER TABLE "public"."bank_account" ADD CONSTRAINT "fk_currency" FOREIGN KEY ("currency_id") REFERENCES "public"."bank_currency" ("id") ON DELETE NO ACTION ON UPDATE NO ACTION;
ALTER TABLE "public"."bank_account" ADD CONSTRAINT "fk_user" FOREIGN KEY ("user_id") REFERENCES "public"."bank_user" ("id") ON DELETE NO ACTION ON UPDATE NO ACTION;
ALTER TABLE "public"."bank_transaction" ADD CONSTRAINT "fk_from_account" FOREIGN KEY ("from_account_id") REFERENCES "public"."bank_account" ("id") ON DELETE NO ACTION ON UPDATE NO ACTION;
ALTER TABLE "public"."bank_transaction" ADD CONSTRAINT "fk_to_account" FOREIGN KEY ("to_account_id") REFERENCES "public"."bank_account" ("id") ON DELETE NO ACTION ON UPDATE NO ACTION;