0% found this document useful (0 votes)
493 views141 pages

Advanced SQL DBA

This is a project-based course, for students looking for a practical, hands-on, and highly engaging approach to building & maintaining databases with MySQL.

Uploaded by

Satyawan Rich
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
493 views141 pages

Advanced SQL DBA

This is a project-based course, for students looking for a practical, hands-on, and highly engaging approach to building & maintaining databases with MySQL.

Uploaded by

Satyawan Rich
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 141

ADVANCED

SQL DATABASE
ADMINISTRATION
With Expert SQL Instructor John Pauler
COURSE STRUCTURE

This is a project-based course, for students looking for a practical, hands-on,


and highly engaging approach to building & maintaining databases with MySQL

Additional resources include:

Downloadable Ebook to serve as a helpful reference when you’re offline or on the go

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

*Copyright Maven Analytics, LLC


COURSE OUTLINE
Discuss the basic prerequisites, download Community Server and
1 Introduction & Setup Workbench, and review the Workbench Interface

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]

Use automation like triggers, stored procedures and scheduled


4 Automating Database Activity events, and learn how website data is written to a DB in real-time

Learn how Views & EER Diagrams are used as tools for reporting
5 Creating Views & EER Diagrams and understanding data in your database

Discuss best practices in database security, and learn what can


6 Database Security happen when security isn’t taken seriously

[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.

THE Use SQL to:


OBJECTIVE • Build and maintain the Maven Bear Builders database
• Become the data expert for the company, and the go-to person for mission critical data enhancements
• Leverage automation, and make decisions about security and backup to plan for long-term success

*Copyright Maven Analytics, LLC


SETTING EXPECTATIONS

1 You’ll learn MySQL Database Administration using MySQL Workbench


• In your career, you may use other “flavors” of SQL (T-SQL, PL/SQL, PostgreSQL, etc.)
• Each flavor is very similar. The principles you learn here will apply universally, but syntax will vary slightly

2 We will focus on the creation and maintenance of databases


• We will design and create schemas and tables from scratch, and alter existing tables to improve them

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

4 We will NOT go deep on data analysis or into engineering


• This course will focus on Database Administration concepts, like creating and maintaining databases
• We offer separate courses that focus on using SQL for data analysis and business intelligence (beginner + advanced-level)
• We will not be covering languages like PHP, Python, or Ruby

*Copyright Maven Analytics, LLC


PREREQUISITE SKILLS REVIEW

*Copyright Maven Analytics, LLC


PREREQUISITE DBA SKILLS

You should be familiar with the basic statements used in Data Definition
and Data Manipulation: CREATE, ALTER, INSERT, UPDATE, & DELETE

You should have some understanding of automation using database


tools like TRIGGERS, and STORED PROCEDURES

You should be able to create EER DIAGRAMS, and should be familiar


with Database Design concepts like NORMALIZATION, & CARDINALITY

*Copyright Maven Analytics, LLC


COMMON MySQL DATA TYPES
Data Type Specifications Data Type Specifications

TINYINT Integer (-128 to 127) CHAR String (0 – 255)

SMALLINT Integer (-32768 to 32767) VARCHAR String (0 – 255)

MEDIUMINT Integer (-8388608 to 8388607) TINYTEXT String (0 – 255)

INT Integer (-2147483648 to 2147483647) TEXT String (0 – 65535)

Integer (-9223372036854775808 to
BIGINT DATE YYYY-MM-DD
9223372036854775807)

FLOAT Decimal (precise to 23 digits) DATETIME YYYY-MM-DD HH:MM:SS

DOUBLE Decimal (23 to 53 digits) TIMESTAMP YYYYMMDDHHMMSS

DECIMAL Decimal (to 65 digits – most precise) ENUM One of a number of preset options

*Copyright Maven Analytics, LLC


THE “BIG 6” ELEMENTS OF A SQL SELECT STATEMENT
START OF
STATEMENT

Identifies the column(s) you want your


SELECT query to select for your results
SELECT columnName

Identifies the table(s) your query will pull


FROM data from
FROM tableName

(Optional) Specifies record-filtering criteria


WHERE for filtering your results
WHERE logicalCondition

(Optional) Specifies how to group the data


GROUP BY in your results
GROUP BY columnName

(Optional) Specifies group-filtering criteria


HAVING for filtering your results
HAVING logicalCondition

(Optional) Specifies the order in which your


ORDER BY query results are displayed
ORDER BY columnName
END OF
STATEMENT

*Copyright Maven Analytics, LLC


COMMON JOIN TYPES

Returns records that exist in BOTH tables, and FROM leftTableName


INNER JOIN excludes unmatched records from either table INNER JOIN rightTableName

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

Returns ALL records from BOTH tables, including FROM leftTableName


FULL OUTER JOIN non-matching records FULL JOIN rightTableName

Returns all data from one table, with all data


SELECT FROM firstTableName
UNION from another table appended to the end UNION
SELECT FROM secondTableName
*Copyright Maven Analytics, LLC
DOWNLOAD & SETUP

*Copyright Maven Analytics, LLC


MySQL DOWNLOAD & SETUP – OVERVIEW

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 get you connected to the server so you can use


Step 3 Connect Workbench to Server Workbench to start running your own SQL queries

We’ll take a quick tour of the Workbench interface to get


Step 4 Review Workbench Interface you familiar with the layout and key components

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!)

HEY THIS IS IMPORTANT!


If you took one of my other courses, and have already installed Community Server and MySQL Workbench,
then you can skip ahead to creating the database. No need to re-install. Whatever version you have is great.

*Copyright Maven Analytics, LLC


STEP 1: COMMUNITY SERVER (MAC)
HEY THIS IS IMPORTANT!
If you took one of my other courses, and have already installed Community Server and MySQL Workbench,
then you can skip ahead to creating the database. No need to re-install. Whatever version you have is great.

*Copyright Maven Analytics, LLC


MySQL COMMUNITY SERVER – MAC DOWNLOAD GUIDE

