82
© Stephan Sorger 2016; www.StephanSorger.com ; Data Science: SQL: MySQL 1 Disclaimer: All images such as logos, photos, etc. used in this presentation are the property of their respective copyright owners and are used here for educational purposes only Some material adapted from: Sorger, Stephan. “Marketing Analytics: Strategic Models and Metrics. Admiral Press. 2013. Introduction to Data Science and Analytics Stephan Sorger www.StephanSorger.com Unit 6B. SQL: Overview and Coding SQL Coding Session in MySQL

Data Science and Analytics - Stephan SorgerFor GoDaddy, launch hosting account to see cPanel (shown). Click on MySQL Database Wizard . MySQL Server: Setup ... from tables using comma

  • Upload
    others

  • View
    4

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Data Science and Analytics - Stephan SorgerFor GoDaddy, launch hosting account to see cPanel (shown). Click on MySQL Database Wizard . MySQL Server: Setup ... from tables using comma

© Stephan Sorger 2016; www.StephanSorger.com; Data Science: SQL: MySQL 1

Disclaimer:

• All images such as logos, photos, etc. used in this presentation are the property of their respective copyright owners and are used

here for educational purposes only

• Some material adapted from: Sorger, Stephan. “Marketing Analytics: Strategic Models and Metrics. Admiral Press. 2013.

Introduction to

Data Science and Analytics

Stephan Sorger www.StephanSorger.com

Unit 6B. SQL: Overview and Coding SQL Coding Session in MySQL

Page 2: Data Science and Analytics - Stephan SorgerFor GoDaddy, launch hosting account to see cPanel (shown). Click on MySQL Database Wizard . MySQL Server: Setup ... from tables using comma

Outline/ Learning Objectives

© Stephan Sorger 2016; www.StephanSorger.com; Data Science: SQL: MySQL 1

Category Description

Hosting Obtaining hosting server for MySQL (free or paid)

Setup Setting up the MySQL Server on hosting platforms

Import Importing databases into MySQL Server

Structure Data structure; Showing tables within databases

SHOW, SELECT Commands to display data and choose data

Sorting, Filtering Commands to sort and filter data

Creating Creating, Dropping, and Altering databases and tables

Page 3: Data Science and Analytics - Stephan SorgerFor GoDaddy, launch hosting account to see cPanel (shown). Click on MySQL Database Wizard . MySQL Server: Setup ... from tables using comma

MySQL: Hosting

© Stephan Sorger 2016; www.StephanSorger.com; Data Science: SQL: MySQL 1

Go to

000webhost.com

(or other similar

Web hosting service)

and get an account

Page 4: Data Science and Analytics - Stephan SorgerFor GoDaddy, launch hosting account to see cPanel (shown). Click on MySQL Database Wizard . MySQL Server: Setup ... from tables using comma

MySQL Server: Setup

© Stephan Sorger 2016; www.StephanSorger.com; Data Science: SQL: MySQL 1

Creating MySQL

Databases for

hosting accounts

on GoDaddy.com

Page 5: Data Science and Analytics - Stephan SorgerFor GoDaddy, launch hosting account to see cPanel (shown). Click on MySQL Database Wizard . MySQL Server: Setup ... from tables using comma

© Stephan Sorger 2016; www.StephanSorger.com; Data Science: SQL: MySQL 1

MySQL Server: Setup

For GoDaddy,

launch hosting

account to see

cPanel (shown).

Click on MySQL

Database Wizard

Page 6: Data Science and Analytics - Stephan SorgerFor GoDaddy, launch hosting account to see cPanel (shown). Click on MySQL Database Wizard . MySQL Server: Setup ... from tables using comma

MySQL Server: Setup

© Stephan Sorger 2016; www.StephanSorger.com; Data Science: SQL: MySQL 1

Enter name for

database

Page 7: Data Science and Analytics - Stephan SorgerFor GoDaddy, launch hosting account to see cPanel (shown). Click on MySQL Database Wizard . MySQL Server: Setup ... from tables using comma

MySQL Server: Setup

© Stephan Sorger 2016; www.StephanSorger.com; Data Science: SQL: MySQL 1

Enter username

and password

Page 8: Data Science and Analytics - Stephan SorgerFor GoDaddy, launch hosting account to see cPanel (shown). Click on MySQL Database Wizard . MySQL Server: Setup ... from tables using comma

MySQL Server: Setup

© Stephan Sorger 2016; www.StephanSorger.com; Data Science: SQL: MySQL 1

