-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmy_sql_table_create.py
More file actions
194 lines (164 loc) · 6.8 KB
/
Copy pathmy_sql_table_create.py
File metadata and controls
194 lines (164 loc) · 6.8 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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed Apr 25 23:05:09 2018
@author: SebastianArr
"""
import mysql.connector as mdb
import getpass
# Connect to the MySQL instance
db_host = 'localhost'
db_user = 'root'
db_name = 'securities_master'
db_pass = getpass.getpass('Enter database password: ')
con = mdb.connect(host=db_host, user=db_user, passwd=db_pass, db=db_name)
# create dictionary with needed tables
TABLES = {}
# define tables for equities
TABLES['equities_data_vendors'] = (
"CREATE TABLE `equities_data_vendors` ("
"`id` int NOT NULL AUTO_INCREMENT,"
"`name` varchar(64) NOT NULL,"
"`website_url` varchar(255) NULL,"
"`support_email` varchar(255) NULL,"
"`created_date` datetime NOT NULL,"
"PRIMARY KEY (`id`)"
") ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;")
TABLES['equities_exchanges'] = (
"CREATE TABLE `equities_exchanges` ("
"`id` int NOT NULL AUTO_INCREMENT,"
"`abbreviation` varchar(32) NOT NULL,"
"`name` varchar(255) NOT NULL,"
"`city` varchar(255) NULL,"
"`country` varchar(255) NULL,"
"`currency` varchar(64) NULL,"
"`timezone_offset` time NULL,"
"`created_date` datetime NOT NULL,"
"PRIMARY KEY (`id`)"
") ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;")
TABLES['equities_symbols'] = (
"CREATE TABLE `equities_symbols` ("
"`id` int NOT NULL AUTO_INCREMENT,"
"`ticker` varchar(32) NOT NULL,"
"`instrument` varchar(64) NOT NULL,"
"`name` varchar(255) NULL,"
"`sector` varchar(255) NULL,"
"`currency` varchar(32) NULL,"
"`created_date` datetime NOT NULL,"
"PRIMARY KEY (`id`)"
") ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;")
TABLES['equities_combinations'] = (
"CREATE TABLE `equities_combinations` ("
"`id` int NOT NULL AUTO_INCREMENT,"
"`equities_data_vendors_id` int NOT NULL,"
"`equities_exchanges_id` int NOT NULL,"
"`equities_symbols_id` int NOT NULL,"
"`created_date` datetime NOT NULL,"
"PRIMARY KEY (`id`),"
"FOREIGN KEY (`equities_data_vendors_id`) REFERENCES equities_data_vendors (`id`) ON DELETE CASCADE ON UPDATE CASCADE,"
"FOREIGN KEY (`equities_exchanges_id`) REFERENCES equities_exchanges (`id`) ON DELETE CASCADE ON UPDATE CASCADE,"
"FOREIGN KEY (`equities_symbols_id`) REFERENCES equities_symbols (`id`) ON DELETE CASCADE ON UPDATE CASCADE"
") ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;")
TABLES['equities_daily_prices'] = (
"CREATE TABLE `equities_daily_prices` ("
"`id` int NOT NULL AUTO_INCREMENT,"
"`equities_combinations_id` int NOT NULL,"
"`price_date` datetime NOT NULL,"
"`created_date` datetime NOT NULL,"
"`open_price` decimal(19,4) NULL,"
"`high_price` decimal(19,4) NULL,"
"`low_price` decimal(19,4) NULL,"
"`close_price` decimal(19,4) NULL,"
"`close_price_adjusted` decimal(19,4) NULL,"
"`volume` bigint NULL,"
"PRIMARY KEY (`id`),"
"FOREIGN KEY (`equities_combinations_id`) REFERENCES equities_combinations (`id`) ON DELETE CASCADE ON UPDATE CASCADE"
") ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;")
# define tables for cryptocurrencies
TABLES['cryptocurrencies_data_vendors'] = (
"CREATE TABLE `cryptocurrencies_data_vendors` ("
"`id` int NOT NULL AUTO_INCREMENT,"
"`name` varchar(64) NOT NULL,"
"`website_url` varchar(255) NULL,"
"`support_email` varchar(255) NULL,"
"`created_date` datetime NOT NULL,"
"PRIMARY KEY (`id`)"
") ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;")
TABLES['cryptocurrencies_exchanges'] = (
"CREATE TABLE `cryptocurrencies_exchanges` ("
"`id` int NOT NULL AUTO_INCREMENT,"
"`abbreviation` varchar(32) NOT NULL,"
"`name` varchar(255) NOT NULL,"
"`city` varchar(255) NULL,"
"`country` varchar(255) NULL,"
"`currency` varchar(64) NULL,"
"`timezone_offset` time NULL,"
"`created_date` datetime NOT NULL,"
"PRIMARY KEY (`id`)"
") ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;")
TABLES['cryptocurrencies_symbols'] = (
"CREATE TABLE `cryptocurrencies_symbols` ("
"`id` int NOT NULL AUTO_INCREMENT,"
"`ticker` varchar(32) NOT NULL,"
"`instrument` varchar(64) NOT NULL,"
"`name` varchar(255) NULL,"
"`proof_type` varchar(255) NULL,"
"`total_coin_supply` bigint NULL,"
"`created_date` datetime NOT NULL,"
"PRIMARY KEY (`id`)"
") ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;")
TABLES['cryptocurrencies_symbols_reference_currencies'] = (
"CREATE TABLE `cryptocurrencies_symbols_reference_currencies` ("
"`id` int NOT NULL AUTO_INCREMENT,"
"`ticker` varchar(32) NOT NULL,"
"`instrument` varchar(64) NOT NULL,"
"`name` varchar(255) NULL,"
"`proof_type` varchar(255) NULL,"
"`total_coin_supply` bigint NULL,"
"`created_date` datetime NOT NULL,"
"PRIMARY KEY (`id`)"
") ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;")
TABLES['cryptocurrencies_combinations'] = (
"CREATE TABLE `cryptocurrencies_combinations` ("
"`id` int NOT NULL AUTO_INCREMENT,"
"`cryptocurrencies_data_vendors_id` int NOT NULL,"
"`cryptocurrencies_exchanges_id` int NOT NULL,"
"`cryptocurrencies_symbols_id` int NOT NULL,"
"`cryptocurrencies_symbols_reference_currencies_id` int NOT NULL,"
"`created_date` datetime NOT NULL,"
"PRIMARY KEY (`id`),"
"FOREIGN KEY (`cryptocurrencies_data_vendors_id`) REFERENCES cryptocurrencies_data_vendors (`id`) ON DELETE CASCADE ON UPDATE CASCADE,"
"FOREIGN KEY (`cryptocurrencies_exchanges_id`) REFERENCES cryptocurrencies_exchanges (`id`) ON DELETE CASCADE ON UPDATE CASCADE,"
"FOREIGN KEY (`cryptocurrencies_symbols_id`) REFERENCES cryptocurrencies_symbols (`id`) ON DELETE CASCADE ON UPDATE CASCADE,"
"FOREIGN KEY (`cryptocurrencies_symbols_reference_currencies_id`) REFERENCES cryptocurrencies_symbols_reference_currencies (`id`) ON DELETE CASCADE ON UPDATE CASCADE"
") ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;")
TABLES['cryptocurrencies_daily_prices'] = (
"CREATE TABLE `cryptocurrencies_daily_prices` ("
"`id` int NOT NULL AUTO_INCREMENT,"
"`cryptocurrencies_combinations_id` int NOT NULL,"
"`price_date` datetime NOT NULL,"
"`created_date` datetime NOT NULL,"
"`open_price` decimal(19,4) NULL,"
"`high_price` decimal(19,4) NULL,"
"`low_price` decimal(19,4) NULL,"
"`close_price` decimal(19,4) NULL,"
"`volume_from` decimal(25,2) NULL,"
"`volume_to` decimal(25,2) NULL,"
"PRIMARY KEY (`id`),"
"FOREIGN KEY (`cryptocurrencies_combinations_id`) REFERENCES cryptocurrencies_combinations (`id`) ON DELETE CASCADE ON UPDATE CASCADE"
") ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;")
# create required tables
for name, ddl in TABLES.items():
try:
print("Creating table: ", name)
cur = con.cursor()
cur.execute(ddl)
cur.close()
except mdb.Error as err:
if err.errno == mdb.errorcode.ER_TABLE_EXISTS_ERROR:
print("Warning: Table already exists. It is not overwritten.")
else:
print(err.msg)
else:
print("OK")
con.close()