1 Go to https://dev.mysql.com/downloads and download MySQL Community Server

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!

*Copyright Maven Analytics, LLC


MySQL COMMUNITY SERVER – MAC DOWNLOAD GUIDE

1 3 5

2 4

*Copyright Maven Analytics, LLC


STEP 1: COMMUNITY SERVER (PC)
HEY THIS IS IMPORTANT!
If you took one of my other courses, and have already installed Community Server and MySQL Workbench,
then you can skip ahead to creating the database. No need to re-install. Whatever version you have is great.

*Copyright Maven Analytics, LLC


MySQL COMMUNITY SERVER – PC DOWNLOAD GUIDE

1 Go to https://dev.mysql.com/downloads and download MySQL Community Server

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!

*Copyright Maven Analytics, LLC


MySQL COMMUNITY SERVER – PC DOWNLOAD GUIDE

1 3 5

2 4

*Copyright Maven Analytics, LLC


STEP 2: MySQL WORKBENCH (MAC)
HEY THIS IS IMPORTANT!
If you took one of my other courses, and have already installed Community Server and MySQL Workbench,
then you can skip ahead to creating the database. No need to re-install. Whatever version you have is great.

*Copyright Maven Analytics, LLC


MySQL WORKBENCH – MAC DOWNLOAD GUIDE

1 Go to https://dev.mysql.com/downloads/workbench, scroll down to Generally Available


(GA) Releases, and select the MacOS operating system

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

*Copyright Maven Analytics, LLC


MySQL WORKBENCH – MAC DOWNLOAD GUIDE

1 3 5

2 Look for version 8.0.16, or download the latest


4

*Copyright Maven Analytics, LLC


STEP 2: MySQL WORKBENCH (PC)
HEY THIS IS IMPORTANT!
If you took one of my other courses, and have already installed Community Server and MySQL Workbench,
then you can skip ahead to creating the database. No need to re-install. Whatever version you have is great.

*Copyright Maven Analytics, LLC


MySQL WORKBENCH – PC DOWNLOAD GUIDE

1 Go to https://dev.mysql.com/downloads/workbench, scroll down to Generally Available


(GA) Releases, and select the Microsoft Windows operating system

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

*Copyright Maven Analytics, LLC


MySQL WORKBENCH – PC DOWNLOAD GUIDE

1 3 5

2 Look for version 8.0.13, or download the latest


4

*Copyright Maven Analytics, LLC


STEP 3: CONNECTING TO THE SERVER

*Copyright Maven Analytics, LLC


CONNECTING TO THE SERVER

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!

*Copyright Maven Analytics, LLC


CONNECTING TO THE SERVER

1 3

2 4

*Copyright Maven Analytics, LLC


STEP 4: MySQL WORKBENCH INTERFACE

*Copyright Maven Analytics, LLC


MySQL WORKBENCH INTERFACE (MAC VS. PC)

Mac interface PC interface

HEY THIS IS IMPORTANT!


Workbench looks slightly different on Mac vs. PC, but everything you need is found in the same place.
While the course is recorded on a Mac, but you should have no problem keeping up on a PC

*Copyright Maven Analytics, LLC


QUICK TOUR: THE WORKBENCH INTERFACE
Query Editor Window
This is where you write and run your code

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!)

*Copyright Maven Analytics, LLC


QUICK TOUR: THE WORKBENCH INTERFACE

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

*Copyright Maven Analytics, LLC


IMPORTANT: PREPARE WORKBENCH SETTINGS

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

*Copyright Maven Analytics, LLC


INSERTING DATA INTO THE DATABASE

*Copyright Maven Analytics, LLC


REVIEW OF CREATE, INSERT, & DELETE

1 First, we’ll review CREATE SCHEMA to get started

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.

*Copyright Maven Analytics, LLC


REVIEW OF CREATE SCHEMA

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.

*Copyright Maven Analytics, LLC


CREATE SCHEMA W/ UI MySQL WORKBENCH TOOLS IN ACTION:

• In MySQL, the terms ‘Schema’


and ‘Database’ will be used
interchangeably to discuss a
collection of data tables which
relate to one another

• We can create a new schema


using Workbench’s user
interface tools, with just a few
quick clicks

• Whether we create schemas


using the UI tools or using code
is a matter of preference
*Copyright Maven Analytics, LLC
REVIEW OF CREATE TABLE

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.

*Copyright Maven Analytics, LLC


CREATE TABLE W/ CODE MySQL QUERY IN ACTION:

• After our schema is created, we can


begin populating our schema with
tables that will contain records of
data

• Creating a new table with code


typically involves: ACTION OUTPUT:
• Selecting a schema to add the table to
• Giving the new table a name
• Specifying which columns to include
• Setting data formats and types

*Copyright Maven Analytics, LLC


REVIEW OF INSERTING RECORDS

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.

*Copyright Maven Analytics, LLC


INSERTING NEW RECORDS MySQL QUERY IN ACTION:

• MySQL makes it easy to insert


additional records into a table
using an INSERT INTO statement

• We can insert one row at a time, or


we can batch insert many rows in
one single SQL statement (Another) MySQL QUERY IN ACTION:

• We may choose to specify values


for every column in our INSERT
INTO statement, or we may omit
values for some columns. If we
omit values, MySQL places the
default value for that column
*Copyright Maven Analytics, LLC
REVIEW OF DELETING RECORDS

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.

*Copyright Maven Analytics, LLC


DELETE RECORDS MySQL QUERY IN ACTION:

• MySQL allows us to target one or


more records to DELETE FROM a
given data table

• We will do this using the DELETE


FROM statement, followed by the
tablename
ACTION OUTPUT:

• As with UPDATE statements, we will


usually want to specify which records
to delete using a WHERE clause

• If we do not specify a WHERE clause,


DELETE FROM will delete ALL records
*Copyright Maven Analytics, LLC
IMPORT DATA FROM A FILE MySQL QUERY IN ACTION:

• Sometimes we will want to import


a large amount of data at once

• Writing an INSERT statement and


