Download presentation
Presentation is loading. Please wait.
Published byBarnaby Atkinson Modified over 8 years ago
1
Marketing Analytics: Database Query with MySQL Disclaimer: All logos, photos, etc. used in this presentation are the property of their respective copyright owners and are used here for educational purposes only © Stephan Sorger 2015. www.StephanSorger.com; Database Query with MySQL; SQL.1www.StephanSorger.com
2
CategoryDescription IntroductionOverview of database query languages RDBMSExample of Relational Database Management System HostingObtaining hosting server for MySQL (free or paid) SetupSetting up the MySQL Server on hosting platforms ImportImporting databases into MySQL Server StructureData structure; Showing tables within databases SHOWCommands to display data SELECTCommands to choose certain pieces of data SortingCommands to sort data FilteringCommands to filter data LogicalUsing logical commands (AND, OR) in commands CreatingCreating, Dropping, and Altering databases and tables Outline © Stephan Sorger 2015. www.StephanSorger.com; Database Query with MySQL; SQL.2www.StephanSorger.com
3
CategoryDescription SQLStructured Query Language Programming language for accessing RDBMS RDBMSRelational Database Management System SQLConsists of two items: Data Definition Language: Syntax for defining data structures Data Manipulation Language: Syntax for selecting, inserting deleting, and updating data in databases Top 3Top 3 Database engines: Oracle MySQL Microsoft SQL Server MySQLOpen source; Free; Available for Mac and PC Manual: See dev.mysql.com/doc/index.html Database Query: Introduction © Stephan Sorger 2015. www.StephanSorger.com; Database Query with MySQL; SQL.3www.StephanSorger.com
4
RDBMS Database Example Row: Record Primary Key: Column with 100% unique entries similar to Social Security Number (‘Engine’ column will not work; duplicate entries) © Stephan Sorger 2015. www.StephanSorger.com; Database Query with MySQL; SQL.4www.StephanSorger.com
5
MySQL: Hosting Go to 000webhost.com (or other similar Web hosting service) and get an account © Stephan Sorger 2015. www.StephanSorger.com; Database Query with MySQL; SQL.5www.StephanSorger.com
6
MySQL Server: Setup Creating MySQL Databases for hosting accounts on GoDaddy.com © Stephan Sorger 2015. www.StephanSorger.com; Database Query with MySQL; SQL.6www.StephanSorger.com
7
For GoDaddy, launch hosting account to see cPanel (shown). Click on MySQL Database Wizard MySQL: Server Setup © Stephan Sorger 2015. www.StephanSorger.com; Database Query with MySQL; SQL.7www.StephanSorger.com
8
MySQL Server: Setup Enter name for database © Stephan Sorger 2015. www.StephanSorger.com; Database Query with MySQL; SQL.8www.StephanSorger.com
9
MySQL Server: Setup Enter username and password © Stephan Sorger 2015. www.StephanSorger.com; Database Query with MySQL; SQL.9www.StephanSorger.com
10
MySQL Server: Setup Select privileges for user © Stephan Sorger 2015. www.StephanSorger.com; Database Query with MySQL; SQL.10www.StephanSorger.com
11
MySQL Server: Setup Select phpMyAdmin © Stephan Sorger 2015. www.StephanSorger.com; Database Query with MySQL; SQL.11www.StephanSorger.com
12
MySQL: Importing Databases Select your Database (Database1) © Stephan Sorger 2015. www.StephanSorger.com; Database Query with MySQL; SQL.12www.StephanSorger.com
13
MySQL: Importing Databases Select Import © Stephan Sorger 2015. www.StephanSorger.com; Database Query with MySQL; SQL.13www.StephanSorger.com
14
MySQL: Importing Databases MySQL Sample Databases: dev.mysql.com/doc/index-other.html © Stephan Sorger 2015. www.StephanSorger.com; Database Query with MySQL; SQL.14www.StephanSorger.com
15
MySQL: Importing Databases Choose File: Dataset: world.sql Click “Go” (bottom of page) © Stephan Sorger 2015. www.StephanSorger.com; Database Query with MySQL; SQL.15www.StephanSorger.com
16
MySQL: Importing Databases Imported OK; Click “Database1” © Stephan Sorger 2015. www.StephanSorger.com; Database Query with MySQL; SQL.16www.StephanSorger.com
17
MySQL: Data Structure Check “City” Table within World database Click on SQL tab © Stephan Sorger 2015. www.StephanSorger.com; Database Query with MySQL; SQL.17www.StephanSorger.com
18
MySQL: Data Structure 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 © Stephan Sorger 2015. www.StephanSorger.com; Database Query with MySQL; SQL.18www.StephanSorger.com
19
MySQL: Data Structure Shows databases: information_schema (already in SQL server) Database1 (we created) © Stephan Sorger 2015. www.StephanSorger.com; Database Query with MySQL; SQL.19www.StephanSorger.com
20
MySQL: Data Structure In SQL tab, enter “SHOW TABLES” and hit “Go” © Stephan Sorger 2015. www.StephanSorger.com; Database Query with MySQL; SQL.20www.StephanSorger.com
21
MySQL: Data Structure Tables: City Country Country Language © Stephan Sorger 2015. www.StephanSorger.com; Database Query with MySQL; SQL.21www.StephanSorger.com
22
MySQL: SHOW Commands View columns of table Click on SQL tab Syntax: SHOW COLUMNS FROM (table) Here: SHOW COLUMNS FROM City Then click Go © Stephan Sorger 2015. www.StephanSorger.com; Database Query with MySQL; SQL.22www.StephanSorger.com
23
MySQL: SHOW Commands Results: See columns from table Note PRI (primary) key Here, PRI = ID field © Stephan Sorger 2015. www.StephanSorger.com; Database Query with MySQL; SQL.23www.StephanSorger.com
24
MySQL: SHOW Commands Click on file structure list in left column to see table Here, click on “City” © Stephan Sorger 2015. www.StephanSorger.com; Database Query with MySQL; SQL.24www.StephanSorger.com
25
MySQL: SELECT Commands SELECT Name FROM City and click Go Note column names on right Note commands on bottom © Stephan Sorger 2015. www.StephanSorger.com; Database Query with MySQL; SQL.25www.StephanSorger.com
26
MySQL: SELECT Commands Result: Names of cities © Stephan Sorger 2015. www.StephanSorger.com; Database Query with MySQL; SQL.26www.StephanSorger.com
27
MySQL: SELECT Commands 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; © Stephan Sorger 2015. www.StephanSorger.com; Database Query with MySQL; SQL.27www.StephanSorger.com
28
MySQL: SELECT Commands Result: Names of cities AND Names of Country Codes © Stephan Sorger 2015. www.StephanSorger.com; Database Query with MySQL; SQL.28www.StephanSorger.com
29
MySQL: SELECT Commands Insert wildcard character (*) to retrieve all information SELECT * FROM City © Stephan Sorger 2015. www.StephanSorger.com; Database Query with MySQL; SQL.29www.StephanSorger.com
30
MySQL: SELECT Commands Result: Entire dataset © Stephan Sorger 2015. www.StephanSorger.com; Database Query with MySQL; SQL.30www.StephanSorger.com
31
MySQL: SELECT with Distinct and LIMIT DISTINCT: To show only one instance of a particular value SELECT DISTINCT CountryCode FROM City © Stephan Sorger 2015. www.StephanSorger.com; Database Query with MySQL; SQL.31www.StephanSorger.com
32
Result: Distinct country codes MySQL: SELECT with Distinct and LIMIT © Stephan Sorger 2015. www.StephanSorger.com; Database Query with MySQL; SQL.32www.StephanSorger.com
33
Show only first 5 using LIMIT command SELECT Name, CountryCode FROM City LIMIT 5 MySQL: SELECT with Distinct and LIMIT © Stephan Sorger 2015. www.StephanSorger.com; Database Query with MySQL; SQL.33www.StephanSorger.com
34
Result: First 5 entries MySQL: SELECT with Distinct and LIMIT © Stephan Sorger 2015. www.StephanSorger.com; Database Query with MySQL; SQL.34www.StephanSorger.com
35
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 MySQL: SELECT with Distinct and LIMIT © Stephan Sorger 2015. www.StephanSorger.com; Database Query with MySQL; SQL.35www.StephanSorger.com
36
Result: Results 5 - 15 MySQL: SELECT with Distinct and LIMIT © Stephan Sorger 2015. www.StephanSorger.com; Database Query with MySQL; SQL.36www.StephanSorger.com
37
Fully Qualified Names: “dataset.label” Handy if working on multiple databases with the same column SELECT City.Name FROM City MySQL: SELECT with Distinct and LIMIT © Stephan Sorger 2015. www.StephanSorger.com; Database Query with MySQL; SQL.37www.StephanSorger.com
38
Results: Same as before Note names in order as found in the original table; Can be confusing. Alternative: Sort. MySQL: SELECT with Distinct and LIMIT © Stephan Sorger 2015. www.StephanSorger.com; Database Query with MySQL; SQL.38www.StephanSorger.com
39
MySQL: Sorting Sort results using ORDER BY command SELECT Name FROM City ORDER BY Name © Stephan Sorger 2015. www.StephanSorger.com; Database Query with MySQL; SQL.39www.StephanSorger.com
40
MySQL: Sorting Results are now in alphabetic order © Stephan Sorger 2015. www.StephanSorger.com; Database Query with MySQL; SQL.40www.StephanSorger.com
41
MySQL: Sorting Sorting multiple columns, specifying order parameter SELECT Name, CountryCode, District FROM City ORDER BY Name © Stephan Sorger 2015. www.StephanSorger.com; Database Query with MySQL; SQL.41www.StephanSorger.com
42
MySQL: Sorting Results: Multiple columns, sorted by Name © Stephan Sorger 2015. www.StephanSorger.com; Database Query with MySQL; SQL.42www.StephanSorger.com
43
MySQL: Sorting Sequential Sort: Sort by x, then by y SELECT Name,CountryCode,District from City ORDER BY name,CountryCode © Stephan Sorger 2015. www.StephanSorger.com; Database Query with MySQL; SQL.43www.StephanSorger.com
44
MySQL: Sorting Result: Sort by Name, then by Country Code (not very interesting in this case, because data is unique for each area) © Stephan Sorger 2015. www.StephanSorger.com; Database Query with MySQL; SQL.44www.StephanSorger.com
45
MySQL: Sorting Sorting in reverse direction; from high to low DESC = Descending ASC = Ascending SELECT Name, CountryCode FROM City ORDER BY Name DESC © Stephan Sorger 2015. www.StephanSorger.com; Database Query with MySQL; SQL.45www.StephanSorger.com
46
MySQL: Sorting Result: Names from Z – A BUT: Algorithm lists special characters, such as umlauted vowels, AFTER Z © Stephan Sorger 2015. www.StephanSorger.com; Database Query with MySQL; SQL.46www.StephanSorger.com
47
MySQL: Sorting Select highest value (or lowest value) SELECT Name, Population FROM City ORDER BY Population LIMIT 1 © Stephan Sorger 2015. www.StephanSorger.com; Database Query with MySQL; SQL.47www.StephanSorger.com
48
MySQL: Sorting Results: Adamstown has the smallest population, with only 42 people © Stephan Sorger 2015. www.StephanSorger.com; Database Query with MySQL; SQL.48www.StephanSorger.com
49
MySQL: Sorting Select highest value (or lowest value) SELECT Name, Population FROM City ORDER BY Population DESC LIMIT 1 © Stephan Sorger 2015. www.StephanSorger.com; Database Query with MySQL; SQL.49www.StephanSorger.com
50
MySQL: Sorting Results: Mubai has the largest population, with 10,500,000 people © Stephan Sorger 2015. www.StephanSorger.com; Database Query with MySQL; SQL.50www.StephanSorger.com
51
MySQL: Filtering Filtering: Want only areas within country code ‘AFG’ SELECT Name, CountryCode FROM City WHERE CountryCode=‘AFG’ © Stephan Sorger 2015. www.StephanSorger.com; Database Query with MySQL; SQL.51www.StephanSorger.com
52
MySQL: Filtering Results © Stephan Sorger 2015. www.StephanSorger.com; Database Query with MySQL; SQL.52www.StephanSorger.com
53
MySQL: Filtering 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 © Stephan Sorger 2015. www.StephanSorger.com; Database Query with MySQL; SQL.53www.StephanSorger.com
54
MySQL: Filtering Result: Listing of “small” cities in ID order (could also sort if desired) © Stephan Sorger 2015. www.StephanSorger.com; Database Query with MySQL; SQL.54www.StephanSorger.com
55
MySQL: Filtering 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 © Stephan Sorger 2015. www.StephanSorger.com; Database Query with MySQL; SQL.55www.StephanSorger.com
56
MySQL: Filtering Result: “Sweet spot” cities between 10,000 and 50,000 © Stephan Sorger 2015. www.StephanSorger.com; Database Query with MySQL; SQL.56www.StephanSorger.com
57
MySQL: Logical Operators Find name of city within AFG with a population greater than 200,000 SELECT Name, CountryCode FROM City WHERE CountryCode=‘AFG’ AND Population>200000 © Stephan Sorger 2015. www.StephanSorger.com; Database Query with MySQL; SQL.57www.StephanSorger.com
58
MySQL: Logical Operators Result: Two cities met both criteria © Stephan Sorger 2015. www.StephanSorger.com; Database Query with MySQL; SQL.58www.StephanSorger.com
59
MySQL: Logical Operators 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 © Stephan Sorger 2015. www.StephanSorger.com; Database Query with MySQL; SQL.59www.StephanSorger.com
60
MySQL Server Result: Three cities met at least one of the criteria © Stephan Sorger 2015. www.StephanSorger.com; Database Query with MySQL; SQL.60www.StephanSorger.com
61
MySQL: Searching 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 ' © Stephan Sorger 2015. www.StephanSorger.com; Database Query with MySQL; SQL.61www.StephanSorger.com
62
MySQL: Searching Results: Many districts have the word “Holland” in them © Stephan Sorger 2015. www.StephanSorger.com; Database Query with MySQL; SQL.62www.StephanSorger.com
63
MySQL: Special Columns 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 © Stephan Sorger 2015. www.StephanSorger.com; Database Query with MySQL; SQL.63www.StephanSorger.com
64
MySQL: Special Columns Result: Special Column temporarily inserted in database: Population Growth Shows population + 10% SELECT Name, Population, Population*1.1 AS Population_Growth FROM City © Stephan Sorger 2015. www.StephanSorger.com; Database Query with MySQL; SQL.64www.StephanSorger.com
65
MySQL: Create Database Create new database called “database2” CREATE DATABASE database2 To delete database: DROP DATABASE database2 © Stephan Sorger 2015. www.StephanSorger.com; Database Query with MySQL; SQL.65www.StephanSorger.com
66
MySQL: Create Table Create new table through phpMyAdmin By clicking on Database © Stephan Sorger 2015. www.StephanSorger.com; Database Query with MySQL; SQL.66www.StephanSorger.com
67
MySQL: Create Table Within phpMyAdmin: Create table: Enter Name Enter number of columns © Stephan Sorger 2015. www.StephanSorger.com; Database Query with MySQL; SQL.67www.StephanSorger.com
68
MySQL: Create Table 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 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 © Stephan Sorger 2015. www.StephanSorger.com; Database Query with MySQL; SQL.68www.StephanSorger.com
69
MySQL: Create Table Click on Structure to see table structure id underlined PRIMARY KEY © Stephan Sorger 2015. www.StephanSorger.com; Database Query with MySQL; SQL.69www.StephanSorger.com
70
MySQL: Create Table Manually enter data by clicking Insert tab, then click Go © Stephan Sorger 2015. www.StephanSorger.com; Database Query with MySQL; SQL.70www.StephanSorger.com
71
MySQL Server Results: -Data inserted -ID incremented To Delete a Row: DELETE FROM signup WHERE name = ‘Kathy Kar’ © Stephan Sorger 2015. www.StephanSorger.com; Database Query with MySQL; SQL.71www.StephanSorger.com
72
MySQL: Alter Table 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 © Stephan Sorger 2015. www.StephanSorger.com; Database Query with MySQL; SQL.72www.StephanSorger.com
73
MySQL Server Results: New column inserted: “phone” © Stephan Sorger 2015. www.StephanSorger.com; Database Query with MySQL; SQL.73www.StephanSorger.com
74
MySQL: Reference Manuals To learn all commands, functions, etc. available, go to: dev.mysql.com/doc/index.html © Stephan Sorger 2015. www.StephanSorger.com; Database Query with MySQL; SQL.74www.StephanSorger.com
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.