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 Database Query with MySQL; SQL.1www.StephanSorger.com
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 Database Query with MySQL; SQL.2www.StephanSorger.com
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 Database Query with MySQL; SQL.3www.StephanSorger.com
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 Database Query with MySQL; SQL.4www.StephanSorger.com
MySQL: Hosting Go to 000webhost.com (or other similar Web hosting service) and get an account © Stephan Sorger Database Query with MySQL; SQL.5www.StephanSorger.com
MySQL Server: Setup Creating MySQL Databases for hosting accounts on GoDaddy.com © Stephan Sorger Database Query with MySQL; SQL.6www.StephanSorger.com
For GoDaddy, launch hosting account to see cPanel (shown). Click on MySQL Database Wizard MySQL: Server Setup © Stephan Sorger Database Query with MySQL; SQL.7www.StephanSorger.com
MySQL Server: Setup Enter name for database © Stephan Sorger Database Query with MySQL; SQL.8www.StephanSorger.com
MySQL Server: Setup Enter username and password © Stephan Sorger Database Query with MySQL; SQL.9www.StephanSorger.com
MySQL Server: Setup Select privileges for user © Stephan Sorger Database Query with MySQL; SQL.10www.StephanSorger.com
MySQL Server: Setup Select phpMyAdmin © Stephan Sorger Database Query with MySQL; SQL.11www.StephanSorger.com
MySQL: Importing Databases Select your Database (Database1) © Stephan Sorger Database Query with MySQL; SQL.12www.StephanSorger.com
MySQL: Importing Databases Select Import © Stephan Sorger Database Query with MySQL; SQL.13www.StephanSorger.com
MySQL: Importing Databases MySQL Sample Databases: dev.mysql.com/doc/index-other.html © Stephan Sorger Database Query with MySQL; SQL.14www.StephanSorger.com
MySQL: Importing Databases Choose File: Dataset: world.sql Click “Go” (bottom of page) © Stephan Sorger Database Query with MySQL; SQL.15www.StephanSorger.com
MySQL: Importing Databases Imported OK; Click “Database1” © Stephan Sorger Database Query with MySQL; SQL.16www.StephanSorger.com
MySQL: Data Structure Check “City” Table within World database Click on SQL tab © Stephan Sorger Database Query with MySQL; SQL.17www.StephanSorger.com
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 Database Query with MySQL; SQL.18www.StephanSorger.com
MySQL: Data Structure Shows databases: information_schema (already in SQL server) Database1 (we created) © Stephan Sorger Database Query with MySQL; SQL.19www.StephanSorger.com
MySQL: Data Structure In SQL tab, enter “SHOW TABLES” and hit “Go” © Stephan Sorger Database Query with MySQL; SQL.20www.StephanSorger.com
MySQL: Data Structure Tables: City Country Country Language © Stephan Sorger Database Query with MySQL; SQL.21www.StephanSorger.com
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 Database Query with MySQL; SQL.22www.StephanSorger.com
MySQL: SHOW Commands Results: See columns from table Note PRI (primary) key Here, PRI = ID field © Stephan Sorger Database Query with MySQL; SQL.23www.StephanSorger.com
MySQL: SHOW Commands Click on file structure list in left column to see table Here, click on “City” © Stephan Sorger Database Query with MySQL; SQL.24www.StephanSorger.com
MySQL: SELECT Commands SELECT Name FROM City and click Go Note column names on right Note commands on bottom © Stephan Sorger Database Query with MySQL; SQL.25www.StephanSorger.com
MySQL: SELECT Commands Result: Names of cities © Stephan Sorger Database Query with MySQL; SQL.26www.StephanSorger.com
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 Database Query with MySQL; SQL.27www.StephanSorger.com
MySQL: SELECT Commands Result: Names of cities AND Names of Country Codes © Stephan Sorger Database Query with MySQL; SQL.28www.StephanSorger.com
MySQL: SELECT Commands Insert wildcard character (*) to retrieve all information SELECT * FROM City © Stephan Sorger Database Query with MySQL; SQL.29www.StephanSorger.com
MySQL: SELECT Commands Result: Entire dataset © Stephan Sorger Database Query with MySQL; SQL.30www.StephanSorger.com
MySQL: SELECT with Distinct and LIMIT DISTINCT: To show only one instance of a particular value SELECT DISTINCT CountryCode FROM City © Stephan Sorger Database Query with MySQL; SQL.31www.StephanSorger.com
Result: Distinct country codes MySQL: SELECT with Distinct and LIMIT © Stephan Sorger Database Query with MySQL; SQL.32www.StephanSorger.com
Show only first 5 using LIMIT command SELECT Name, CountryCode FROM City LIMIT 5 MySQL: SELECT with Distinct and LIMIT © Stephan Sorger Database Query with MySQL; SQL.33www.StephanSorger.com
Result: First 5 entries MySQL: SELECT with Distinct and LIMIT © Stephan Sorger Database Query with MySQL; SQL.34www.StephanSorger.com
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 Database Query with MySQL; SQL.35www.StephanSorger.com
Result: Results MySQL: SELECT with Distinct and LIMIT © Stephan Sorger Database Query with MySQL; SQL.36www.StephanSorger.com
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 Database Query with MySQL; SQL.37www.StephanSorger.com
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 Database Query with MySQL; SQL.38www.StephanSorger.com
MySQL: Sorting Sort results using ORDER BY command SELECT Name FROM City ORDER BY Name © Stephan Sorger Database Query with MySQL; SQL.39www.StephanSorger.com
MySQL: Sorting Results are now in alphabetic order © Stephan Sorger Database Query with MySQL; SQL.40www.StephanSorger.com
MySQL: Sorting Sorting multiple columns, specifying order parameter SELECT Name, CountryCode, District FROM City ORDER BY Name © Stephan Sorger Database Query with MySQL; SQL.41www.StephanSorger.com
MySQL: Sorting Results: Multiple columns, sorted by Name © Stephan Sorger Database Query with MySQL; SQL.42www.StephanSorger.com
MySQL: Sorting Sequential Sort: Sort by x, then by y SELECT Name,CountryCode,District from City ORDER BY name,CountryCode © Stephan Sorger Database Query with MySQL; SQL.43www.StephanSorger.com
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 Database Query with MySQL; SQL.44www.StephanSorger.com
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 Database Query with MySQL; SQL.45www.StephanSorger.com
MySQL: Sorting Result: Names from Z – A BUT: Algorithm lists special characters, such as umlauted vowels, AFTER Z © Stephan Sorger Database Query with MySQL; SQL.46www.StephanSorger.com
MySQL: Sorting Select highest value (or lowest value) SELECT Name, Population FROM City ORDER BY Population LIMIT 1 © Stephan Sorger Database Query with MySQL; SQL.47www.StephanSorger.com
MySQL: Sorting Results: Adamstown has the smallest population, with only 42 people © Stephan Sorger Database Query with MySQL; SQL.48www.StephanSorger.com
MySQL: Sorting Select highest value (or lowest value) SELECT Name, Population FROM City ORDER BY Population DESC LIMIT 1 © Stephan Sorger Database Query with MySQL; SQL.49www.StephanSorger.com
MySQL: Sorting Results: Mubai has the largest population, with 10,500,000 people © Stephan Sorger Database Query with MySQL; SQL.50www.StephanSorger.com
MySQL: Filtering Filtering: Want only areas within country code ‘AFG’ SELECT Name, CountryCode FROM City WHERE CountryCode=‘AFG’ © Stephan Sorger Database Query with MySQL; SQL.51www.StephanSorger.com
MySQL: Filtering Results © Stephan Sorger Database Query with MySQL; SQL.52www.StephanSorger.com
MySQL: Filtering Filtering: Want to list “small cities”, i.e. areas with population under 10,000 SELECT Name, Population FROM City WHERE Population <= (could also show “large cities”) : SELECT Name, Population FROM City WHERE Population >= © Stephan Sorger Database Query with MySQL; SQL.53www.StephanSorger.com
MySQL: Filtering Result: Listing of “small” cities in ID order (could also sort if desired) © Stephan Sorger Database Query with MySQL; SQL.54www.StephanSorger.com
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 AND OR SELECT Name, Population FROM City WHERE Population >= && Population <= © Stephan Sorger Database Query with MySQL; SQL.55www.StephanSorger.com
MySQL: Filtering Result: “Sweet spot” cities between 10,000 and 50,000 © Stephan Sorger Database Query with MySQL; SQL.56www.StephanSorger.com
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> © Stephan Sorger Database Query with MySQL; SQL.57www.StephanSorger.com
MySQL: Logical Operators Result: Two cities met both criteria © Stephan Sorger Database Query with MySQL; SQL.58www.StephanSorger.com
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> © Stephan Sorger Database Query with MySQL; SQL.59www.StephanSorger.com
MySQL Server Result: Three cities met at least one of the criteria © Stephan Sorger Database Query with MySQL; SQL.60www.StephanSorger.com
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 Database Query with MySQL; SQL.61www.StephanSorger.com
MySQL: Searching Results: Many districts have the word “Holland” in them © Stephan Sorger Database Query with MySQL; SQL.62www.StephanSorger.com
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 Database Query with MySQL; SQL.63www.StephanSorger.com
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 Database Query with MySQL; SQL.64www.StephanSorger.com
MySQL: Create Database Create new database called “database2” CREATE DATABASE database2 To delete database: DROP DATABASE database2 © Stephan Sorger Database Query with MySQL; SQL.65www.StephanSorger.com
MySQL: Create Table Create new table through phpMyAdmin By clicking on Database © Stephan Sorger Database Query with MySQL; SQL.66www.StephanSorger.com
MySQL: Create Table Within phpMyAdmin: Create table: Enter Name Enter number of columns © Stephan Sorger Database Query with MySQL; SQL.67www.StephanSorger.com
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 s 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 varchar (30) NOT NULL Third variable: ; max length: 30 char. ; Required PRIMARY KEY (id) Declare primary key; in this case, “id” ) Close bracket © Stephan Sorger Database Query with MySQL; SQL.68www.StephanSorger.com
MySQL: Create Table Click on Structure to see table structure id underlined PRIMARY KEY © Stephan Sorger Database Query with MySQL; SQL.69www.StephanSorger.com
MySQL: Create Table Manually enter data by clicking Insert tab, then click Go © Stephan Sorger Database Query with MySQL; SQL.70www.StephanSorger.com
MySQL Server Results: -Data inserted -ID incremented To Delete a Row: DELETE FROM signup WHERE name = ‘Kathy Kar’ © Stephan Sorger Database Query with MySQL; SQL.71www.StephanSorger.com
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 Database Query with MySQL; SQL.72www.StephanSorger.com
MySQL Server Results: New column inserted: “phone” © Stephan Sorger Database Query with MySQL; SQL.73www.StephanSorger.com
MySQL: Reference Manuals To learn all commands, functions, etc. available, go to: dev.mysql.com/doc/index.html © Stephan Sorger Database Query with MySQL; SQL.74www.StephanSorger.com