specifying every row of a large data
set would be very tedious work
Accessing the Table Data Import Wizard:
• Luckily, MySQL Workbench
provides a couple of ways to
import data in bulk

• The method we will primarily be


using is the Table Data Import
Wizard
*Copyright Maven Analytics, LLC
NEW MESSAGE Helpful Hints
April 2, 2012

From: Sally Bleu (CEO)


Subject: Help get order data into the database

Good morning,

I’m excited to get going on the data centralization


project! As a first step, let’s get the items customers
are ordering stored in the database.

I’ve attached 2 files from our sales software. Can you


create a table and import this data into our database?
Thanks!

-Sally

01.order_items_2012_Mar
02.order_items_2012_Apr

TEST YOUR SKILLS: IMPORT ORDER ITEM DATA


*Copyright Maven Analytics, LLC
Solution
NEW MESSAGE
April 2, 2012

From: Sally Bleu (CEO)


Subject: Help get order data into the database

Good morning,

I’m excited to get going on the data centralization


project! As a first step, let’s get the items customers
are ordering stored in the database.

I’ve attached 2 files from our sales software. Can you


create a table and import this data into our database?
Thanks!

-Sally

01.order_items_2012_Mar
02.order_items_2012_Apr

TEST YOUR SKILLS: IMPORT ORDER ITEM DATA


*Copyright Maven Analytics, LLC
NEW MESSAGE Helpful Hints
April 5, 2012

From: Sally Bleu (CEO)


Subject: Can we aggregate refunds too?

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?

It will be great to start tracking refund rates better so


we can keep an eye on product quality.

Thanks!

-Sally

03.order_item_refunds_2012_Apr

TEST YOUR SKILLS: IMPORT REFUND DATA


*Copyright Maven Analytics, LLC
Solution
NEW MESSAGE
April 5, 2012

From: Sally Bleu (CEO)


Subject: Can we aggregate refunds too?

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?

It will be great to start tracking refund rates better so


we can keep an eye on product quality.

Thanks!

-Sally

03.order_item_refunds_2012_Apr

TEST YOUR SKILLS: IMPORT REFUND DATA


*Copyright Maven Analytics, LLC
NEW MESSAGE Helpful Hints
April 8, 2012

From: Sally Bleu (CEO)


Subject: Help Cleaning Up Some Bad Data

Well…

Turns out the new guy messed up some of our data. He


flagged order_items 131, 132, 145, 151, and 153 as
refunds, but they were actually customer inquiries.

Can you remove these from order_item_refunds to


clean up our data?
Thanks!

-Sally

TEST YOUR SKILLS: DELETING RECORDS


*Copyright Maven Analytics, LLC
Solution
NEW MESSAGE
April 8, 2012

From: Sally Bleu (CEO)


Subject: Help Cleaning Up Some Bad Data

Well…

Turns out the new guy messed up some of our data. He


flagged order_items 131, 132, 145, 151, and 153 as
refunds, but they were actually customer inquiries.

Can you remove these from order_item_refunds to


clean up our data?
Thanks!

-Sally

TEST YOUR SKILLS: DELETING RECORDS


*Copyright Maven Analytics, LLC
NEW MESSAGE Helpful Hint
January 02, 2013

From: Sally Bleu (CEO)


Subject: Time to pull in all 2012 data

Hey,

The business finished 2012 strong, and I would like to


get all of our order_items and order_item_refunds
data updated through the end of the year.

Can you help me import the 2 attached files into the


correct tables?

Thanks!

-Sally

04.order_items_2012_May-Dec
05.order_item_refunds_May-Dec

TEST YOUR SKILLS: IMPORT REMAINING 2012 DATA


*Copyright Maven Analytics, LLC
Solution
NEW MESSAGE
January 02, 2013

From: Sally Bleu (CEO)


Subject: Time to pull in all 2012 data

Hey,

The business finished 2012 strong, and I would like to


get all of our order_items and order_item_refunds
data updated through the end of the year.

Can you help me import the 2 attached files into the


correct tables?

Thanks!

-Sally

04.order_items_2012_May-Dec
05.order_item_refunds_May-Dec

TEST YOUR SKILLS: IMPORT REMAINING 2012 DATA


*Copyright Maven Analytics, LLC
LOAD DATA MySQL LOAD DATA SYNTAX

• Another method for importing


large datasets is to use LOAD DATA

• One major benefit is that using


LOAD DATA can be faster when
your data sets get large

• Another advantage is that


Engineers can automate scripts
that contain LOAD DATA scripts

NOT FOR EVERYONE!


LOAD DATA is not enabled by default, and enabling it requires editing your configuration file using command line.
This is too technical for many in our audience, so we are not going to teach it in detail.

*Copyright Maven Analytics, LLC


ALTER, UPDATE, & KEYS

*Copyright Maven Analytics, LLC


REVIEW OF ALTER, UPDATE, AND KEY RELATIONSHIPS

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.

*Copyright Maven Analytics, LLC


REVIEW OF ALTER TABLE

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.

*Copyright Maven Analytics, LLC


ALTER TABLE W/ CODE MySQL QUERY IN ACTION:

• We can use SQL code to alter


tables by adding and dropping
columns

• We specify ALTER TABLE


tablename, and then we can either
ADD or DROP a column ACTION OUTPUT:

• When we ADD a column, we must


also specify its data type

• Optionally when we ADD a column,


we may specify where it should
appear, by using FIRST or AFTER
*Copyright Maven Analytics, LLC
REVIEW OF UPDATING RECORDS

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.

*Copyright Maven Analytics, LLC


UPDATING RECORDS MySQL QUERY IN ACTION:

• We can update the values of one or


more records using an UPDATE
statement

• Our UPDATE statement will always


require a SET clause, which tells the
server what values to set (ANOTHER) MySQL QUERY IN ACTION:

• In most cases, we will also use a


WHERE clause (optional) to specify
which records to update. If we do
not use a WHERE clause, the
UPDATE will process on all records

*Copyright Maven Analytics, LLC


REVIEW OF TABLE RELATIONSHIPS

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.

