-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit_database.sql
More file actions
167 lines (150 loc) · 5.41 KB
/
init_database.sql
File metadata and controls
167 lines (150 loc) · 5.41 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
-- goblog 数据库初始化脚本
-- 使用 root 用户运行此脚本
-- 1. 创建数据库(如果不存在)
CREATE DATABASE IF NOT EXISTS goblog CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
-- 2. 选择数据库
USE goblog;
-- 3. 创建用户表
CREATE TABLE IF NOT EXISTS users (
id INT PRIMARY KEY AUTO_INCREMENT,
username VARCHAR(255) UNIQUE NOT NULL,
email VARCHAR(255) UNIQUE NOT NULL,
password VARCHAR(255) NOT NULL,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP
);
-- 4. 创建分类表
CREATE TABLE IF NOT EXISTS categories (
id INT PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(255) NOT NULL,
type VARCHAR(50) NOT NULL,
icon VARCHAR(50),
color VARCHAR(50),
is_default INT DEFAULT 0,
is_custom INT DEFAULT 0,
sort_order INT DEFAULT 0,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP
);
-- 5. 创建交易表
CREATE TABLE IF NOT EXISTS transactions (
id INT PRIMARY KEY AUTO_INCREMENT,
user_id INT NOT NULL,
type VARCHAR(50),
category_id INT,
category VARCHAR(255),
amount DECIMAL(10,2),
date DATETIME,
note TEXT,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY(category_id) REFERENCES categories(id),
FOREIGN KEY(user_id) REFERENCES users(id)
);
-- 6. 创建财务目标表
CREATE TABLE IF NOT EXISTS finance_goals (
id INT PRIMARY KEY AUTO_INCREMENT,
user_id INT NOT NULL,
type VARCHAR(50),
target_amount DECIMAL(10,2),
start_date DATETIME,
end_date DATETIME,
FOREIGN KEY(user_id) REFERENCES users(id)
);
-- 7. 创建习惯表
CREATE TABLE IF NOT EXISTS habits (
id INT PRIMARY KEY AUTO_INCREMENT,
user_id INT NOT NULL,
name VARCHAR(255),
description TEXT,
frequency VARCHAR(50),
streak INT DEFAULT 0,
total_days INT DEFAULT 0,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY(user_id) REFERENCES users(id)
);
-- 8. 创建习惯记录表
CREATE TABLE IF NOT EXISTS habit_logs (
id INT PRIMARY KEY AUTO_INCREMENT,
habit_id INT,
date DATETIME,
FOREIGN KEY(habit_id) REFERENCES habits(id)
);
-- 9. 创建待办事项表
CREATE TABLE IF NOT EXISTS todos (
id INT PRIMARY KEY AUTO_INCREMENT,
user_id INT NOT NULL,
content TEXT,
status VARCHAR(50) DEFAULT 'pending',
due_date DATETIME,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY(user_id) REFERENCES users(id)
);
-- 10. 创建待办事项检查表
CREATE TABLE IF NOT EXISTS todo_checkins (
id INT PRIMARY KEY AUTO_INCREMENT,
todo_id INT,
checkin_date DATETIME,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY(todo_id) REFERENCES todos(id)
);
-- 11. 创建徽章表
CREATE TABLE IF NOT EXISTS badges (
id INT PRIMARY KEY AUTO_INCREMENT,
user_id INT NOT NULL,
name VARCHAR(255),
description TEXT,
icon VARCHAR(50),
unlocked INT DEFAULT 0,
condition_days INT,
FOREIGN KEY(user_id) REFERENCES users(id)
);
-- 12. 创建日记表
CREATE TABLE IF NOT EXISTS diaries (
id INT PRIMARY KEY AUTO_INCREMENT,
user_id INT NOT NULL,
title VARCHAR(255),
content TEXT,
weather VARCHAR(50),
mood VARCHAR(50),
date DATETIME,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
FOREIGN KEY(user_id) REFERENCES users(id)
);
-- 13. 启用外键约束
SET FOREIGN_KEY_CHECKS = 1;
-- 14. 插入默认收入分类
INSERT IGNORE INTO categories (name, type, icon, color, is_default, is_custom, sort_order) VALUES
('工资收入', 'income', '💰', '#10B981', 1, 0, 1),
('奖金福利', 'income', '🎁', '#10B981', 1, 0, 2),
('投资理财', 'income', '📈', '#10B981', 1, 0, 3),
('副业兼职', 'income', '💼', '#10B981', 1, 0, 4),
('经营收入', 'income', '🏪', '#10B981', 1, 0, 5),
('其他收入', 'income', '💵', '#10B981', 1, 0, 6),
('自定义输入', 'income', '✏️', '#6B7280', 1, 0, 999);
-- 15. 插入默认支出分类
INSERT IGNORE INTO categories (name, type, icon, color, is_default, is_custom, sort_order) VALUES
('餐饮美食', 'expense', '🍽️', '#EF4444', 1, 0, 1),
('超市购物', 'expense', '🛒', '#EF4444', 1, 0, 2),
('交通出行', 'expense', '🚗', '#EF4444', 1, 0, 3),
('休闲娱乐', 'expense', '🎮', '#EF4444', 1, 0, 4),
('房租房贷', 'expense', '🏠', '#EF4444', 1, 0, 5),
('水电物业', 'expense', '💡', '#EF4444', 1, 0, 6),
('医疗保健', 'expense', '🏥', '#EF4444', 1, 0, 7),
('教育学习', 'expense', '📚', '#EF4444', 1, 0, 8),
('人情往来', 'expense', '🎁', '#EF4444', 1, 0, 9),
('运动健身', 'expense', '🏃', '#EF4444', 1, 0, 10),
('美容护肤', 'expense', '💄', '#EF4444', 1, 0, 11),
('服饰鞋包', 'expense', '👔', '#EF4444', 1, 0, 12),
('通讯费用', 'expense', '📱', '#EF4444', 1, 0, 13),
('其他支出', 'expense', '📝', '#EF4444', 1, 0, 14),
('自定义输入', 'expense', '✏️', '#6B7280', 1, 0, 999);
-- 16. 创建普通用户并授予权限
-- 注意:请将 'your_password' 替换为实际的密码
CREATE USER IF NOT EXISTS 'goblog_user'@'localhost' IDENTIFIED BY 'your_password';
GRANT SELECT, INSERT, UPDATE, DELETE ON goblog.* TO 'goblog_user'@'localhost';
FLUSH PRIVILEGES;
-- 17. 完成信息
SELECT '数据库初始化完成!' AS message;
SELECT '请使用以下配置连接数据库:' AS message;
SELECT '用户: goblog_user' AS message;
SELECT '密码: your_password (请替换为实际密码)' AS message;
SELECT '数据库: goblog' AS message;