Select

privileges

for user

Page 9: Data Science and Analytics - Stephan SorgerFor GoDaddy, launch hosting account to see cPanel (shown). Click on MySQL Database Wizard . MySQL Server: Setup ... from tables using comma

MySQL Server: Setup

© Stephan Sorger 2016; www.StephanSorger.com; Data Science: SQL: MySQL 1

Select

phpMyAdmin

Page 10: Data Science and Analytics - Stephan SorgerFor GoDaddy, launch hosting account to see cPanel (shown). Click on MySQL Database Wizard . MySQL Server: Setup ... from tables using comma

MySQL: Importing Databases

© Stephan Sorger 2016; www.StephanSorger.com; Data Science: SQL: MySQL 1

Select your Database

(Database1)

Page 11: Data Science and Analytics - Stephan SorgerFor GoDaddy, launch hosting account to see cPanel (shown). Click on MySQL Database Wizard . MySQL Server: Setup ... from tables using comma

MySQL: Importing Databases

© Stephan Sorger 2016; www.StephanSorger.com; Data Science: SQL: MySQL 1

Select Import

Page 12: Data Science and Analytics - Stephan SorgerFor GoDaddy, launch hosting account to see cPanel (shown). Click on MySQL Database Wizard . MySQL Server: Setup ... from tables using comma

MySQL: Importing Databases

© Stephan Sorger 2016; www.StephanSorger.com; Data Science: SQL: MySQL 1

MySQL Sample Databases:

dev.mysql.com/doc/index-other.html

For our example: world database

Page 13: Data Science and Analytics - Stephan SorgerFor GoDaddy, launch hosting account to see cPanel (shown). Click on MySQL Database Wizard . MySQL Server: Setup ... from tables using comma

MySQL: Importing Databases

© Stephan Sorger 2016; www.StephanSorger.com; Data Science: SQL: MySQL 1

Choose File:

Dataset:

world.sql

Click “Go”

(bottom of page)

Page 14: Data Science and Analytics - Stephan SorgerFor GoDaddy, launch hosting account to see cPanel (shown). Click on MySQL Database Wizard . MySQL Server: Setup ... from tables using comma

MySQL: Importing Databases

© Stephan Sorger 2016; www.StephanSorger.com; Data Science: SQL: MySQL 1

Imported OK;

Click “Database1”

Page 15: Data Science and Analytics - Stephan SorgerFor GoDaddy, launch hosting account to see cPanel (shown). Click on MySQL Database Wizard . MySQL Server: Setup ... from tables using comma

MySQL: Data Structure

© Stephan Sorger 2016; www.StephanSorger.com; Data Science: SQL: MySQL 1

Check “City” Table

within World database

Click on SQL tab

Page 16: Data Science and Analytics - Stephan SorgerFor GoDaddy, launch hosting account to see cPanel (shown). Click on MySQL Database Wizard . MySQL Server: Setup ... from tables using comma

MySQL: Data Structure

© Stephan Sorger 2016; www.StephanSorger.com; Data Science: SQL: MySQL 1

In SQL tab, enter

“SHOW DATABASES”

and hit “Go”

SQL commands

also called “queries”

are in ALL CAPS

Table entries are in lower case or Mixed Case

Page 17: Data Science and Analytics - Stephan SorgerFor GoDaddy, launch hosting account to see cPanel (shown). Click on MySQL Database Wizard . MySQL Server: Setup ... from tables using comma

MySQL: Data Structure

© Stephan Sorger 2016; www.StephanSorger.com; Data Science: SQL: MySQL 1

Shows databases:

information_schema

(already in SQL server)

Database1

(we created)

Page 18: Data Science and Analytics - Stephan SorgerFor GoDaddy, launch hosting account to see cPanel (shown). Click on MySQL Database Wizard . MySQL Server: Setup ... from tables using comma

MySQL: Data Structure

© Stephan Sorger 2016; www.StephanSorger.com; Data Science: SQL: MySQL 1

In SQL tab, enter

“SHOW TABLES”

and hit “Go”

Page 19: Data Science and Analytics - Stephan SorgerFor GoDaddy, launch hosting account to see cPanel (shown). Click on MySQL Database Wizard . MySQL Server: Setup ... from tables using comma

MySQL: Data Structure

© Stephan Sorger 2016; www.StephanSorger.com; Data Science: SQL: MySQL 1

Tables:

City

Country

Country Language