*Copyright Maven Analytics, LLC


TABLE RELATIONSHIPS & CARDINALITY

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

*Copyright 2019, Excel Maven & Maven Analytics, LLC


NEW MESSAGE Result Preview
January 05, 2013

From: Sally Bleu (CEO)


Subject: New table to track products

Hey,
Tomorrow we’re launching a new product called The
Forever Love Bear to complement The Original Mr. Fuzzy.

Could you please create a products table in the database?


Track when they launched (2012-03-09 at 9am and 2013-
01-06 at 1pm, respectively), the product name, and
assign an id so we can link to other tables later.

Thanks!
-Sally

TEST YOUR SKILLS: ADDING A NEW PRODUCT


*Copyright Maven Analytics, LLC
Solution Queries
NEW MESSAGE
January 05, 2013

From: Sally Bleu (CEO)


Subject: New table to track products

Hey,
Tomorrow we’re launching a new product called The
Forever Love Bear to complement The Original Mr. Fuzzy.

Could you please create a products table in the database?


Track when they launched (2012-03-09 at 9am and 2013-
01-06 at 1pm, respectively), the product name, and
assign an id so we can link to other tables later.

Thanks!
-Sally

TEST YOUR SKILLS: ADDING A NEW PRODUCT


*Copyright Maven Analytics, LLC
NEW MESSAGE Result Preview
January 06, 2013

From: Sally Bleu (CEO)


Subject: Add product data to item sales

Good morning,

Later today, we’ll have multiple products selling, I


would love to be able to tie our order_items data to
the product sold.

Can you please add product_id to the order_items


table?

Thanks for the help!


-Sally

TEST YOUR SKILLS: ADDING PRODUCT TO ORDER_ITEMS


*Copyright Maven Analytics, LLC
Solution Query
NEW MESSAGE
January 06, 2013

From: Sally Bleu (CEO)


Subject: Add product data to item sales

Good morning,

Later today, we’ll have multiple products selling, I


would love to be able to tie our order_items data to
the product sold.

Can you please add product_id to the order_items


table?

Thanks for the help!


-Sally

TEST YOUR SKILLS: ADDING PRODUCT TO ORDER_ITEMS


*Copyright Maven Analytics, LLC
NEW MESSAGE Result Preview
January 07, 2013

From: Sally Bleu (CEO)


Subject: Back-populate sales with product_id

Hey there,

I noticed that all records in order_items show NULL for


product_id.

All of the sales reflected in the database are for


product 1, so could you update the records to reflect
that? Then we’ll have perfect data to use in the future.

Thanks for the help!


-Sally

TEST YOUR SKILLS: UPDATING ORDER_ITEMS


*Copyright Maven Analytics, LLC
Solution Query
NEW MESSAGE
January 07, 2013

From: Sally Bleu (CEO)


Subject: Back-populate sales with product_id

Hey there,

I noticed that all records in order_items show NULL for


product_id.

All of the sales reflected in the database are for


product 1, so could you update the records to reflect
that? Then we’ll have perfect data to use in the future.

Thanks for the help!


-Sally

TEST YOUR SKILLS: UPDATING ORDER_ITEMS


*Copyright Maven Analytics, LLC
NEW MESSAGE Helpful Hint
January 07, 2013

From: Sally Bleu (CEO)


Subject: Primary and foreign keys?

Hey there,

I would like to make sure the newly-related


order_items and products tables have the right
relationships specified in the database.

Can you set up the proper primary and foreign key


relationships between those two tables?

Thank you!
-Sally

TEST YOUR SKILLS: PRIMARY AND FOREIGN KEYS


*Copyright Maven Analytics, LLC
Solution
NEW MESSAGE
January 07, 2013

From: Sally Bleu (CEO)


Subject: Primary and foreign keys?

Hey there,

I would like to make sure the newly-related


order_items and products tables have the right
relationships specified in the database.

Can you set up the proper primary and foreign key


relationships between those two tables?

Thank you!
-Sally

TEST YOUR SKILLS: PRIMARY AND FOREIGN KEYS


*Copyright Maven Analytics, LLC
NEW MESSAGE Helpful Hint
April 1, 2013

From: Sally Bleu (CEO)


Subject: Help get order data into the database

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.

Really curious to start digging into sales trends!

Thanks!
-Sally

06.order_items_2013_Jan-Mar
07.order_item_refunds_2013_Jan-Mar

TEST YOUR SKILLS: IMPORT 2013 Q1 DATA


*Copyright Maven Analytics, LLC
Solution
NEW MESSAGE
April 1, 2013

From: Sally Bleu (CEO)


Subject: Help get order data into the database

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.

Really curious to start digging into sales trends!

Thanks!
-Sally

06.order_items_2013_Jan-Mar
07.order_item_refunds_2013_Jan-Mar

TEST YOUR SKILLS: IMPORT 2013 Q1 DATA


*Copyright Maven Analytics, LLC
DATABASE REPLICATION

*Copyright Maven Analytics, LLC


INTRODUCTION TO REPLICATION

Replication enables us to store the same data on two or more servers by creating
copies, known as replicas

Master Replica BENEFITS & USES:


• Replication can serve as a form of backup

• Creating a read only replica for reporting


purposes keeps analysis out of production

• In certain cases, splitting the load between two


servers can improve application performance

*Copyright 2019, Excel Maven & Maven Analytics, LLC


ROW-BASED BINARY LOG FILE REPLICATION

The first method of Replication we are going to focus on is Row-based Replication


using Binary Log files

Master Replica IMPORTANT POINTS


• Binary Log files record changes made to records
Replica makes a request
of data (think “before and after”)

• Replication typically (not always) happens


asynchronously, so the Replica can lag behind

• Row-based replication is typically faster than


statement-based replication, but can be harder
to audit because it does not contain SQL
statements
Relay Log File Write to Replica
Binary Log File (IO Thread) (SQL thread)

*Copyright 2019, Excel Maven & Maven Analytics, LLC


STATEMENT-BASED BINARY LOG FILE REPLICATION

