MySQL Database Administration
MySQL Database Administration
ADMINISTRATION
With Top-Rated SQL Instructor John Pauler
Quizzes & Homework Exercises to test and reinforce key concepts, with step-by-step solutions
Bonus Projects to test your abilities and apply the skills developed throughout the course
2 Creating Schemas & Tables Create and delete schemas, tables, and columns using the
Workbench Editor, and by running SQL code
3
Insert data into tables, and update or delete specific records
Inserting, Updating & Deleting based on logical criteria you specify
[MID-COURSE PROJECT]
[FINAL PROJECT]
*Copyright Maven Analytics, LLC
INTRODUCING THE COURSE PROJECT
THE You have just been hired by a small database consulting shop. You will be working on projects
SITUATION for various clients to help them get their databases built and optimized for business
As a member of the consulting team, you will be asked to perform database services for
THE various clients.
BRIEF
Sometimes you will work with existing databases and tables to make small improvements, and
other times you will need to set up a database for clients from the ground up.
3 This course is for SQL Beginners and Data Analysts interested in DBA skills
• This course starts with the basics; if you’re already an expert DBA, this course will not be appropriate for you
• If you’ve already mastered the basics, feel free to skip ahead to the parts of the course that feel relevant to you
• SQL stands for Structured Query Language, and was designed in the early
1970s at IBM to manipulate and retrieve data stored in a relational
database management system (RDMS)
Mac, Windows, Linux Web App (works on any OS) Windows only Mac only
Step 1 Download Community Server This allows SQL to run on your machine
Step 5
We’ll run a quick SQL script to build some schemas and tables
Create the Databases we’ll be exploring throughout the course (this part is easy!)
2 Select the MacOS operating system, and download the DMG Archive version
• Note: you’ll likely see a later version than the one shown (just download the latest)
3 No need to Login or Sign Up, just click “No thanks, just start my download”
4 Find the install file in your downloads, then double click to run the installer package
5 Click through each install step, leaving defaults unless you need customized settings
• Note: Make sure to store your root password somewhere, you’ll need this later!
1 3 5
2 4
2 Select the Microsoft Windows operating system, and the Installer MSI download
• Note: On the download page you may see two versions: select mysql-installer-web-community if you are connected
to the internet, and keep in mind that you may see a later version than the one shown (just download the latest)
3 No need to Login or Sign Up, just click “No thanks, just start my download”
4 Find the install file in your downloads, then double click to run the installer package
5 Click through each install step, leaving defaults unless you need customized settings
• Note: Make sure to store your root password somewhere, you’ll need this later!
1 3 5
2 4
2 We’ll be using version 8.0.16 for this course, so you can either click “Looking for previous
GA versions?” to search for the same one, or simply download the latest available
3 No need to Login or Sign Up, just click “No thanks, just start my download”
4 Find the install file in your downloads, click the MySQL Workbench logo (with the dolphin)
and drag it into your Applications folder
5 Look for MySQL workbench in your list of applications, double click to launch, then proceed
to Step 3: Connecting to the server
1 3 5
2 We’ll be using version 8.0.13 for this course, so you can either click “Looking for previous
GA versions?” to search for the same one, or simply download the latest available
3 No need to Login or Sign Up, just click “No thanks, just start my download”
4 Find the install file in your downloads, double click to run the installation process, and
stick with default settings unless you need a custom configuration
5 Look for MySQL workbench in your list of programs, double click to launch, then proceed to
Step 3: Connecting to the server
• Note: You may see a warning if you aren’t on Windows 10+, but most older systems (i.e. Windows 7) should be compatible
1 3 5
1 After launching Workbench, check the MySQL Connections section on the welcome page
• If you see a connection already, right-click to Edit Connection, otherwise click the plus sign (+) to add a new one
2 Name the connection “mavenmovies”, confirm that the Username is “root”, and click OK
3 Once you see the mavenmovies connection on your welcome screen, simply click the tile
and enter your root password to complete the connection
Fun Fact!
Maven Movies is the name of the database I used when I made my first course. I always name my connections
‘mavenmovies’ as tribute. It does not matter what you name your connection. Name it anything you want!
1 3
2 4
Action Output
Schemas Tab
This is a summary of
Here you can view actions taken by the
tables and views server (TIP: the Response
in your schemas column is great for
troubleshooting!)
Tool Tabs
This is where the various
Management, Instance,
and Performance tools
show up after they are
selected in the
Administration Tab.
Administration Tab
Note: these show up as
Here you can select the
additional tabs alongside
Management, Instance,
tabs you have open for
and Performance tools
SQL query editing. They
co-exist in the same
section of Workbench,
even thought their
functions are different
1 In MySQL Workbench, click File from the top menu, then select Run SQL Script
1 3 5
2 4 6
Integer (-9223372036854775808 to
BIGINT 9223372036854775807)
DATE YYYY-MM-DD
DECIMAL Decimal (to 65 digits – most precise) ENUM One of a number of preset options
From: Amy Baker (owns the candy store) If you’ve done everything correctly, your table should go from this…
Subject: Help Updating A Database Table
Hi,
Thanks!
2. Drop the schema ‘candystore_old’. This is an old copy DROP a table you’ll need later in this course,
that is now out of date, so we don’t need to maintain it. and you’ll need to go back to the beginning
and reinstall. Just don’t do it. Be careful!!
Thanks!
-Amy
Hi,
Thank you!
-Amy
Tables contain information organized into columns (or fields) and rows (or records)
Cardinality refers to the uniqueness of values in a column (or attribute) of a table and is commonly used to describe
how two tables relate (one-to-one, one-to-many, or many-to-many). For now, here are the key points to grasp:
FOREIGN FOREIGN
(MANY) (MANY)
• Primary keys are unique
inventory_id film_id address_id
1 1 1 • They cannot repeat, so there is only one instance
2 1 1 PRIMARY of each primary key value in a column
3 1 1 (ONE)
4 1 1 film_id title release_year • Foreign keys are non-unique
5 1 2 1 ACADEMY DINOSAUR 2006
2 ACE GOLDFINGER 2006
• They can repeat, so there may be many instances
6 1 2
7 1 2 3 ADAPTATION HOLES 2006 of each foreign key value in a column
8 1 2 4 AFFAIR PREJUDICE 2006
9 2 2 • We can create a one-to-many relationship
10 2 2 PRIMARY by connecting a foreign key in one table to
11 2 2
12 3 2
(ONE)
a primary key in another
address_id address district
13 3 2 1 47 MySakila Drive Alberta
14 3 2 2 28 MySQL Boulevard QLD
15 3 2
16 4 1
17 4 1
18 4 1
19 4 1
20 4 2
Hey!
1) Tell me what the primary key is, and whether there are
any foreign keys
Thank you!
Hey!
Thank you!
As we designing our databases, it can be helpful to create EER diagrams to visually model the relationships between
tables and the constraints within those tables
Customer table:
PRO TIP:
When you explore a database for the first time, diagram
your relationships to understand your table structure
1 3 5
2 4
2 4
Hey again!
Thank you!
-Emily
THE A new client, the owner of the Maven Movies DVD rental business, has reached out to
SITUATION you for help restructuring their non-normalized database.
Take a look at the mavenmoviesmini schema. What do you notice about it? How many tables are there?
1 What does the data represent? What do you think the current schema?
~ 1:10
If you wanted to break out the data from the inventory_non_normalized table into multiple tables, how
2 many tables do you think would be ideal? What would you name those tables?
~ 4:30
Based on your answer from question #2, create a new schema with the tables you think will best serve this
3 data set. You can use SQL code or Workbench’s UI tools (whichever you feel more comfortable with).
~ 8:10
Next, use the data from the original schema to populate the tables in your newly optimized schema
4 (TIP: Revisit the video on database normalization again if you get stuck)
~ 17:45
Make sure your new tables have the proper primary keys defined and that applicable foreign keys are added.
5 Add any constraints you think should apply to the data as well (unique, non-NULL, etc.)
~ 24:50
Finally, after doing all of this technical work, write a brief summary of what you have done, in a way that your
6 non-technical client can understand. Communicate what you did, and why your new schema design is better.
~ 26:05
From MySQL.com:
The best way to improve the
performance of SELECT
operations is to create indexes
on one or more of the columns
that are tested in the query. The
index entries act like pointers to Using an Index
the table rows, allowing the
query to quickly determine
which rows match a condition in
the WHERE clause, and retrieve
the other column values for
those rows. All MySQL data
types can be indexed.
*Copyright Maven Analytics, LLC
CREATING AN INDEX MySQL Workbench in Action
Hi there…
Hi there…
CALL sp_staffOrdersServed;
Hi there…
Hi again…
I talked with another business owner who said the server can
sometimes get stuck and need to be restarted by a Database
Administrator. Can you check the status of the server and re-
start it for us?
-Joe
Hi there…
Hoping you could help us out with one more thing. I would like
to create two new Users who can access our database…
Thank you!!
-Joe
THE A new client, Bubs Glover, the owner of Bubs’ Bigtime Baby Booties, has reached out to
SITUATION you for help building his business a database from the ground up.
Bubs wants you to track information on his customers (first name, last name, email), his employees (first
1 name, last name, start date, position held), his products, and the purchases customers make (which customer,
~ 0:37 when it was purchased, for how much money). Think about how many tables you should create. Which data
goes in which tables? How should the tables relate to one another?
Given the database design you came up with, use Workbench to create an EER diagram of the database.
2 Include things like primary keys and foreign keys, and anything else you think you should have in the tables.
~ 4:50 Make sure to use reasonable data types for each column.
Create a schema called bubsbooties. Within that schema, create the tables that you have diagramed out. Make
3 sure they relate to each other, and that they have the same reasonable data types you selected previously.
~ 15:50
Add any constraints you think your tables’ columns should have. Think through which columns need to be
4 unique, which ones are allowed to have NULL values, etc.
~ 24:05
Insert at least 3 records of data into each table. The exact values do not matter, so feel free to make them
5 up. Just make sure that the data you insert makes sense, and that the tables tie together.
~ 28:40
Create two users and give them access to the database. The first user, TuckerReilly, will be the DBA, and should
6 get full database administrator privileges. The second user, EllaBrody, is an Analyst, and only needs read access.
~ 40:10