Stock Database Project Report v1
Stock Database Project Report v1
Akanksha
Report Version 1
1
Summary
This version 1 report explains how to setup and administer the MySQL database for storing
Setting up the tables for storing daily price data in DEV and PROD
Updating the tables in DEV and PROD with daily price data for the stocks
Contents
Summary ................................................................................................................................................. 1
Entity-Relationship-Diagram ................................................................................................................... 5
Appendix ................................................................................................................................................. 8
3
We will create 2 MySQL server instances on our local machine for PROD and DEV each. This
will ensure restricted access to PROD environment and safeguard against errors, and ensure
DEV scripts are not run in its schema accidentally. For the first server instance download
MySQL from your browser and follow the installation steps as directed. For the second
Copy
C:\ProgramData\MySQL\MySQL Server 8.0\my.ini
1.
to
C:\ProgramData\MySQL\MySQL Server 8.0\my1.ini
Open my1.ini and modify:
Port = 3307(under Client and Server Section)
2.
Datadir = C:/ProgramData/MySQL/MySQL Server 8.0/Data1
report_port = 3307
Copy
C:\ProgramData\MySQL\MySQL Server 8.0\Data
3.
to
C:\ProgramData\MySQL\MySQL Server 8.0\Data1
Run on cmd prompt: (With Administrator privileges if necessary)
6. Type services.msc, find the service name MySQL80-1, right-click on it and click Start.
If all went well, you will see the Status change to Running.
Now that we have two MySQL server instances [(DEV) MySQL80 | (PROD) MySQL80-1], we
will create the respective databases on the servers through MySQL Workbench.
* Stop the MySQL80 DEV service from Services.msc on your Windows machine (preferably)
when creating PROD databases. This will reduce load on your hardware and also protect PROD
data completely.
1. Right click in the MySQL Workbench Navigator and click Create Schema.
Make sure you are logged in as root (Users and Privileges) in both databases. This is
3.
important for the future steps of the setup.
In the schema list on both workbenches you will see DEV and PROD schemas respectively
4.
if everything runs correctly.
5
Entity-Relationship-Diagram
For the DEV and PROD databases we have created a Strong Entity table called Ticker_List
which will be a directory of all the stocks we will be collecting data on. The id from this table
will form a Foreign Key reference to the other Strong Entity table Daily_Ticker_Price table
column ticker_id, so we don’t have to insert the name of the stock repetitively. The
from Ticker_List to Daily_Ticker_Price is ‘One is to Many’. The day the stock data was created
will be stored under created_date column for all three tables, with a Foreign Key reference
from the Ticker_List table. The Ticker_Price_History table will also have a Foreign Key
reference for the ticker_id column. The last_updated column, which stores the date and time
of the last price change, will be stored in Ticker_Price_History, with a Foreign Key reference
*[The Daily_Ticker_Price table will be partitioned by id and ticker_id to be able to query the
table in slices, which will be faster, given the millions of rows that will be stored in this table.
So individual stock data can be queried by just selecting the partition p0, p1, p2…..pn.]
6
Run the CreateTables_DEV.Py script to create the aforementioned tables (in ERD) in
DEV
* Stop the MySQL80 DEV service from Services.msc on your Windows machine (preferably)
when running scripts in PROD. This will reduce load on your hardware and also protect PROD
data completely.
Run the Multiprocessing_CSV_DEV.Py script to insert the CSV files’ data into DEV
tables
Run the Multiprocessing_CSV_PROD.Py script to insert the CSV files’ data into PROD
tables
* Stop the MySQL80 DEV service from Services.msc on your Windows machine (preferably)
when running scripts in PROD. This will reduce load on your hardware and also protect PROD
data completely.
7
Run the Reports_PROD.Py script to generate a report of the data stored in the PROD
tables
The report will be directly emailed to the GMAIL account of the user (Python’s package
* Stop the MySQL80 DEV service from Services.msc on your Windows machine (preferably)
when running scripts in PROD. This will reduce load on your hardware and also protect PROD
data completely.
8
Appendix
1. Partitioning Tables
2. CreateTables_*.py
This script will create the tables with the primary key, foreign key, indexes and partitioning references
such as above. The id and stock_id will be the primary keys and indexes in the daily_ticker_price table.
3. Multiprocessing_CSV_*.py
This script will insert all the CSV files’ data into the tables. They will drop the INDEXES at the beginning
of the script before beginning the inserts and add them back at the end of the script, before closing
connection to the database (Indexes slow down inserts and updates but fasten select queries).
All such statements will be run within TRIGGERS or BEGIN…END blocks such as below to prevent bad
queries from corrupting the tables. The statements will only be committed at the end of the block;