The next method of Replication we are going to discuss is Statement-based Replication


using Binary Log files. Much of this will feel similar to row-based replication.

Master Replica IMPORTANT POINTS


• Binary Log files record the actual SQL statements
Replica makes a request
that make changes to the database

• Statement-based replication is typically easier to


read and audit compared to row-based

• Non-deterministic queries can be big trouble

Relay Log File Write to Replica


Binary Log File (IO Thread) (SQL thread)

*Copyright 2019, Excel Maven & Maven Analytics, LLC


ROW-BASED vs STATEMENT-BASED REPLICATION

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.

*Copyright 2019, Excel Maven & Maven Analytics, LLC


BACKUP & RECOVERY

*Copyright Maven Analytics, LLC


INTRODUCTION TO BACKUP & RECOVERY

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.

*Copyright 2019, Excel Maven & Maven Analytics, LLC


mysqldump MySQL WORKBENCH IN ACTION:

• mysqldump is a database backup


method we can use to make sure
we can restore data if we lose it

• You can initiate mysqldump from


command line, or in Workbench
using the wizard

• When you use mysqldump, the


output will be a .sql file that
contains the create statements and
insert statements for all of the
tables you have dumped

*Copyright Maven Analytics, LLC


NEW MESSAGE Helpful Hints
April 10, 2013

From: Sally Bleu (CEO)


Subject: Need a Backup & Recover Plan

Good morning,

One of our board members is upset because we


haven’t taken any steps to backup the data we’re
aggregating on the MySQL Server.

I would like you to put together a written proposal for


everything we should be doing in terms of replication,
backup, and recovery. And take a snapshot of the data
while you’re at it. Thanks!

-Sally

TEST YOUR SKILLS: BACKUP & RECOVERY


*Copyright Maven Analytics, LLC
Solution Query
NEW MESSAGE
April 10, 2013

From: Sally Bleu (CEO)


Subject: Need a Backup & Recover Plan

Good morning,

One of our board members is upset because we


haven’t taken any steps to backup the data we’re
aggregating on the MySQL Server.

I would like you to put together a written proposal for


everything we should be doing in terms of replication,
backup, and recovery. And take a snapshot of the data
while you’re at it. Thanks!

-Sally

TEST YOUR SKILLS: BACKUP & RECOVERY


*Copyright Maven Analytics, LLC
INTRODUCING THE MID COURSE PROJECT

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.

THE Use SQL to:


OBJECTIVE • Import additional data into the bearbuilders database
• Enhance the data structure to accommodate new tracking needs for the business

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!

*Copyright Maven Analytics, LLC


MID COURSE PROJECT QUESTIONS

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

Next, help update the structure of the order_items table:


2
• The company is going to start cross-selling products and will want to track whether each item sold is the
~ 4:20 primary item (the first one put into the user’s shopping cart) or a cross-sold item
• Add a binary column to the order_items table called is_primary_item

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

*Copyright Maven Analytics, LLC


MID COURSE PROJECT QUESTIONS

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.

*Copyright Maven Analytics, LLC


AUTOMATION USING TRIGGERS

THIS FIRST TRIGGERS VIDEO IS REVIEW!


We covered the basics of TRIGGERS in our MySQL Database Administration for Beginners course. If you already have
these concepts covered, feel free to skip ahead. If you want review of this important concept, check it out!

*Copyright Maven Analytics, LLC


TRIGGERS REVIEW MySQL Workbench in Action

• MySQL allows us to create Triggers,


where we can prescribe certain
actions on a table to trigger one or
more other actions to occur

• We may prescribe that our


triggered action occurs either Result Preview (SELECT * FROM inventory)
BEFORE or AFTER an INSERT,
UPDATE, or a DELETE

• Triggers are a very common way to


make sure related tables remain in
sync as they are updated over time

*Copyright Maven Analytics, LLC


TRIGGERS DEEP DIVE MySQL Workbench in Action

• MySQL supports only row-level


Triggers, however in other syntax
languages, statement-level Triggers
are supported

• MySQL does not support ALTER


TRIGGER, so to modify an existing Trigger Privileges
Trigger, you’ll use DROP TRIGGER
and then CREATE TRIGGER

• In order to CREATE, DROP or view Schema-Specific


the Triggers on a table, you will
need to have the Triggers privilege Global
enabled for that table
TRIGGERS DEEP DIVE MySQL Workbench in Action

• In some cases, using Triggers with


BEFORE and AFTER will produce
different results

• Multiple Triggers can exist on the


same table. By default, they will fire in
creation order. This can be modified Check Out Active Triggers
using FOLLOWS or PROCEDES

• To see Triggers in a schema, use


SHOW TRIGGERS. Or you can query
the INFORMATION_SCHEMA to see
all triggers in your instance

*Copyright Maven Analytics, LLC


TRIGGERS DEEP DIVE MySQL Workbench in Action

• When creating a Trigger, BEFORE or


AFTER is required, as is INSERT,
UPDATE, or DELETE, naming the table,
Error Code: 1363.
and including a valid SQL statement There is no OLD row
in on INSERT trigger
• FOLLOWS or PRECEDES are optional
RECAP: Triggers Syntax w/ Optional Modifiers
• Specifying OLD or NEW when
referencing columns in your
statement is an optional modifier

• Note that INSERT Triggers do not work


with OLD (no OLD value exists) and
DELETE Triggers won’t work with NEW
*Copyright Maven Analytics, LLC
NEW MESSAGE Helpful Hint
January 03, 2014

From: Sally Bleu (CEO)


Subject: Create an order summary table

Morning! Now that we’re selling multiple products, it


would be great to have a table summarizing full orders.

Can you create a table to capture order_id, a created_at


timestamp, website_session_id, primary product_id, # of
items purchased, price and cogs in USD?

Could you also back-populate the table using the records


from our order_items table?

Thank you!
-Sally

TEST YOUR SKILLS: ORDER SUMMARY DATA


*Copyright Maven Analytics, LLC
Solution Query
NEW MESSAGE
January 03, 2014

