| 1 | |
| 2 | {{{#!sql |
| 3 | -- Create USER table |
| 4 | CREATE TABLE USER ( |
| 5 | user_id SERIAL PRIMARY KEY, |
| 6 | user_name VARCHAR(50) NOT NULL, |
| 7 | email VARCHAR(100) NOT NULL, |
| 8 | password VARCHAR(100) NOT NULL |
| 9 | ); |
| 10 | |
| 11 | -- Create TRANSACTION_ACCOUNT table |
| 12 | CREATE TABLE TRANSACTION_ACCOUNT ( |
| 13 | transaction_account_id SERIAL PRIMARY KEY, |
| 14 | account_name VARCHAR(50), |
| 15 | balance DECIMAL(10, 2), |
| 16 | user_id INT REFERENCES USER(user_id) |
| 17 | ); |
| 18 | |
| 19 | -- Create TRANSACTION table |
| 20 | CREATE TABLE TRANSACTION ( |
| 21 | transaction_id SERIAL PRIMARY KEY, |
| 22 | transaction_name VARCHAR(100), |
| 23 | amount DECIMAL(10, 2) NOT NULL, |
| 24 | net_amount DECIMAL(10, 2), |
| 25 | day_id INT REFERENCES DAY(day_id), |
| 26 | tag_id INT REFERENCES TAG(tag_id) |
| 27 | ); |
| 28 | |
| 29 | -- Create TRANSACTION_BREAKDOWN table |
| 30 | CREATE TABLE TRANSACTION_BREAKDOWN ( |
| 31 | transaction_breakdown_id SERIAL PRIMARY KEY, |
| 32 | transaction_id INT REFERENCES TRANSACTION(transaction_id), |
| 33 | transaction_account_id INT REFERENCES TRANSACTION_ACCOUNT(transaction_account_id), |
| 34 | spent_amount DECIMAL(10, 2), |
| 35 | earned_amount DECIMAL(10, 2) |
| 36 | ); |
| 37 | |
| 38 | -- Create YEAR table |
| 39 | CREATE TABLE YEAR ( |
| 40 | year_id SERIAL PRIMARY KEY, |
| 41 | year_name INT NOT NULL |
| 42 | ); |
| 43 | |
| 44 | -- Create MONTH table |
| 45 | CREATE TABLE MONTH ( |
| 46 | month_id SERIAL PRIMARY KEY, |
| 47 | year_id INT REFERENCES YEAR(year_id), |
| 48 | month_name VARCHAR(20) NOT NULL |
| 49 | ); |
| 50 | |
| 51 | -- Create DAY table |
| 52 | CREATE TABLE DAY ( |
| 53 | day_id SERIAL PRIMARY KEY, |
| 54 | month_id INT REFERENCES MONTH(month_id), |
| 55 | day_number INT, |
| 56 | day_name VARCHAR(20) |
| 57 | ); |
| 58 | |
| 59 | -- Create TAG table |
| 60 | CREATE TABLE TAG ( |
| 61 | tag_id SERIAL PRIMARY KEY, |
| 62 | tag_name VARCHAR(50) NOT NULL |
| 63 | ); |
| 64 | |
| 65 | -- Delete tables if they exist |
| 66 | DROP TABLE IF EXISTS TRANSACTION_BREAKDOWN CASCADE; |
| 67 | DROP TABLE IF EXISTS TRANSACTION CASCADE; |
| 68 | DROP TABLE IF EXISTS TRANSACTION_ACCOUNT CASCADE; |
| 69 | DROP TABLE IF EXISTS DAY CASCADE; |
| 70 | DROP TABLE IF EXISTS MONTH CASCADE; |
| 71 | DROP TABLE IF EXISTS YEAR CASCADE; |
| 72 | DROP TABLE IF EXISTS TAG CASCADE; |
| 73 | DROP TABLE IF EXISTS USER CASCADE; |
| 74 | }}} |