-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathschema.sql
74 lines (57 loc) · 1.94 KB
/
schema.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
-- Exported from QuickDBD: https://www.quickdatabasediagrams.com/
-- Link to schema: https://app.quickdatabasediagrams.com/#/d/s8Wnqm
-- NOTE! If you have used non-SQL datatypes in your design, you will have to change these here.
--Card Holder
CREATE TABLE "card_holder" (
"id" SERIAL NOT NULL,
"name" VARCHAR(50) NOT NULL,
CONSTRAINT "pk_card_holder" PRIMARY KEY (
"id"
)
);
-- Credit Card
CREATE TABLE "credit_card" (
"card" VARCHAR(20) NOT NULL,
"id_card_holder" INT NOT NULL,
CONSTRAINT "pk_credit_card" PRIMARY KEY (
"card"
)
);
-- Merchant
CREATE TABLE "merchant" (
"id" SERIAL NOT NULL,
"name" VARCHAR(255) NOT NULL,
"id_merchant_category" INT NOT NULL,
CONSTRAINT "pk_merchant" PRIMARY KEY (
"id"
)
);
-- Merchant Category
CREATE TABLE "merchant_category" (
"id" SERIAL NOT NULL,
"name" VARCHAR(50) NOT NULL,
CONSTRAINT "pk_merchant_category" PRIMARY KEY (
"id"
)
);
-- Transaction
CREATE TABLE "transaction" (
"id" INT NOT NULL,
"date" TIMESTAMP NOT NULL,
"amount" FLOAT NOT NULL,
"card" VARCHAR(20) NOT NULL,
"id_merchant" INT NOT NULL,
CONSTRAINT "pk_transaction" PRIMARY KEY (
"id"
)
);
ALTER TABLE "credit_card" ADD CONSTRAINT "fk_credit_card_id_card_holder" FOREIGN KEY("id_card_holder")
REFERENCES "card_holder" ("id");
ALTER TABLE "credit_card" ADD CONSTRAINT "check_credit_card_length" CHECK (char_length("card") <= 20);
--ALTER TABLE "credit_card" DROP CONSTRAINT check_credit_card_length
ALTER TABLE "merchant" ADD CONSTRAINT "fk_merchant_id_merchant_category" FOREIGN KEY("id_merchant_category")
REFERENCES "merchant_category" ("id");
ALTER TABLE "transaction" ADD CONSTRAINT "fk_transaction_card" FOREIGN KEY("card")
REFERENCES "credit_card" ("card");
ALTER TABLE "transaction" ADD CONSTRAINT "fk_transaction_id_merchant" FOREIGN KEY("id_merchant")
REFERENCES "merchant" ("id");