Page 20: Data Science and Analytics - Stephan SorgerFor GoDaddy, launch hosting account to see cPanel (shown). Click on MySQL Database Wizard . MySQL Server: Setup ... from tables using comma

MySQL: SHOW Command

© Stephan Sorger 2016; www.StephanSorger.com; Data Science: SQL: MySQL 1

View columns of table

Click on SQL tab

Syntax:

SHOW COLUMNS FROM (table)

Here:

SHOW COLUMNS FROM City

Then click Go

Page 21: Data Science and Analytics - Stephan SorgerFor GoDaddy, launch hosting account to see cPanel (shown). Click on MySQL Database Wizard . MySQL Server: Setup ... from tables using comma

MySQL: SHOW Command

© Stephan Sorger 2016; www.StephanSorger.com; Data Science: SQL: MySQL 1

Results:

See columns from table

Note PRI (primary) key

Here, PRI = ID field

Page 22: Data Science and Analytics - Stephan SorgerFor GoDaddy, launch hosting account to see cPanel (shown). Click on MySQL Database Wizard . MySQL Server: Setup ... from tables using comma

MySQL: SHOW Command

© Stephan Sorger 2016; www.StephanSorger.com; Data Science: SQL: MySQL 1

Click on file structure list

in left column to see table

Here, click on “City”

Page 23: Data Science and Analytics - Stephan SorgerFor GoDaddy, launch hosting account to see cPanel (shown). Click on MySQL Database Wizard . MySQL Server: Setup ... from tables using comma

MySQL: SELECT Command

© Stephan Sorger 2016; www.StephanSorger.com; Data Science: SQL: MySQL 1

SELECT Name FROM City

and click Go

Note column names on right

Note commands on bottom

Page 24: Data Science and Analytics - Stephan SorgerFor GoDaddy, launch hosting account to see cPanel (shown). Click on MySQL Database Wizard . MySQL Server: Setup ... from tables using comma

MySQL: SELECT Command

© Stephan Sorger 2016; www.StephanSorger.com; Data Science: SQL: MySQL 1

Result:

Names of cities

Page 25: Data Science and Analytics - Stephan SorgerFor GoDaddy, launch hosting account to see cPanel (shown). Click on MySQL Database Wizard . MySQL Server: Setup ... from tables using comma

MySQL: SELECT Command

© Stephan Sorger 2016; www.StephanSorger.com; Data Science: SQL: MySQL 1

Select multiple columns

from tables using comma (,)

between column labels

Note semicolon at end of line

(most compilers will run command without semicolon if command is only 1 line)

SELECT Name, CountryCode FROM City;

Page 26: Data Science and Analytics - Stephan SorgerFor GoDaddy, launch hosting account to see cPanel (shown). Click on MySQL Database Wizard . MySQL Server: Setup ... from tables using comma

MySQL: SELECT Command

© Stephan Sorger 2016; www.StephanSorger.com; Data Science: SQL: MySQL 1

Result:

Names of cities

AND

Names of Country Codes

Page 27: Data Science and Analytics - Stephan SorgerFor GoDaddy, launch hosting account to see cPanel (shown). Click on MySQL Database Wizard . MySQL Server: Setup ... from tables using comma

MySQL: SELECT Command

© Stephan Sorger 2016; www.StephanSorger.com; Data Science: SQL: MySQL 1

Insert wildcard

character (*) to

retrieve all information

SELECT * FROM City

Page 28: Data Science and Analytics - Stephan SorgerFor GoDaddy, launch hosting account to see cPanel (shown). Click on MySQL Database Wizard . MySQL Server: Setup ... from tables using comma

MySQL: SELECT Command

© Stephan Sorger 2016; www.StephanSorger.com; Data Science: SQL: MySQL 1

Result:

Entire dataset

Page 29: Data Science and Analytics - Stephan SorgerFor GoDaddy, launch hosting account to see cPanel (shown). Click on MySQL Database Wizard . MySQL Server: Setup ... from tables using comma

MySQL: SELECT with DISTINCT and LIMIT

© Stephan Sorger 2016; www.StephanSorger.com; Data Science: SQL: MySQL 1

DISTINCT:

To show only one

instance of a particular

value

SELECT DISTINCT CountryCode FROM City

Page 30: Data Science and Analytics - Stephan SorgerFor GoDaddy, launch hosting account to see cPanel (shown). Click on MySQL Database Wizard . MySQL Server: Setup ... from tables using comma

MySQL: SELECT with DISTINCT and LIMIT

© Stephan Sorger 2016; www.StephanSorger.com; Data Science: SQL: MySQL 1

