Data 101: MySQL Tutorial using a Diabetes Database

Introduction

This tutorial demonstrates how to use MySQL and MySQL Workbench to create and explore a MYSQL database containing diabetes treatment records.

Original Dataset

I generated the SQL for this tutorial using the Diabetes Data Set from the UCI Machine Learning Repository. I added a "patients" table using random celebrity names.

MySQL Setup

Connecting to the Database

Open MySQL Workbench. Click Database -> Connect to Database

Connect to a MySQL Database Menue
Connect to a MySQL Database via Menu

The local MySQL server should be running on Hostname: 127.0.0.1 and Port 3306. Your hostname or IP address may be different if you are connecting to another host running MySQL. Click on “Password” to enter the password generated during installation.

Enter IP and Password for MySQL database.
Enter IP and Password for MySQL database.

Save the password here:

Save your password (MacOS)
Save your password (MacOS)

Now that you connected to the server, list the installed databases using.

SHOW SCHEMAS

You should see the following output similar to this:

MySQL Workbench Output of Show Schema
MySQL Workbench Output of Show Schema

Creating the Diabetes Database

In MySQL workbench, execute “create_diabetes_db.sql” using “File -> Open SQL Script”.

"Open SQL Script" Menu
"Open SQL Script" Menu

You can find this file in my github within the medical_databases/sql/ directory.

Open "create_diabetes_db.sql"
Open "create_diabetes_db.sql"

Once the file is open, use Command-A to “Select ALL” - or (Edit -> Select ALL).

"Select All" SQL in window
"Select All" SQL in window

Now Click the Execute (leftmost lightning bolt) button. The “Action Output” window should show:

Output from "create db" script
Output from "create db" script

Loading the Diabetes Data into the Database

Now open “diabetes_data.sql”. Click here to download that file from github.

Open "diabetes_data.sql"
Open "diabetes_data.sql"

Once the file is open, use Command-A to “Select ALL” - or (Edit -> Select ALL). This is a big file so you’ll only see the beginning part in the window.

"Select All" Diabetes data SQL
"Select All" Diabetes data SQL

Now Click the Execute button (leftmost lightning bolt). The “Action Output” window should show something similar to the output below. There are lot of commands in this file but you should see some “CREATE TABLE” and “INSERTS” statements.

Output from executing diabetes data SQL
Output from executing diabetes data SQL

Now create a new “Query Tab”. (File -> New Query Tab) or COMMAND-T then try the query:

SELECT * FROM patients;

The result grid pane should show something similar to the following.

"SELECT * FROM patients" Output
"SELECT * FROM patients" Output