-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.sql
More file actions
129 lines (117 loc) · 4.82 KB
/
Copy pathdatabase.sql
File metadata and controls
129 lines (117 loc) · 4.82 KB
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
-- MySQL database schema for Budget Tracker Application
-- Create database
CREATE DATABASE IF NOT EXISTS budget_tracker;
USE budget_tracker;
-- Create users table
CREATE TABLE IF NOT EXISTS users (
user_id INT AUTO_INCREMENT PRIMARY KEY,
username VARCHAR(50) NOT NULL UNIQUE,
password VARCHAR(255) NOT NULL, -- Store hashed passwords
email VARCHAR(100) NOT NULL UNIQUE,
first_name VARCHAR(50) NOT NULL,
last_name VARCHAR(50) NOT NULL,
role ENUM('admin', 'user') NOT NULL DEFAULT 'user',
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
reset_token VARCHAR(100),
reset_token_expiry TIMESTAMP NULL DEFAULT NULL
);
-- Create income_categories table
CREATE TABLE IF NOT EXISTS income_categories (
category_id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(50) NOT NULL,
description VARCHAR(255),
created_by INT,
is_system BOOLEAN DEFAULT FALSE, -- System categories can't be deleted
FOREIGN KEY (created_by) REFERENCES users(user_id) ON DELETE SET NULL
);
-- Create expense_categories table
CREATE TABLE IF NOT EXISTS expense_categories (
category_id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(50) NOT NULL,
description VARCHAR(255),
created_by INT,
is_system BOOLEAN DEFAULT FALSE, -- System categories can't be deleted
FOREIGN KEY (created_by) REFERENCES users(user_id) ON DELETE SET NULL
);
-- Create incomes table
CREATE TABLE IF NOT EXISTS incomes (
income_id INT AUTO_INCREMENT PRIMARY KEY,
user_id INT NOT NULL,
category_id INT NOT NULL,
amount DECIMAL(10, 2) NOT NULL,
description VARCHAR(255),
income_date DATE NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (user_id) REFERENCES users(user_id) ON DELETE CASCADE,
FOREIGN KEY (category_id) REFERENCES income_categories(category_id)
);
-- Create expenses table
CREATE TABLE IF NOT EXISTS expenses (
expense_id INT AUTO_INCREMENT PRIMARY KEY,
user_id INT NOT NULL,
category_id INT NOT NULL,
amount DECIMAL(10, 2) NOT NULL,
description VARCHAR(255),
expense_date DATE NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (user_id) REFERENCES users(user_id) ON DELETE CASCADE,
FOREIGN KEY (category_id) REFERENCES expense_categories(category_id)
);
-- Create budgets table
CREATE TABLE IF NOT EXISTS budgets (
budget_id INT AUTO_INCREMENT PRIMARY KEY,
user_id INT NOT NULL,
category_id INT NOT NULL,
amount DECIMAL(10, 2) NOT NULL,
start_date DATE NOT NULL,
end_date DATE NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (user_id) REFERENCES users(user_id) ON DELETE CASCADE,
FOREIGN KEY (category_id) REFERENCES expense_categories(category_id)
);
-- Create savings table
CREATE TABLE IF NOT EXISTS savings (
saving_id INT AUTO_INCREMENT PRIMARY KEY,
user_id INT NOT NULL,
name VARCHAR(100) NOT NULL,
description VARCHAR(255),
target_amount DECIMAL(10, 2) NOT NULL,
current_amount DECIMAL(10, 2) DEFAULT 0.00,
target_date DATE NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (user_id) REFERENCES users(user_id) ON DELETE CASCADE
);
-- Create savings_goals table
CREATE TABLE IF NOT EXISTS savings_goals (
goal_id INT AUTO_INCREMENT PRIMARY KEY,
user_id INT NOT NULL,
name VARCHAR(100) NOT NULL,
target_amount DECIMAL(10, 2) NOT NULL,
current_amount DECIMAL(10, 2) DEFAULT 0,
target_date DATE,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (user_id) REFERENCES users(user_id) ON DELETE CASCADE
);
-- Insert default admin user
INSERT INTO users (username, password, email, first_name, last_name, role)
VALUES ('admin', '$2a$10$hKDVYxLefVHV/vtuPhWD3OigtRyOykRLDdUAp80Z1crSoS1lFqaFS', 'admin@budgettracker.com', 'Admin', 'User', 'admin');
-- Default password is 'admin123' (bcrypt hashed)
-- Insert default categories
INSERT INTO income_categories (name, description, is_system) VALUES
('Salary', 'Regular employment income', TRUE),
('Freelance', 'Income from freelance work', TRUE),
('Investments', 'Income from investments', TRUE),
('Gifts', 'Money received as gifts', TRUE),
('Other', 'Other income sources', TRUE);
INSERT INTO expense_categories (name, description, is_system) VALUES
('Housing', 'Rent, mortgage, repairs, etc.', TRUE),
('Utilities', 'Electricity, water, internet, etc.', TRUE),
('Groceries', 'Food and household supplies', TRUE),
('Transportation', 'Car payments, fuel, public transit, etc.', TRUE),
('Entertainment', 'Movies, dining out, etc.', TRUE),
('Health', 'Medical expenses, insurance, etc.', TRUE),
('Education', 'Tuition, books, courses, etc.', TRUE),
('Debt', 'Credit card payments, loans, etc.', TRUE),
('Savings', 'Money set aside for future', TRUE),
('Other', 'Miscellaneous expenses', TRUE);