-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase_creation_script.sql
More file actions
70 lines (67 loc) · 2.12 KB
/
database_creation_script.sql
File metadata and controls
70 lines (67 loc) · 2.12 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
CREATE TABLE budgets(
id INT NOT NULL AUTO_INCREMENT,
fromDateTime DATETIME(6),
ToDateTime DATETIME(6),
amount DECIMAL(19,4),
PRIMARY KEY(id)
);
CREATE TABLE categorys (
id INT NOT NULL AUTO_INCREMENT,
name varchar(255),
PRIMARY KEY(id)
);
CREATE TABLE expenses (
id INT NOT NULL AUTO_INCREMENT,
categorys_id INT,
creationTimeStamp DATETIME(6), -- timeStamp is a reserved word.
amount DECIMAL(19,4),
comments VARCHAR(255), -- comment is taken, made it comments
-- im skipping adress
PRIMARY KEY (id),
FOREIGN KEY (categorys_id) REFERENCES categorys_id(id)
);
CREATE TABLE users_expenses (
id INT NOT NULL AUTO_INCREMENT,
expenses_id INT,
users_id INT,
PRIMARY KEY(id),
FOREIGN KEY(expenses_id) REFERENCES expenses(id) ON DELETE CASCADE,
FOREIGN KEY(users_id) REFERENCES users(id)
);
CREATE TABLE categorys_budgets (
id INT NOT NULL AUTO_INCREMENT,
budgets_id INT,
categorys_id INT,
PRIMARY KEY(id),
FOREIGN KEY(budgets_id) REFERENCES budgets(id) ON DELETE CASCADE,
FOREIGN KEY(categorys_id) REFERENCES categorys(id) ON DELETE CASCADE
);
CREATE TABLE users_categorys (
id INT NOT NULL AUTO_INCREMENT,
users_id INT,
categorys_id INT,
PRIMARY KEY(id),
FOREIGN KEY(categorys_id) REFERENCES categorys(id) ON DELETE CASCADE,
FOREIGN KEY(users_id) REFERENCES users(id)
);
CREATE TABLE settings (
id INT NOT NULL AUTO_INCREMENT,
users_id INT,
categoryCount INT,
mainColour varchar(10),
mainStyle varchar(10),
notificationPreference INT, -- Int/varchar?
-- more stuff?
PRIMARY KEY (id),
FOREIGN KEY (users_id) REFERENCES users(id) ON DELETE CASCADE
);
ALTER TABLE users
ADD COLUMN age INT,
-- ADD COLUMN gender VARCHAR(12),
ADD COLUMN gender ENUM('F','M','O') -- not exactly what's the best way to hold gender.
-- ADD COLUMN Roles INT(10) UNSIGNED NOT NULL
-- roles or something will be needed for the admin/user distinction
AFTER email
-- add the foreign key to expenses
ALTER TABLE expenses ADD user_id INT NOT NULL DEFAULT 1;
ALTER TABLE expenses ADD FOREIGN KEY (`user_id`) REFERENCES users(`id`);