Result:

Distinct country codes

Page 31: Data Science and Analytics - Stephan SorgerFor GoDaddy, launch hosting account to see cPanel (shown). Click on MySQL Database Wizard . MySQL Server: Setup ... from tables using comma

MySQL: SELECT with DISTINCT and LIMIT

© Stephan Sorger 2016; www.StephanSorger.com; Data Science: SQL: MySQL 1

Show only first 5

using LIMIT command

SELECT Name, CountryCode FROM City LIMIT 5

Page 32: Data Science and Analytics - Stephan SorgerFor GoDaddy, launch hosting account to see cPanel (shown). Click on MySQL Database Wizard . MySQL Server: Setup ... from tables using comma

MySQL: SELECT with DISTINCT and LIMIT

© Stephan Sorger 2016; www.StephanSorger.com; Data Science: SQL: MySQL 1

Result: First 5 entries

Page 33: Data Science and Analytics - Stephan SorgerFor GoDaddy, launch hosting account to see cPanel (shown). Click on MySQL Database Wizard . MySQL Server: Setup ... from tables using comma

MySQL: SELECT with DISTINCT and LIMIT

© Stephan Sorger 2016; www.StephanSorger.com; Data Science: SQL: MySQL 1

Select only limited set of entries

From start point (say, 5)

To a certain number of data points (say, 10)