From: Sally Bleu (CEO)


Subject: Create an order summary table

Morning! Now that we’re selling multiple products, it


would be great to have a table summarizing full orders.

Can you create a table to capture order_id, a created_at


timestamp, website_session_id, primary product_id, # of
items purchased, price and cogs in USD?

Could you also back-populate the table using the records


from our order_items table?

Thank you!
-Sally

TEST YOUR SKILLS: ORDER SUMMARY DATA


*Copyright Maven Analytics, LLC
NEW MESSAGE Helpful Hints
January 05, 2014

From: Sally Bleu (CEO)


Subject: Automation to update orders table

Hey there,

The new orders table you created is so helpful!

Next, would you be able to set up some automation so


that anytime order_items records are inserted into the
database, the orders table is updated as well?

Thank you!
-Sally

TEST YOUR SKILLS: AUTOMATED ORDERS TRIGGER


*Copyright Maven Analytics, LLC
Solution Query
NEW MESSAGE
January 05, 2014

From: Sally Bleu (CEO)


Subject: Automation to update orders table

Hey there,

The new orders table you created is so helpful!

Next, would you be able to set up some automation so


that anytime order_items records are inserted into the
database, the orders table is updated as well?

Thank you!
-Sally

TEST YOUR SKILLS: AUTOMATED ORDERS TRIGGER


*Copyright Maven Analytics, LLC
NEW MESSAGE Result Preview
March 01, 2014

From: Sally Bleu (CEO)


Subject: Putting your automation to the test

Hey,

It’s time to see if your trigger to sync the order_items


and orders tables is working correctly.

Why don’t you go ahead and update the order_items


and order_item_refunds tables with the attached data
and we’ll see how everything is working.

Thank you!
-Sally

12.order_items_2014_Jan-Feb
13.order_item_refunds_2014_Jan-Feb

TEST YOUR SKILLS: TESTING YOUR TRIGGER


*Copyright Maven Analytics, LLC
Solution
NEW MESSAGE
March 01, 2014

From: Sally Bleu (CEO)


Subject: Putting your automation to the test

Hey,

It’s time to see if your trigger to sync the order_items


and orders tables is working correctly.

Why don’t you go ahead and update the order_items


and order_item_refunds tables with the attached data
and we’ll see how everything is working.

Thank you!
-Sally

12.order_items_2014_Jan-Feb
13.order_item_refunds_2014_Jan-Feb

TEST YOUR SKILLS: TESTING YOUR TRIGGER


*Copyright Maven Analytics, LLC
VIEWS MySQL Workbench in Action

• A view is like a “virtual table”;


contents are defined by a query,
and data is not physically created

• One advantage of views is the


ability to aggregate data from
multiple tables into one view. Result Preview

• Another advantage is security – you


can grant someone access to
certain views, exclusively

• Finally, altering views will not affect


any downstream systems
*Copyright Maven Analytics, LLC
NEW MESSAGE Helpful Hints
March 07, 2014

From: Brent Cheeseman (Marketing Lead)


Subject: Tying website activity to sales

Hey data rockstar!

Sally tells me the great things you’ve done with our


order data. I have this website session data that I would
love to tie into that order data so we can better
understand where sales are coming from.

Can you create a website_sessions table and help me


import the attached files?
-Brent

14.website_sessions_2014_Jan
15.website_sessions_2014_Feb

TEST YOUR SKILLS: TRACKING WEBSITE SESSIONS


*Copyright Maven Analytics, LLC
Solution Query
NEW MESSAGE
March 07, 2014

From: Brent Cheeseman (Marketing Lead)


Subject: Tying website activity to sales

Hey data rockstar!

Sally tells me the great things you’ve done with our


order data. I have this website session data that I would
love to tie into that order data so we can better
understand where sales are coming from.

Can you create a website_sessions table and help me


import the attached files?
-Brent

14.website_sessions_2014_Jan
15.website_sessions_2014_Feb

TEST YOUR SKILLS: TRACKING WEBSITE SESSIONS


*Copyright Maven Analytics, LLC
NEW MESSAGE Helpful Hints
March 09, 2014

From: Brent Cheeseman (Marketing Lead)


Subject: Reporting Views

Hey, thanks for getting that website_session data


imported!

Next, would you be able to create a view summarizing


performance for January and February? I would like to
see the number of sessions sliced by year, month,
utm_source, and utm_campaign if possible.

Thanks!

-Brent

TEST YOUR SKILLS: CREATING VIEWS FOR REPORTING


*Copyright Maven Analytics, LLC
Solution Query
NEW MESSAGE
March 09, 2014

From: Brent Cheeseman (Marketing Lead)


Subject: Reporting Views

Hey, thanks for getting that website_session data


imported!

Next, would you be able to create a view summarizing


performance for January and February? I would like to
see the number of sessions sliced by month,
utm_source, and utm_campaign if possible.

Thanks!

-Brent

TEST YOUR SKILLS: CREATING VIEWS FOR REPORTING


*Copyright Maven Analytics, LLC
EER DIAGRAMS

THIS FIRST PART IS REVIEW!


We covered the basics of EER Diagrams in our MySQL Database Administration for Beginners course. If you already
have these concepts covered, feel free to skip ahead. If you want to review this important concept, watch the video.

*Copyright Maven Analytics, LLC


CREATE AN EER DIAGRAM FROM SCRATCH

1 3 5

2 4

*Copyright Maven Analytics, LLC


CREATE AN EER DIAGRAM VIA REVERSE ENGINEERING

2 4

*Copyright Maven Analytics, LLC


NEW MESSAGE Helpful Hints
March 15, 2014

From: Sally Bleu (CEO)


Subject: Can we make an EER Diagram?

Good morning,

Another CEO friend of mine just walked me through


their database using an EER diagram. It was so easy to
understand how everything connected. We should
have one!

Can you put together an EER Diagram for us?

Thanks!

-Sally

TEST YOUR SKILLS: CREATING EER DIAGRAMS


