-
Notifications
You must be signed in to change notification settings - Fork 0
/
createDB.sql
123 lines (95 loc) · 3.71 KB
/
createDB.sql
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
CREATE TABLE `Users` (
`id` INT NOT NULL AUTO_INCREMENT,
`username` VARCHAR(255) NOT NULL UNIQUE,
`password` VARCHAR(255) NOT NULL,
PRIMARY KEY (`id`)
);
CREATE TABLE `Accounts` (
`id` INT NOT NULL AUTO_INCREMENT,
`name` VARCHAR(255) NOT NULL,
`opened` DATETIME NOT NULL,
`closed` DATETIME,
PRIMARY KEY (`id`)
);
CREATE TABLE `User_Account` (
`user_id` INT NOT NULL,
`acc_id` INT NOT NULL,
PRIMARY KEY (`user_id`,`acc_id`)
);
CREATE TABLE `Currency` (
`id` INT NOT NULL AUTO_INCREMENT,
`name` VARCHAR(255) NOT NULL UNIQUE,
PRIMARY KEY (`id`)
);
CREATE TABLE `Category` (
`id` INT NOT NULL AUTO_INCREMENT,
`name` VARCHAR(255) NOT NULL,
PRIMARY KEY (`id`)
);
CREATE TABLE `Account_Currency` (
`acc_id` INT NOT NULL,
`curr_id` INT NOT NULL,
`id` INT NOT NULL AUTO_INCREMENT UNIQUE,
`init_value` FLOAT NOT NULL DEFAULT '0',
PRIMARY KEY (`acc_id`,`curr_id`)
);
CREATE TABLE `Transactions` (
`id` INT NOT NULL AUTO_INCREMENT,
`acc_curr_id_from` INT,
`acc_curr_id_to` INT,
`cat_id` INT,
`value` FLOAT NOT NULL,
`exchange_rate` FLOAT,
`tr_date` DATETIME NOT NULL,
PRIMARY KEY (`id`)
);
CREATE TABLE `Tags` (
`id` INT NOT NULL AUTO_INCREMENT,
`name` VARCHAR(255) NOT NULL,
`user_id` INT NOT NULL,
PRIMARY KEY (`id`)
);
CREATE TABLE `Accumulations` (
`id` INT NOT NULL AUTO_INCREMENT,
`name` VARCHAR(255) NOT NULL,
`value` FLOAT NOT NULL,
`cur_id` INT NOT NULL,
PRIMARY KEY (`id`)
);
CREATE TABLE `Transaction_tag` (
`trans_id` INT NOT NULL,
`tag_id` INT NOT NULL
);
CREATE TABLE `Wishlist` (
`id` INT NOT NULL AUTO_INCREMENT,
`user_id` INT NOT NULL,
`name` VARCHAR(255) NOT NULL,
`value` FLOAT NOT NULL,
`curr_id` INT NOT NULL,
PRIMARY KEY (`id`)
);
CREATE TABLE `Templates` (
`id` INT NOT NULL AUTO_INCREMENT,
`user_id` INT NOT NULL,
`currency_id` INT NOT NULL,
`value` INT NOT NULL,
`name` VARCHAR(255) NOT NULL UNIQUE,
PRIMARY KEY (`id`)
);
ALTER TABLE `User_Account` ADD CONSTRAINT `User_Account_fk0` FOREIGN KEY (`user_id`) REFERENCES `Users`(`id`);
ALTER TABLE `User_Account` ADD CONSTRAINT `User_Account_fk1` FOREIGN KEY (`acc_id`) REFERENCES `Accounts`(`id`);
ALTER TABLE `Account_Currency` ADD CONSTRAINT `Account_Currency_fk0` FOREIGN KEY (`acc_id`) REFERENCES `Accounts`(`id`);
ALTER TABLE `Account_Currency` ADD CONSTRAINT `Account_Currency_fk1` FOREIGN KEY (`curr_id`) REFERENCES `Currency`(`id`);
ALTER TABLE `Transactions` ADD CONSTRAINT `Transactions_fk0` FOREIGN KEY (`acc_curr_id_from`) REFERENCES `Account_Currency`(`id`);
ALTER TABLE `Transactions` ADD CONSTRAINT `Transactions_fk1` FOREIGN KEY (`acc_curr_id_to`) REFERENCES `Account_Currency`(`id`);
ALTER TABLE `Transactions` ADD CONSTRAINT `Transactions_fk2` FOREIGN KEY (`cat_id`) REFERENCES `Category`(`id`);
ALTER TABLE `Tags` ADD CONSTRAINT `Tags_fk0` FOREIGN KEY (`user_id`) REFERENCES `Users`(`id`);
ALTER TABLE `Accumulations` ADD CONSTRAINT `Accumulations_fk0` FOREIGN KEY (`cur_id`) REFERENCES `Currency`(`id`);
ALTER TABLE `Transaction_tag` ADD CONSTRAINT `Transaction_tag_fk0` FOREIGN KEY (`trans_id`) REFERENCES `Transactions`(`id`);
ALTER TABLE `Transaction_tag` ADD CONSTRAINT `Transaction_tag_fk1` FOREIGN KEY (`tag_id`) REFERENCES `Tags`(`id`);
ALTER TABLE `Wishlist` ADD CONSTRAINT `Wishlist_fk0` FOREIGN KEY (`user_id`) REFERENCES `Users`(`id`);
ALTER TABLE `Wishlist` ADD CONSTRAINT `Wishlist_fk1` FOREIGN KEY (`curr_id`) REFERENCES `Currency`(`id`);
ALTER TABLE `Templates` ADD CONSTRAINT `Templates_fk0` FOREIGN KEY (`user_id`) REFERENCES `Users`(`id`);
ALTER TABLE `Templates` ADD CONSTRAINT `Templates_fk1` FOREIGN KEY (`currency_id`) REFERENCES `Currency`(`id`);
ALTER TABLE `Templates` ADD CONSTRAINT `Templates_fk2` FOREIGN KEY (`value`) REFERENCES `Currency`(`id`);
INSERT INTO Currency (name) VALUES ('RUB'), ('USD'), ('EUR');