Advanced SQL DBA
Advanced SQL DBA
SQL DATABASE
ADMINISTRATION
With Expert SQL Instructor John Pauler
COURSE STRUCTURE
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
Insert large datasets to the database using the wizard. Review alter
2 File Import, Alter, Update & Keys and update, and map primary and foreign keys in our new schema
Learn about replication, and how and when we use it. Discuss
3 Replication, Backup & Recovery methods for backup and recovery of data to ensure nothing is lost
[MID-COURSE PROJECT]
Learn how Views & EER Diagrams are used as tools for reporting
5 Creating Views & EER Diagrams and understanding data in your database
[FINAL PROJECT]
*Copyright Maven Analytics, LLC
INTRODUCING THE COURSE PROJECT
THE You’ve just been hired as a Database Administrator for Maven Bear Builders, an online
SITUATION retailer which has just launched their first product.
As a member of the startup team, you will work with the CEO, the Head of Marketing, and the
THE Website Manager to help build their data infrastructure so they can grow their business.
BRIEF
You will create schemas and tables, load large datasets to the database, use automation to
keep things running smoothly, and tackle serious issues like security, backup, and data recovery.
3 This course is meant for people who already have basic SQL + DBA skills
• This course skips over the SQL basics, and jumps straight into a simulated real-world experience where students can
practice building and maintaining a real-world database
• Prior completion of MySQL Database Administration for Beginners and MySQL for Data Analysis is recommended
You should be familiar with the basic statements used in Data Definition
and Data Manipulation: CREATE, ALTER, INSERT, UPDATE, & DELETE
Integer (-9223372036854775808 to
BIGINT DATE YYYY-MM-DD
9223372036854775807)
DECIMAL Decimal (to 65 digits – most precise) ENUM One of a number of preset options
Returns ALL records from the LEFT table, and any FROM leftTableName
LEFT JOIN matching records from the RIGHT table LEFT JOIN rightTableName
Returns ALL records from the RIGHT table, and FROM leftTableName
RIGHT JOIN any matching records from the LEFT table RIGHT JOIN rightTableName
Step 1 Download Community Server This allows SQL to run on your machine
This is the program you’ll use to write and run SQL queries
Step 2 Download MySQL Workbench (it’s intuitive, and works across operating systems)
We’ll run the SQL code to build the database which we’ll be
Step 5 Create the Database 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
THIS IS IMPORTANT!
Before we get started, we will quickly configure a few settings in Workbench which will dramatically cut down on
problems you might otherwise face later in the course. Please do not skip this step.
1 First, we’ll adjust a date setting, so we won’t run into issues with our dates
2 Next, we’ll update the GROUP BY setting to only allow FULL GROUP BY
3 Then, we’ll adjust our MAX ALLOWED PACKET size, so any larger files we use will work
4 Last, we will shut down Workbench and restart, which will launch our new settings
2 Then, we’ll review using CREATE TABLE to add a table to the schema
3 Next, we’ll review the INSERT statement, used to add records to tables
4 Finally, we’ll review the DELETE statement, used to remove records from tables
THIS IS REVIEW!
We covered the basics of CREATE, INSERT, & DELETE in our MySQL Database Administration for Beginners course. If
you already have these concepts covered, feel free to skip ahead to the lecture on Importing Data From a File.
THIS IS REVIEW!
We covered the basics of creating schemas in our MySQL Database Administration for Beginners course.
If you already have this concept covered, feel free to skip this lecture.
THIS IS REVIEW!
We covered the basics of creating tables in our MySQL Database Administration for Beginners course. If you already
have this concept covered, feel free to skip this lecture.
THIS IS REVIEW!
We covered the basics of inserting records into tables in our MySQL Database Administration for Beginners course.
If you already have this concept covered, feel free to skip this lecture.
THIS IS REVIEW!
We covered the basics of deleting records into tables in our MySQL Database Administration for Beginners course.
If you already have this concept covered, feel free to skip this lecture.
Good morning,
-Sally
01.order_items_2012_Mar
02.order_items_2012_Apr
Good morning,
-Sally
01.order_items_2012_Mar
02.order_items_2012_Apr
Hey there,
Now that we have order_items built out, could you
also import this attached April refund data (weren’t any
in March) in a new table called order_item_refunds?
Thanks!
-Sally
03.order_item_refunds_2012_Apr
Hey there,
Now that we have order_items built out, could you
also import this attached April refund data (weren’t any
in March) in a new table called order_item_refunds?
Thanks!
-Sally
03.order_item_refunds_2012_Apr
Well…
-Sally
Well…
-Sally
Hey,
Thanks!
-Sally
04.order_items_2012_May-Dec
05.order_item_refunds_May-Dec
Hey,
Thanks!
-Sally
04.order_items_2012_May-Dec
05.order_item_refunds_May-Dec
1 First, we’ll review the ALTER TABLE statement used for adding and removing columns
2 Then, we’ll review using UPDATE to SET values of records already in the database
3 Next, we’ll review Cardinality and Primary Key to Foreign Key relationships
4 After our review is complete, we’ll use each of these concepts to update the database as
the business rolls out a new product and data tracking needs become more complex
THIS IS REVIEW!
We covered the basics of ALTER TABLE, UPDATE and Primary and Foreign Key relationships in our MySQL Database
Administration for Beginners course. If you already have these concepts covered, feel free to skip ahead.
THIS IS REVIEW!
We covered the basics of modifying tables to add or delete columns using ALTER TABLE in our MySQL Database
Administration for Beginners course. If you already have this concept covered, feel free to skip this lecture.
THIS IS REVIEW!
We covered the basics of updating records in our MySQL Database Administration for Beginners course.
If you already have this concept covered, feel free to skip this lecture.
THIS IS REVIEW!
We covered the basics of table relationships in our MySQL Database Administration for Beginners course.
If you already have this concept covered, feel free to skip this lecture.
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
4 AFFAIR PREJUDICE 2006
8 1 2
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 (ONE)
12 3 2 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,
Tomorrow we’re launching a new product called The
Forever Love Bear to complement The Original Mr. Fuzzy.
Thanks!
-Sally
Hey,
Tomorrow we’re launching a new product called The
Forever Love Bear to complement The Original Mr. Fuzzy.
Thanks!
-Sally
Good morning,
Good morning,
Hey there,
Hey there,
Hey there,
Thank you!
-Sally
Hey there,
Thank you!
-Sally
Hey there,
Now that you’ve done all the work to get our products
and order_items tables synced up, let’s import the
attached Q1 data into order_items and
order_item_refunds.
Thanks!
-Sally
06.order_items_2013_Jan-Mar
07.order_item_refunds_2013_Jan-Mar
Hey there,
Now that you’ve done all the work to get our products
and order_items tables synced up, let’s import the
attached Q1 data into order_items and
order_item_refunds.
Thanks!
-Sally
06.order_items_2013_Jan-Mar
07.order_item_refunds_2013_Jan-Mar
Replication enables us to store the same data on two or more servers by creating
copies, known as replicas
ROW-BASED STATEMENT-BASED
Changing ✓ PRO: statements which update very few rows X CON: statements which update very few rows will
Few Rows will execute very quickly usually perform more slowly than Row-Based
Changing X CON: statements which update a large ✓ PRO: statements updating a large number of
Many Rows number of rows will perform very slowly rows can still execute quickly
Consistent ✓ PRO: does not encounter issues with non- X CON: non-deterministic queries can spell trouble
Data deterministic queries like statement-based (example, INSERT w/ auto-incrementing PK)
Auditing X CON: harder to audit, because you only see ✓ PRO: easier to audit, because you see the
Changes changes, not the statements themselves statements themselves
Handling ✓ PRO: no problem handling stored routines X CON: can create problems with stored routines and
Triggers and triggers triggers.
We use Backups to make sure our data is protected and recoverable in the event of loss
• Physical backups store the raw data in a file, where Logical backups store the SQL statements
Physical vs needed to recreate the database and populate it
Logical Backups
• With Logical backups, (ex: MySQL Dump), you’ll be storing CREATE and INSERT statements
• Online backups occur while the server is running. The advantage you don’t have to take the server
Online vs down, so it won’t interfere with other clients using the server.
Offline Backups
• Offline backups happen when the server is stopped. Simpler, but other clients won’t have access either.
Local vs • Local backups happen on the same host that the MySQL server is running
Remote Backups • Remote backups are written somewhere else. This could be another host, a local machine, etc.
Good morning,
-Sally
Good morning,
-Sally
THE Maven Bear Builders has been up and running for a little over a year. You and your CEO
SITUATION have made some improvements to the database, but as the business continues to change,
she needs more help tweaking the structure and importing additional data sets.
As a Database Administrator, part of your job is executing on specific tasks like altering
tables. Another major focus area is staying on top of things like backup, recovery, and
database security. Use any opportunities you see as chance to flex your muscle as a
thought leader in these areas!
Import Q2 orders and refunds into the database using the files below:
1 08.order_items_2013_Apr-June
09.order_item_refunds_2013_Apr-Jun
~ 0:38
Update all previous records in the order_items table, setting is_primary_item = 1 for all records
3
• Up until now, all items sold were the primary item (since cross-selling is new)
~ 6:15 • Confirm this change has executed successfully
Add two new products to the products table, then import the remainder of 2013 orders and refunds,
4 using the product details and files shown below:
~ 9:00 10.order_items_2013_Jul-Dec
11.order_item_refunds_2013_Jul-Dec
Your CEO would like to make sure the database has a high degree of data integrity and avoid potential
5 issues as more people start using the database. If you see any opportunities to ensure data integrity
~ 13:38 by using constrains like NON-NULL, add them to the relevant columns in the tables you have created.
One of the company’s board advisors is pressuring your CEO on data risks and making sure she has a
6 great backup and recovery plan. Prepare a report on possible risks for data loss and steps the
~ 17:30 company can take to mitigate these concerns.
Thank you!
-Sally
Thank you!
-Sally
Hey there,
Thank you!
-Sally
Hey there,
Thank you!
-Sally
Hey,
Thank you!
-Sally
12.order_items_2014_Jan-Feb
13.order_item_refunds_2014_Jan-Feb
Hey,
Thank you!
-Sally
12.order_items_2014_Jan-Feb
13.order_item_refunds_2014_Jan-Feb
14.website_sessions_2014_Jan
15.website_sessions_2014_Feb
14.website_sessions_2014_Jan
15.website_sessions_2014_Feb
Thanks!
-Brent
Thanks!
-Brent
1 3 5
2 4
2 4
Good morning,
Thanks!
-Sally
Good morning,
Thanks!
-Sally
Hey there!
Hey there!
SOCIAL
PAGE A PAGE B
SEARCH
DIRECT
PAGE A PAGE B
Good morning!
Given your expertise and your knowledge of the
website data we are already tracking in our SQL
database, is there any additional data you recommend
we add?
Thanks!
-Molly
Good morning!
Given your expertise and your knowledge of the
website data we are already tracking in our SQL
database, is there any additional data you recommend
we add?
Thanks!
-Molly
Hey!
I was able to get this February pageview data out of our
web analytics tool. Would you be able to help me load
it into the database so we can tie it to all of your other
great data?
Thanks!
-Molly
16.website_pageviews_2014_Feb
Hey!
I was able to get this February pageview data out of our
web analytics tool. Would you be able to help me load
it into the database so we can tie it to all of your other
great data?
Thanks!
-Molly
16.website_pageviews_2014_Feb
Authentication refers to the processes you employ for users to prove to your
system that they are who they say they are.
Attackers can gain access to your system by tricking your system into thinking
they are one of your trusted users.
With Denial of Service (DoS) attacks, the hackers will attempt to overwhelm your
system with requests, which will render it useless for your legitimate users.
There are services you can employ that will filter out suspicious activity, and when a
DoS attack is detected, there are a number of mechanisms for blocking the attack.
In one special case -- a Distributed Denial of Service (DDoS) -- attackers will coordinate
the attack from multiple machines simultaneously, often distributed globally. This
makes it harder to determine the attacking source and shut them off quickly.
THIS IS IMPORTANT!
A DoS or DDoS can sometimes be used to distract from and cover up additional attacks on your systems. In some
cases, the intent is to extract data elsewhere. When you encounter a DoS, be on high alert across all systems.
Once they have gained access to some of your systems, it can become easier for
them to identify additional security holes and gain higher levels of access.
• Form-based authentication
user_id = 666
With SQL injection attacks, hackers will attempt to make your application do
something it’s not supposed to do by editing SQL statements utilized on the back-end.
A buffer overflow is when more data is put into the buffer than it can handle.
The causes the data to overflow and get written elsewhere.
THIS IS IMPORTANT!
Preparing for and defending against ransomware attacks needs to be a team effort. The Database Administrator
and CTO should prepare by having adequate and safe backups, and employees should be trained to avoid phishing.
Make every attempt to store as little personally identifiable information (PII) as possible
• If you don’t absolutely need it for your application, consider NOT storing it
For sensitive data that you need to store (credit cards, social security #, email address,
phone number, passwords, etc.), make sure to encrypt it whenever possible
Make a list of the applications and people in your organization that have access to
sensitive data, and where you are vulnerable. Is the list longer than it should be?
Make sure to practice “minimum viable access” in granting your permission levels
• Very few employees will need full access to systems and data. Grant only what they need.
When thinking about permission levels, consider whether employees need edit rights or
if read-only access is sufficient for their job function
Make sure that you have a plan for quickly removing access to systems when employees
leave, or when you need to limit access on a case-by-case basis
Require strong passwords for employees, and don’t allow them create weak passwords
• Minimum 8 characters, containing a number, a special character, and both upper- and lower-case letters
Whether it is a whole team, a single person, or part of someone’s time (in very small
companies), someone in your organization should be responsible for security planning
Give the responsible employee the authority to work with the rest of your organization
to ensure that things are set up properly
Writing down your security policy forces you to create a cohesive plan and determine
specific action items that you will be held accountable for completing
Make sure you are continually reviewing and revising your security policy as your business
and your risks evolve over time
Back up your data, especially the most important information; in the event of a breach,
system malfunction, or malware, you will be happy to have another copy
Create logs on admin systems and database activity so you can monitor activity
Conduct periodic reviews and invite friendly third parties to try hacking your system to
expose potential vulnerabilities
Hey there!
-Sally
Hey there!
-Sally
THE There have been some exciting developments for Maven Bear Builders. The company is going
SITUATION to start offering chat support on the website, and needs your help planning. The company has
also been approached by potential acquirers, and you’ll be asked to help with due diligence.
NEW MESSAGE
May 1, 2014
THE
From: Sally Bleu (CEO) YOUR OBJECTIVES:
LETTER
Subject: Exciting Developments!
• Update the database with the most recent data
1. We’re adding chat support to the website, and need your • Help Sally with some support for asks related to
help planning for the data structure. the potential acquisition
Let’s do this!
-Sally
Import the latest order_items and order_item_refunds data below into the database, and verify the
1 order summary trigger you created previously still works (if not, recreate it)
~ 0:37 17.order_items_2014_Mar
18.order_items_2014_Apr
19.order_item_refunds_2014_Mar
20.order_item_refunds_2014_Apr
2 Import the website_sessions and website_pageviews data for March and April, provided below:
21.website_sessions_2014_Mar
~ 5:30 22.website_sessions_2014_Apr
23.website_pageviews_2014_Mar
24.website_pageviews_2014_Apr
3 The company is adding chat support to the website. You’ll need to design a database plan to track
which customers and sessions utilize chat, and which chat representatives serve each customer
~ 9:14
4 Based on your tracking plan for chat support, create an EER diagram that incorporates your new tables
into the existing database schema (including table relationships)
~ 14:00
5 Create the tables from your chat support tracking plan in the database, and include relationships to
existing tables where applicable
~ 20:40
6 Using the new tables, create a stored procedure to allow the CEO to pull a count of chats handled by
chat representative for a given time period, with a simple CALL statement which includes two dates
~ 26:37
7 Create two Views for the potential acquiring company; one detailing monthly order volume and revenue,
the other showing monthly website traffic. Then create a new User, with access restricted to these Views
~ 31:18
8 The potential acquirer is commissioning a third-party security study, and your CEO wants to get in front
of it. Provide her with a list of your top data security threats and recommendations for mitigating risk
~ 37:00