*Copyright Maven Analytics, LLC
Solution Query
NEW MESSAGE
March 15, 2014

From: Sally Bleu (CEO)


Subject: Can we make an EER Diagram?

Good morning,

Another CEO friend of mine just walked me through


their database using an EER diagram. It was so easy to
understand how everything connected. We should
have one!

Can you put together an EER Diagram for us?

Thanks!

-Sally

TEST YOUR SKILLS: CREATING EER DIAGRAMS


*Copyright Maven Analytics, LLC
STORED PROCEDURES & EVENTS

*Copyright Maven Analytics, LLC


STORED PROCEDURES MySQL Workbench in Action

• MySQL gives us the ability to


store and call frequently used
queries on the server. These are
then referred to as ‘Procedures’,
or commonly ‘Stored Procedures’

• Benefits include more efficient


query writing and performance,
and the ability to share complex
procedures more easily between
Analysts and database users

• We invoke the stored procedure


using a CALL statement
*Copyright Maven Analytics, LLC
STORED PROCEDURES MySQL Workbench in Action

• Stored Procedures can be used


with parameters, which can be IN,
OUT, or INOUT

• IN parameters take in a value from


the user or application. Values are
not returned by the Procedure

• OUT parameters have their values


returned by the Procedure

• INOUT is a combination of IN and


OUT parameter modes. It can take
in a value, which will also output
*Copyright Maven Analytics, LLC
STORED PROCEDURES MySQL Workbench in Action

• OUT parameters have their


values returned by the Procedure

• If you want, you can specify a


variable to store the output
returned in an OUT parameter

• INOUT is a combination of IN and


OUT parameter modes. It can
take in a value, which will also
output

*Copyright Maven Analytics, LLC


SCHEDULED EVENTS MySQL Workbench in Action

• MySQL allows us to specify Events,


often referred to as Scheduled
Events

• Similar to Stored Procedures,


Events are created with a user-
defined SQL operation

• Unlike Procedures, Events will fire


at a certain time, or at regular
recurring intervals

*Copyright Maven Analytics, LLC


NEW MESSAGE Result Preview
April 1, 2014

From: Sally Bleu (CEO)


Subject: Help me pull reports on my own?

Hey there!

With all this great data, I would love to be able to


quickly pull together the total orders and revenue for a
given time period. I’m not a SQL guru though.

Is there a way I could specify a startDate and endDate


and see total orders and revenue during that period?

Thanks for the help!


-Sally

TEST YOUR SKILLS: STORED PROCEDURES


*Copyright Maven Analytics, LLC
Solution Query
NEW MESSAGE
April 1, 2014

From: Sally Bleu (CEO)


Subject: Help me pull reports on my own?

Hey there!

With all this great data, I would love to be able to


quickly pull together the total orders and revenue for a
given time period. I’m not a SQL guru though.

Is there a way I could specify a startDate and endDate


and see total orders and revenue during that period?

Thanks for the help!


-Sally

TEST YOUR SKILLS: STORED PROCEDURES


*Copyright Maven Analytics, LLC
WRITING DATA FROM WEBSITES

*Copyright Maven Analytics, LLC


WRITING DATA FROM WEBSITES

EMAIL

SOCIAL

PAGE A PAGE B

SEARCH

DIRECT

*Copyright 2019, Excel Maven & Maven Analytics, LLC


TYPES OF DATA WEBSITES CAPTURE AT SESSION START

TRAFFIC SOURCE DATA

USER-RELATED DATA TECHNOLOGY DATA

*Copyright 2019, Excel Maven & Maven Analytics, LLC


TYPES OF DATA WEBSITES CAPTURE ALONG THE WAY

Pageview and Other Event / Activity Data:


• As activities happen on the website, data records
are created to track and measure behavior

• This can happen on a page load, the download of a


white paper, etc.

Purchases + Key Business Activities


• Business will always want to capture
data related to purchases, subscriptions,
lead signups, etc.

• Many times this data is triggered by


website activities
*Copyright 2019, Excel Maven & Maven Analytics, LLC
RECAP OF CAPTURING WEBSITE DATA

PAGE A PAGE B

• User-related data • Pageviews • Purchases


(cookies)
• Other important events • Subscriptions
• Traffic source data
• Lead signups
• Technology data
• Other business activity
• Pageview data

*Copyright 2019, Excel Maven & Maven Analytics, LLC


NEW MESSAGE Helpful Hints
April 6, 2014

From: Molly Monterey (Website Manager)


Subject: Any recommended enhancements?

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

TEST YOUR SKILLS: WRITING WEBSITE DATA


*Copyright Maven Analytics, LLC
Solution
NEW MESSAGE
April 6, 2014

From: Molly Monterey (Website Manager)


Subject: Any recommended enhancements?

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

TEST YOUR SKILLS: WRITING WEBSITE DATA


*Copyright Maven Analytics, LLC
NEW MESSAGE Helpful Hints
April 15, 2014

From: Molly Monterey (Website Manager)


Subject: Loading Website Pageview Data

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

TEST YOUR SKILLS: IMPORTING PAGEVIEW DATA


*Copyright Maven Analytics, LLC
Solution Query
NEW MESSAGE
April 15, 2014

From: Molly Monterey (Website Manager)


Subject: Loading Website Pageview Data

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

TEST YOUR SKILLS: IMPORTING PAGEVIEW DATA


*Copyright Maven Analytics, LLC
SECURITY

*Copyright Maven Analytics, LLC


COMMON SECURITY THREATS & ATTACKS

Attackers can take advantage of limited or weak authentication


1 Weak Authentication methods in place to break through your perimeter

DoS attackers will attempt to flood or crash your system,


2 Denial of Service rendering it useless for legitimate users

Attackers will use this strategy to gain access to higher levels


3 Privilege Escalation of your systems and information you don’t want them to see

Various endpoints of your system can create vulnerabilities for


4 SQL Injection hackers who understand how your back-end works

Similar to SQL injection, attackers can take advantage of holes


5 Buffer Overflow in your application and make it do things it shouldn’t

