-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathschema.sql
56 lines (47 loc) · 1.68 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
-- Drop tables
DROP TABLE IF EXISTS card_holder CASCADE;
DROP TABLE IF EXISTS credit_card CASCADE;
DROP TABLE IF EXISTS merchant CASCADE;
DROP TABLE IF EXISTS merchant_category CASCADE;
DROP TABLE IF EXISTS transaction CASCADE;
-- Create a table of card holders
CREATE TABLE card_holder (
id int NOT NULL PRIMARY KEY,
Name VARCHAR NOT NULL
);
-- Create a table of credit cards
CREATE TABLE credit_card (
card VARCHAR(20) NOT NULL PRIMARY KEY,
cardholder_id int NOT NULL
);
-- Create a table of merchants
CREATE TABLE merchant (
id int NOT NULL PRIMARY KEY,
name VARCHAR NOT NULL,
id_merchant_category int NOT NULL
);
-- Create a table of merchant categories
CREATE TABLE merchant_category (
id int NOT NULL PRIMARY KEY,
Name VARCHAR NOT NULL
);
-- Create a table of transactions
CREATE TABLE transaction (
id int NOT NULL PRIMARY KEY,
date timestamp NOT NULL,
amount REAL NOT NULL,
card VARCHAR(20) NOT NULL,
id_merchant int NOT NULL
);
-- Add constraint to credit_card table
ALTER TABLE credit_card ADD CONSTRAINT fk_credit_card_cardholder_id FOREIGN KEY(cardholder_id)
REFERENCES card_holder(id);
-- Add constraint to merchant table
ALTER TABLE merchant ADD CONSTRAINT fk_merchant_id_merchant_category FOREIGN KEY(id_merchant_category)
REFERENCES merchant_category(id);
-- Add constraint to transaction table
ALTER TABLE transaction ADD CONSTRAINT fk_transaction_card FOREIGN KEY(card)
REFERENCES credit_card(card);
-- Add constraint to transaction table
ALTER TABLE transaction ADD CONSTRAINT fk_transaction_id_merchant FOREIGN KEY(id_merchant)
REFERENCES merchant(id);