| 1 | |
| 2 | {{{#!sql |
| 3 | -- Delete tables if they exist |
| 4 | DROP TABLE IF EXISTS transaction_breakdown CASCADE; |
| 5 | DROP TABLE IF EXISTS transaction CASCADE; |
| 6 | DROP TABLE IF EXISTS transaction_account CASCADE; |
| 7 | DROP TABLE IF EXISTS tag CASCADE; |
| 8 | DROP TABLE IF EXISTS "user" CASCADE; |
| 9 | |
| 10 | -- Create USER table |
| 11 | CREATE TABLE "user" ( |
| 12 | user_id SERIAL PRIMARY KEY, |
| 13 | user_name VARCHAR(50) NOT NULL, |
| 14 | email VARCHAR(100) NOT NULL, |
| 15 | password VARCHAR(100) NOT NULL |
| 16 | ); |
| 17 | |
| 18 | -- Create TRANSACTION_ACCOUNT table |
| 19 | CREATE TABLE transaction_account ( |
| 20 | transaction_account_id SERIAL PRIMARY KEY, |
| 21 | account_name VARCHAR(50), |
| 22 | balance DECIMAL(10, 2), |
| 23 | user_id INT REFERENCES "user"(user_id) |
| 24 | ); |
| 25 | |
| 26 | -- Create TAG table |
| 27 | CREATE TABLE tag ( |
| 28 | tag_id SERIAL PRIMARY KEY, |
| 29 | tag_name VARCHAR(50) NOT NULL |
| 30 | ); |
| 31 | |
| 32 | -- Create TRANSACTION table |
| 33 | CREATE TABLE transaction ( |
| 34 | transaction_id SERIAL PRIMARY KEY, |
| 35 | transaction_name VARCHAR(100), |
| 36 | amount DECIMAL(10, 2) NOT NULL, |
| 37 | net_amount DECIMAL(10, 2), |
| 38 | date TIMESTAMPTZ, |
| 39 | tag_id INT REFERENCES tag(tag_id) |
| 40 | ); |
| 41 | |
| 42 | -- Create TRANSACTION_BREAKDOWN table |
| 43 | CREATE TABLE transaction_breakdown ( |
| 44 | transaction_breakdown_id SERIAL PRIMARY KEY, |
| 45 | transaction_id INT REFERENCES transaction(transaction_id), |
| 46 | transaction_account_id INT REFERENCES transaction_account(transaction_account_id), |
| 47 | spent_amount DECIMAL(10, 2), |
| 48 | earned_amount DECIMAL(10, 2) |
| 49 | ); |
| 50 | }}} |