Malware attacks will render your data unusable unless you


6 Ransomware agree to pay a ransom

*Copyright Maven Analytics, LLC


WEAK AUTHENTICATION

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.

They may do this by taking advantage of:


• Weak passwords
• Passwords that do not change frequently
• Lack of a two-factor or multi-factor authentication mechanism

*Copyright 2019, Excel Maven & Maven Analytics, LLC


DENIAL OF SERVICE ATTACKS

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.

*Copyright 2019, Excel Maven & Maven Analytics, LLC


PRIVILEGE ESCALATION ATTACKS

With privilege escalation attacks, hackers will attempt to gain access to


increasingly secure levels of information by authenticating as a lower level user
first, then climbing the ladder.

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.

Some common methods for implementing these attacks:


With cookie-based authentication, hackers
• Cookie-based authentication may spoof this to impersonate another user

• Form-based authentication
user_id = 666

*Copyright 2019, Excel Maven & Maven Analytics, LLC


SQL INJECTION ATTACKS

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.

Common ways attackers might use SQL injection maliciously:


• Gain information about your database (structure, version, etc.)
• Return results from your database that they shouldn’t see

User Name Password


admin badPW SELECT * FROM users WHERE name = ‘admin’ AND pass = ‘badPW’

User Name Password


admin’ -- SELECT * FROM users WHERE name = ‘admin’ -- ’ AND pass = ‘’

*Copyright 2019, Excel Maven & Maven Analytics, LLC


BUFFER OVERFLOW VULNERABILITY

A buffer is a sequential section of memory which will contain anything from a


string to an array of numbers.

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.

Attackers can deliberately overflow your application to crash it or to force it to


do things you might not want it to do.

*Copyright 2019, Excel Maven & Maven Analytics, LLC


RANSOMWARE

Ransomware has become more prevalent in recent years. It represents an


attractive opportunity for hackers and an expensive problem for businesses.

When a business is attacked by ransomware, systems will be rendered unusable


and the attacker will demand payment (typically in the form of cryptocurrency)

Malware most often breaches systems by getting employees to download a file


from their email, but social media attacks have been reported as well.

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.

*Copyright 2019, Excel Maven & Maven Analytics, LLC


SECURITY BEST PRACTICES

Steer clear of storing personal information if you can. Anything


1 Practice Safe Data Storage sensitive that you do need to store should be encrypted.

Practice “minimum viable access”. Don’t grant more permissions


2 Limit Access than are needed for the job.

Require strong passwords, require changing frequently, and


3 Take Authentication Seriously enable multi-factor authentication.

Whether it is a team, a person, or some percentage of a person’s


4 Dedicate Resources to Security time, someone in your organization should be focused on security.

Come up with a formal security policy. Write it down. Train your


5 Have a Data Security Policy employees on best practices.

Backup your data. Log changes & monitor access. Perform


6 Backup, Log, Monitor & Audit periodic audits & invite third parties to try and hack you.

*Copyright Maven Analytics, LLC


PRACTICE SAFE DATA STORAGE

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?

*Copyright Maven Analytics, LLC


LIMIT ACCESS TO SYSTEMS AND DATA

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

*Copyright Maven Analytics, LLC


TAKE AUTHENTICATION SERIOUSLY

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

Require employees to change their password once every 60 to 90 days


• They may hate it, but it will mitigate potential attacks

Leverage two-factor or multi-factor authentication methods, especially for employees with


access to sensitive information

*Copyright Maven Analytics, LLC


DEDICATE RESOURCES TO SECURITY

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

Make security a part of someone’s job description and performance review

Give the responsible employee the authority to work with the rest of your organization
to ensure that things are set up properly

*Copyright Maven Analytics, LLC


HAVE A WRITTEN SECURITY POLICY

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

Share your written security policy with all employees

Make sure you are continually reviewing and revising your security policy as your business
and your risks evolve over time

*Copyright Maven Analytics, LLC


BACKUP, LOG & MONITOR YOUR DATA. PERFORM AUDITS

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

*Copyright Maven Analytics, LLC


NEW MESSAGE Result Preview
April 20, 2014

From: Sally Blue (CEO)


Subject: We need a security plan!

Hey there!

I know you’ve been learning a lot about security, and


the board is giving me a lot of pressure on that topic.

Can you put together a comprehensive security plan for


us? Include anything you think makes sense for today,
and also as we scale up.

-Sally

TEST YOUR SKILLS: DATABASE SECURITY


*Copyright Maven Analytics, LLC
Solution Query
NEW MESSAGE
April 20, 2014

From: Sally Blue (CEO)


Subject: We need a security plan!

Hey there!

I know you’ve been learning a lot about security, and


the board is giving me a lot of pressure on that topic.

Can you put together a comprehensive security plan for


us? Include anything you think makes sense for today,
and also as we scale up.

-Sally

TEST YOUR SKILLS: DATABASE SECURITY


*Copyright Maven Analytics, LLC
INTRODUCING THE FINAL COURSE PROJECT

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.

THE Use SQL to:


OBJECTIVE • Create a plan for handling chat support, including the database infrastructure, EER
diagrams explaining your plan, and reports to help management understand performance
• Provide support for questions relating to the potential acquisition, to help your CEO keep
them interested and hopefully close the deal

*Copyright Maven Analytics, LLC


INTRODUCING THE FINAL PROJECT

NEW MESSAGE
May 1, 2014

THE
From: Sally Bleu (CEO) YOUR OBJECTIVES:
LETTER
Subject: Exciting Developments!
• Update the database with the most recent data

Good morning! • Create a plan for the company’s service


expansion to include chat support
Two big updates for you…

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

2. We were approached by a larger company who is


interested in acquiring us. Will need some help here too.

Let’s do this!
-Sally

*Copyright Maven Analytics, LLC


FINAL COURSE PROJECT QUESTIONS

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

*Copyright Maven Analytics, LLC


FINAL COURSE PROJECT QUESTIONS

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

*Copyright Maven Analytics, LLC

You might also like