(Computers start counting at 0, not at 1,

so results will show “6”, not “5”

SELECT Name, CountryCode FROM City LIMIT 5, 10

Page 34: Data Science and Analytics - Stephan SorgerFor GoDaddy, launch hosting account to see cPanel (shown). Click on MySQL Database Wizard . MySQL Server: Setup ... from tables using comma

MySQL: SELECT with DISTINCT and LIMIT

© Stephan Sorger 2016; www.StephanSorger.com; Data Science: SQL: MySQL 1

Result: Results 5 - 15

Page 35: Data Science and Analytics - Stephan SorgerFor GoDaddy, launch hosting account to see cPanel (shown). Click on MySQL Database Wizard . MySQL Server: Setup ... from tables using comma

MySQL: SELECT with DISTINCT and LIMIT

© Stephan Sorger 2016; www.StephanSorger.com; Data Science: SQL: MySQL 1

Fully Qualified Names:

“dataset.label”

Handy if working on

multiple databases

with the same column

SELECT City.Name FROM City

Page 36: Data Science and Analytics - Stephan SorgerFor GoDaddy, launch hosting account to see cPanel (shown). Click on MySQL Database Wizard . MySQL Server: Setup ... from tables using comma

MySQL: SELECT with DISTINCT and LIMIT

© Stephan Sorger 2016; www.StephanSorger.com; Data Science: SQL: MySQL 1

Results: Same as before

Note names in order as

found in the original table;

Can be confusing.

Alternative: Sort.

Page 37: Data Science and Analytics - Stephan SorgerFor GoDaddy, launch hosting account to see cPanel (shown). Click on MySQL Database Wizard . MySQL Server: Setup ... from tables using comma

MySQL: Sorting

© Stephan Sorger 2016; www.StephanSorger.com; Data Science: SQL: MySQL 1

Sort results using

ORDER BY command

SELECT Name FROM City ORDER BY Name

Page 38: Data Science and Analytics - Stephan SorgerFor GoDaddy, launch hosting account to see cPanel (shown). Click on MySQL Database Wizard . MySQL Server: Setup ... from tables using comma

MySQL: Sorting

© Stephan Sorger 2016; www.StephanSorger.com; Data Science: SQL: MySQL 1

Results are now in

alphabetic order

Page 39: Data Science and Analytics - Stephan SorgerFor GoDaddy, launch hosting account to see cPanel (shown). Click on MySQL Database Wizard . MySQL Server: Setup ... from tables using comma

MySQL: Sorting

© Stephan Sorger 2016; www.StephanSorger.com; Data Science: SQL: MySQL 1

Sorting multiple columns,

specifying order parameter

SELECT Name, CountryCode, District FROM City ORDER BY Name

Page 40: Data Science and Analytics - Stephan SorgerFor GoDaddy, launch hosting account to see cPanel (shown). Click on MySQL Database Wizard . MySQL Server: Setup ... from tables using comma

MySQL: Sorting

© Stephan Sorger 2016; www.StephanSorger.com; Data Science: SQL: MySQL 1

Results:

Multiple columns,

sorted by Name

Page 41: Data Science and Analytics - Stephan SorgerFor GoDaddy, launch hosting account to see cPanel (shown). Click on MySQL Database Wizard . MySQL Server: Setup ... from tables using comma

MySQL: Sorting

© Stephan Sorger 2016; www.StephanSorger.com; Data Science: SQL: MySQL 1

Sequential Sort:

Sort by x, then by y

SELECT Name,CountryCode,District from City ORDER BY name,CountryCode

Page 42: Data Science and Analytics - Stephan SorgerFor GoDaddy, launch hosting account to see cPanel (shown). Click on MySQL Database Wizard . MySQL Server: Setup ... from tables using comma

MySQL: Sorting

© Stephan Sorger 2016; www.StephanSorger.com; Data Science: SQL: MySQL 1

Result:

Sort by Name,

then by Country Code

(not very interesting

in this case, because

data is unique for each area)

Page 43: Data Science and Analytics - Stephan SorgerFor GoDaddy, launch hosting account to see cPanel (shown). Click on MySQL Database Wizard . MySQL Server: Setup ... from tables using comma

MySQL: Sorting

© Stephan Sorger 2016; www.StephanSorger.com; Data Science: SQL: MySQL 1

Sorting in reverse

direction; from high to low

DESC = Descending

ASC = Ascending

SELECT Name, CountryCode FROM City ORDER BY Name DESC

Page 44: Data Science and Analytics - Stephan SorgerFor GoDaddy, launch hosting account to see cPanel (shown). Click on MySQL Database Wizard . MySQL Server: Setup ... from tables using comma

MySQL: Sorting

© Stephan Sorger 2016; www.StephanSorger.com; Data Science: SQL: MySQL 1

Result: Names from Z – A

BUT: Algorithm lists

special characters,

such as umlauted vowels,

AFTER Z

Page 45: Data Science and Analytics - Stephan SorgerFor GoDaddy, launch hosting account to see cPanel (shown). Click on MySQL Database Wizard . MySQL Server: Setup ... from tables using comma

MySQL: Sorting

© Stephan Sorger 2016; www.StephanSorger.com; Data Science: SQL: MySQL 1

Select highest value

(or lowest value)

SELECT Name, Population FROM City ORDER BY Population LIMIT 1

Page 46: Data Science and Analytics - Stephan SorgerFor GoDaddy, launch hosting account to see cPanel (shown). Click on MySQL Database Wizard . MySQL Server: Setup ... from tables using comma

MySQL: Sorting

© Stephan Sorger 2016; www.StephanSorger.com; Data Science: SQL: MySQL 1

Results:

Adamstown has the

smallest population,

with only 42 people

Page 47: Data Science and Analytics - Stephan SorgerFor GoDaddy, launch hosting account to see cPanel (shown). Click on MySQL Database Wizard . MySQL Server: Setup ... from tables using comma

MySQL: Sorting

© Stephan Sorger 2016; www.StephanSorger.com; Data Science: SQL: MySQL 1

Select highest value

(or lowest value)

SELECT Name, Population FROM City ORDER BY Population DESC LIMIT 1

Page 48: Data Science and Analytics - Stephan SorgerFor GoDaddy, launch hosting account to see cPanel (shown). Click on MySQL Database Wizard . MySQL Server: Setup ... from tables using comma

MySQL: Sorting

© Stephan Sorger 2016; www.StephanSorger.com; Data Science: SQL: MySQL 1

Results:

Mumbai has the

largest population,

with 10,500,000 people

Page 49: Data Science and Analytics - Stephan SorgerFor GoDaddy, launch hosting account to see cPanel (shown). Click on MySQL Database Wizard . MySQL Server: Setup ... from tables using comma

MySQL: Filtering

© Stephan Sorger 2016; www.StephanSorger.com; Data Science: SQL: MySQL 1

Filtering:

Want only areas

within country code ‘AFG’

SELECT Name, CountryCode FROM City WHERE CountryCode=‘AFG’

Page 50: Data Science and Analytics - Stephan SorgerFor GoDaddy, launch hosting account to see cPanel (shown). Click on MySQL Database Wizard . MySQL Server: Setup ... from tables using comma

MySQL: Filtering

© Stephan Sorger 2016; www.StephanSorger.com; Data Science: SQL: MySQL 1

Results

Page 51: Data Science and Analytics - Stephan SorgerFor GoDaddy, launch hosting account to see cPanel (shown). Click on MySQL Database Wizard . MySQL Server: Setup ... from tables using comma

MySQL: Filtering

© Stephan Sorger 2016; www.StephanSorger.com; Data Science: SQL: MySQL 1

Filtering:

Want to list

“small cities”,

i.e. areas

with population

under 10,000

SELECT Name, Population FROM City WHERE Population <= 10000

(could also show “large cities”) :

SELECT Name, Population FROM City WHERE Population >= 10000

Page 52: Data Science and Analytics - Stephan SorgerFor GoDaddy, launch hosting account to see cPanel (shown). Click on MySQL Database Wizard . MySQL Server: Setup ... from tables using comma

MySQL: Filtering

© Stephan Sorger 2016; www.StephanSorger.com; Data Science: SQL: MySQL 1

Result:

Listing of “small” cities

in ID order

(could also sort if

desired)

Page 53: Data Science and Analytics - Stephan SorgerFor GoDaddy, launch hosting account to see cPanel (shown). Click on MySQL Database Wizard . MySQL Server: Setup ... from tables using comma

MySQL: Filtering

© Stephan Sorger 2016; www.StephanSorger.com; Data Science: SQL: MySQL 1

Filtering:

Want to list

“medium cities”

i.e. areas

with population

between

10,000 and 50,000:

SELECT Name, Population FROM City WHERE Population BETWEEN 10000 AND 50000

OR

SELECT Name, Population FROM City WHERE Population >= 10000 && Population <= 50000

Page 54: Data Science and Analytics - Stephan SorgerFor GoDaddy, launch hosting account to see cPanel (shown). Click on MySQL Database Wizard . MySQL Server: Setup ... from tables using comma

MySQL: Filtering

© Stephan Sorger 2016; www.StephanSorger.com; Data Science: SQL: MySQL 1

Result:

“Sweet spot” cities

between 10,000 and

50,000

Page 55: Data Science and Analytics - Stephan SorgerFor GoDaddy, launch hosting account to see cPanel (shown). Click on MySQL Database Wizard . MySQL Server: Setup ... from tables using comma

MySQL: Logical Operators

© Stephan Sorger 2016; www.StephanSorger.com; Data Science: SQL: MySQL 1

Find name of city

within AFG with a

population greater

than 200,000

SELECT Name, CountryCode FROM City WHERE CountryCode=‘AFG’ AND Population>200000

Page 56: Data Science and Analytics - Stephan SorgerFor GoDaddy, launch hosting account to see cPanel (shown). Click on MySQL Database Wizard . MySQL Server: Setup ... from tables using comma

MySQL: Logical Operators

© Stephan Sorger 2016; www.StephanSorger.com; Data Science: SQL: MySQL 1

Result: Two cities

met both criteria

Page 57: Data Science and Analytics - Stephan SorgerFor GoDaddy, launch hosting account to see cPanel (shown). Click on MySQL Database Wizard . MySQL Server: Setup ... from tables using comma

MySQL: Logical Operators

© Stephan Sorger 2016; www.StephanSorger.com; Data Science: SQL: MySQL 1

Find name of city

within NLD

OR with a population

greater than 400,000

SELECT Name, CountryCode, Population

FROM City WHERE CountryCode=‘NLD’ AND Population>400000

Page 58: Data Science and Analytics - Stephan SorgerFor GoDaddy, launch hosting account to see cPanel (shown). Click on MySQL Database Wizard . MySQL Server: Setup ... from tables using comma

MySQL: Logical Operators

© Stephan Sorger 2016; www.StephanSorger.com; Data Science: SQL: MySQL 1

Result: Three cities

met at least one of the

criteria

Page 59: Data Science and Analytics - Stephan SorgerFor GoDaddy, launch hosting account to see cPanel (shown). Click on MySQL Database Wizard . MySQL Server: Setup ... from tables using comma

MySQL: Searching

© Stephan Sorger 2016; www.StephanSorger.com; Data Science: SQL: MySQL 1

Use REGEXP

(regular expression)

to search for item.

Here, we want to

search for any district

with the word “Holland”

in it.

SELECT Name, District FROM City WHERE District REGEXP 'Holland'

Page 60: Data Science and Analytics - Stephan SorgerFor GoDaddy, launch hosting account to see cPanel (shown). Click on MySQL Database Wizard . MySQL Server: Setup ... from tables using comma

MySQL: Searching

© Stephan Sorger 2016; www.StephanSorger.com; Data Science: SQL: MySQL 1

Results:

Many districts have

the word “Holland”

in them

Page 61: Data Science and Analytics - Stephan SorgerFor GoDaddy, launch hosting account to see cPanel (shown). Click on MySQL Database Wizard . MySQL Server: Setup ... from tables using comma

MySQL: Special Columns

© Stephan Sorger 2016; www.StephanSorger.com; Data Science: SQL: MySQL 1

Create special columns

using math: +, -, *, /

For example, what if

population were to grow by 10%?

SELECT Name, Population, Population*1.1 AS Population_Growth FROM City

Page 62: Data Science and Analytics - Stephan SorgerFor GoDaddy, launch hosting account to see cPanel (shown). Click on MySQL Database Wizard . MySQL Server: Setup ... from tables using comma

MySQL: Special Columns

© Stephan Sorger 2016; www.StephanSorger.com; Data Science: SQL: MySQL 1

Result:

Special Column temporarily

inserted in database:

Population Growth

Shows population + 10%

SELECT Name, Population, Population*1.1 AS Population_Growth FROM City

Page 63: Data Science and Analytics - Stephan SorgerFor GoDaddy, launch hosting account to see cPanel (shown). Click on MySQL Database Wizard . MySQL Server: Setup ... from tables using comma

MySQL: CREATE Database

© Stephan Sorger 2016; www.StephanSorger.com; Data Science: SQL: MySQL 1

Create new database

called “database2”

CREATE DATABASE database2

To delete database:

DROP DATABASE database2

Page 64: Data Science and Analytics - Stephan SorgerFor GoDaddy, launch hosting account to see cPanel (shown). Click on MySQL Database Wizard . MySQL Server: Setup ... from tables using comma

MySQL: CREATE Table

© Stephan Sorger 2016; www.StephanSorger.com; Data Science: SQL: MySQL 1

Create new table

through phpMyAdmin

By clicking on Database

Page 65: Data Science and Analytics - Stephan SorgerFor GoDaddy, launch hosting account to see cPanel (shown). Click on MySQL Database Wizard . MySQL Server: Setup ... from tables using comma

MySQL: CREATE Table

© Stephan Sorger 2016; www.StephanSorger.com; Data Science: SQL: MySQL 1

Within phpMyAdmin:

Create table:

Enter Name

Enter number of columns

Page 66: Data Science and Analytics - Stephan SorgerFor GoDaddy, launch hosting account to see cPanel (shown). Click on MySQL Database Wizard . MySQL Server: Setup ... from tables using comma

MySQL: CREATE Table

© Stephan Sorger 2016; www.StephanSorger.com; Data Science: SQL: MySQL 1

Within MySQL:

Create new table: CREATE

Drop or rename table:

DROP TABLE signup

RENAME TABLE signup TO users

Create new table

for website

where people can

sign up for emails

Page 67: Data Science and Analytics - Stephan SorgerFor GoDaddy, launch hosting account to see cPanel (shown). Click on MySQL Database Wizard . MySQL Server: Setup ... from tables using comma

MySQL: CREATE Table

© Stephan Sorger 2016; www.StephanSorger.com; Data Science: SQL: MySQL 1

CREATE TABLE signup ( Open bracket; Create new lines

id int NOT NULL AUTO_INCREMENT First variable: id; data type: Integer; Automatically add 1

name varchar (30) NOT NULL Second variable: name; alphanumeric text; Required

email varchar (30) NOT NULL Third variable: email; max length: 30 char. ; Required

PRIMARY KEY (id) Declare primary key; in this case, “id”

) Close bracket

Page 68: Data Science and Analytics - Stephan SorgerFor GoDaddy, launch hosting account to see cPanel (shown). Click on MySQL Database Wizard . MySQL Server: Setup ... from tables using comma

MySQL: CREATE Table

© Stephan Sorger 2016; www.StephanSorger.com; Data Science: SQL: MySQL 1

Click on Structure

to see table structure

id underlined

PRIMARY KEY

Page 69: Data Science and Analytics - Stephan SorgerFor GoDaddy, launch hosting account to see cPanel (shown). Click on MySQL Database Wizard . MySQL Server: Setup ... from tables using comma

MySQL: CREATE Table

© Stephan Sorger 2016; www.StephanSorger.com; Data Science: SQL: MySQL 1

Manually enter data

by clicking Insert tab,

then click Go

Page 70: Data Science and Analytics - Stephan SorgerFor GoDaddy, launch hosting account to see cPanel (shown). Click on MySQL Database Wizard . MySQL Server: Setup ... from tables using comma

MySQL: Alter Table

© Stephan Sorger 2016; www.StephanSorger.com; Data Science: SQL: MySQL 1

Results:

-Data inserted

-ID incremented

To Delete a Row:

DELETE FROM signup WHERE name = ‘Kathy Kar’

Page 71: Data Science and Analytics - Stephan SorgerFor GoDaddy, launch hosting account to see cPanel (shown). Click on MySQL Database Wizard . MySQL Server: Setup ... from tables using comma

MySQL: Alter Table

© Stephan Sorger 2016; www.StephanSorger.com; Data Science: SQL: MySQL 1

Add column:

phone (phone number)

To add a column:

ALTER TABLE signup ADD phone varchar(10) NOT NULL

To delete a column:

ALTER TABLE signup DROP COLUMN phone

Page 72: Data Science and Analytics - Stephan SorgerFor GoDaddy, launch hosting account to see cPanel (shown). Click on MySQL Database Wizard . MySQL Server: Setup ... from tables using comma

MySQL: Alter Table

© Stephan Sorger 2016; www.StephanSorger.com; Data Science: SQL: MySQL 1

Results:

New column inserted:

“phone”

Page 73: Data Science and Analytics - Stephan SorgerFor GoDaddy, launch hosting account to see cPanel (shown). Click on MySQL Database Wizard . MySQL Server: Setup ... from tables using comma

MySQL: Reference Manuals

© Stephan Sorger 2016; www.StephanSorger.com; Data Science: SQL: MySQL 1

dev.mysql.com/doc/index.html

Page 74: Data Science and Analytics - Stephan SorgerFor GoDaddy, launch hosting account to see cPanel (shown). Click on MySQL Database Wizard . MySQL Server: Setup ... from tables using comma

MySQL:

© Stephan Sorger 2016; www.StephanSorger.com; Data Science: SQL: MySQL 1

Page 75: Data Science and Analytics - Stephan SorgerFor GoDaddy, launch hosting account to see cPanel (shown). Click on MySQL Database Wizard . MySQL Server: Setup ... from tables using comma

MySQL:

© Stephan Sorger 2016; www.StephanSorger.com; Data Science: SQL: MySQL 1

Page 76: Data Science and Analytics - Stephan SorgerFor GoDaddy, launch hosting account to see cPanel (shown). Click on MySQL Database Wizard . MySQL Server: Setup ... from tables using comma

MySQL:

© Stephan Sorger 2016; www.StephanSorger.com; Data Science: SQL: MySQL 1

Page 77: Data Science and Analytics - Stephan SorgerFor GoDaddy, launch hosting account to see cPanel (shown). Click on MySQL Database Wizard . MySQL Server: Setup ... from tables using comma

Outline/ Learning Objectives

© Stephan Sorger 2016; www.StephanSorger.com; Data Science: SQL: MySQL 1

Page 78: Data Science and Analytics - Stephan SorgerFor GoDaddy, launch hosting account to see cPanel (shown). Click on MySQL Database Wizard . MySQL Server: Setup ... from tables using comma

Outline/ Learning Objectives

© Stephan Sorger 2016; www.StephanSorger.com; Data Science: SQL: MySQL 1

Page 79: Data Science and Analytics - Stephan SorgerFor GoDaddy, launch hosting account to see cPanel (shown). Click on MySQL Database Wizard . MySQL Server: Setup ... from tables using comma

Outline/ Learning Objectives

© Stephan Sorger 2016; www.StephanSorger.com; Data Science: SQL: MySQL 1

Page 80: Data Science and Analytics - Stephan SorgerFor GoDaddy, launch hosting account to see cPanel (shown). Click on MySQL Database Wizard . MySQL Server: Setup ... from tables using comma

Outline/ Learning Objectives

© Stephan Sorger 2016; www.StephanSorger.com; Data Science: SQL: MySQL 1

Page 81: Data Science and Analytics - Stephan SorgerFor GoDaddy, launch hosting account to see cPanel (shown). Click on MySQL Database Wizard . MySQL Server: Setup ... from tables using comma

Outline/ Learning Objectives

© Stephan Sorger 2016; www.StephanSorger.com; Data Science: SQL: MySQL 1

Page 82: Data Science and Analytics - Stephan SorgerFor GoDaddy, launch hosting account to see cPanel (shown). Click on MySQL Database Wizard . MySQL Server: Setup ... from tables using comma

Outline/ Learning Objectives

© Stephan Sorger 2016; www.StephanSorger.com; Data Science: SQL: MySQL 1