Today’s Agenda

 Topics:  Introduction  Using your own MySQL database  HeidiSQL  phpMyAdmin  What is SQL?  The SELECT Statement CompSci 280 S2 2107  Reading: Introduction to Development  How to use HeidiSQL

MySQL  How to use phpMyAdmin  MySQL:  https://dev.mysql.com/doc/refman/5.7/en/

2 Lecture14

1.Introduction MySQL Prodcuts Overview What is MySQL

 A relational database management system  A server providing mulit-user access to a number of databases.  A database is a structured collection of data. To add, access, and process data stored in a computer database, you need a database management system (MySQL server).  The SQL part of MySQL stands for “Structured Query Language”.  The MySQL database Server is very fast, reliable, and easy to use.  MySQL Server today offers a rich and useful set of functions. It connectivity speed, and security make MySQL Server highly suited for accessing databases on the Internet.

3 Lecture14 4 Lecture14 1.Introduction 2.Using your own database What is MySQL MySql Server:

 MySQL software is Open Source.  You should have received an e mail before this lecture giving you  Open Source means that it is possible for anyone to use and modify the the access data to your own database on the server software. Anybody can download the MySQL software and use it without studdb .fos.auckland.ac.nz paying anything.  Server: studdb-mysql-fos.auckland.ac.nz  MySQL server works in client/server.  Database: stu_YOURUPI_COMPSCI_280_C_S2_2017 (Replace  The MySQL Database Software is a client/server system that consists of a YOURUPI with your own UPI) multi-threaded SQL server that supports server different client programs  Enter your User Name with your UPI and administrative tools.  Enter Password.  We provide MySQL server that you can link into your assignment.  Use LinqPad/HeidiSQL/phpMyAdmin to connect to the database file and execute SQL statements.

5 Lecture14 6 Lecture14

2.Using your own database 2.Using your own database phpMyAdmin HeidiSQL

 PhpMyAdmin is one of the most popular applications for MySQL  HeidiSQL is a useful and reliable tool designed for web developers using the popular MySQL server databases management. It is a free tool written in PHP. Through this  Download & install: http://www.heidisql.com/download.php software you can create, alter, drop, delete, import and export  Click New MySQL database tables.  Name the connection. Press the ENTER key.  Network type: MySQL (TCP/IP).  Web access: https://www.sit.auckland.ac.nz/student_database/  Hostname / IP: studdb-mysql.fos.auckland.ac.nz  Enter your username and password (** from the email)  User: enter your User Name with your UPI  In phpMyAdmin, select your database:  Enter Password.  Database: stu_YOURUPI_COMPSCI_280_C_S2_2017 (Replace YOURUPI with your stu_YOURUPI_COMPSCI_280_C_S2_2017 own UPI)  Choose “SQL” Tab on the right panel.  Click Open  Enter a SQL statement  Choose “Query” Tab on the right panel.  Enter a SQL statement  Click the “Go” button to execute the SQL statement. (Note: the table  Click the “Execute SQL” button name is case-sensitive in MySql.)

7 Lecture14 8 Lecture14 3.SQL 3.What is SQL? Data Types

 SQL stands for Structured Query Language  Data type selection is usually dictated by nature of data and by  A Computer Language designed to get information from a intended use Relational Database  Pay close attention to expected use of attributes for sorting and  SQL is a Declarative Computer Language data retrieval purposes  Focus is on the RESULT  Characters: CHAR(20), VARCHAR(50), TEXT  char/varchar: up to 255 characters  SQL functions fit into two broad categories:  char: Holds a fixed length string  Data definition language, commands to  carchar: Holds a variable length string  Create database objects, such as tables, indexes, and views  text: 65,535 characters  Define access rights to those database objects  Numbers: INT, BIGINT, SMALLINT, TINYINT, FLOAT, DOUBLE, DECIMAL  Data manipulation language  Others: DATE, DATETIME  Includes commands to insert, update, delete, and retrieve data within database tables

9 Lecture14 10 Lecture14

3.SQL 3.SQL Create your own table Data Manipulation Commands

 The create table statement creates a new table.  We will look at the following in this semester only:  When it is first created, this table will not have any rows of data in it. The  Adding table rows command has the following format:  Saving table changes CREATE TABLE table_name(  Listing table rows column_name1 data_type_1, column_name2 data_type_2,  Other commands: . . . );  Updating table rows  Restoring table contents  It consists of:  Deleting table rows  A table name CREATE TABLE DEPARTMENTS  Inserting table rows with a select subquery  (DEPT_CODE VARCHAR(3) not null primary key, Name of the columns DEPARTMENT_NAME VARCHAR(30));  Datatypes of the columns  A sequence to the columns

11 Lecture14 12 Lecture14 3.SQL 4.The Select Statement Adding one new row to a table Overview of the SELECT Statement

 Method 1:  SELECT – which columns of data to get INSERT INTO table_name  FROM – which table has the data VALUES ( ...);  WHERE – which rows of data to get  Specifies a value for each column of the table  GROUP BY –  Values MUST be in the same order as the columns of the table  HAVING –  Method 2:  ORDER BY – which columns are used to sort the results INSERT INTO table_name (column_name1, column_name2) VALUES ( ...);

 Put values in only some of the columns of the table SELECT EMPLOYEE_ID, LAST_NAME, CREDIT_LIMIT FROM EMPLOYEES  These columns are listed after the name of the table WHERE CREDIT_LIMIT > 20.00 ORDER BY LAST_NAME; INSERT INTO DEPARTMENTS VALUES ('ACT', 'ACCOUNTING');

13 Lecture14 14 Lecture14

4.The Select Statement 4.The Select Statement SELECT Clause SELECT Clause to List Some Columns SELECT EMPLOYEE_ID, LAST_NAME FROM EMPLOYEES;  SELECT a list of columns  Order of the columns within the Select clause determines their  Get only the columns listed order within the result table  Put them in the order they are listed  Order can be different than order in original table   SELECT * SELECT * FROM EMPLOYEES; Column can be renamed by giving it a column alias   Get ALL of the columns of the table A Literal Value can be included – if text or date it should be within single quote marks.  Put the columns in same order they are in the table  SELECT DISTINCT a list of columns SELECT EMPLOYEE_ID AS NAME, LAST_NAME,CREDIT_LIMIT, SELECT DISTINCT Manager_Id 'EXCELLENT WORKER' As EVALUATION  Get only the columns listed FROM EMPLOYEES; FROM EMPLOYEES;  Put them in the order they are listed manager_id null  Note:  You can rename them 201  SQL Aliases (“as”) are used to give a database table, or a column in a  Eliminate duplicate rows from the result 202 203 table, a temporary name.

15 Lecture14 16 Lecture14 202 Jim Kern 4.The Select Statement 4.The Select Statement 203 Martha Woods WHERE Clause WHERE Clause 207 Dan Smith 208 Fred Campbell  Specifies a condition that is True for all the rows you want in the  SET INCUSION Test – list of specific values 209 Paula Jacobs 210 Nancy Hoffman result table  IN (In a Set) select employee_id, first_name,last_name  NOT IN (Not In a Set) from EMPLOYEES  EQUAL and other Comparison Conditions where manager_id in (201, 203);  RANGE Test – between two values  Equal (=) select employee_id, first_name, last_name, manager_id  BETWEEN (in a range) select employee_id, first_name, last_name  Less than (<) from EMPLOYEES where manager_id = 203;  NOT BETWEEN (not within a range) from EMPLOYEES where employee_id between 203 and 205;  Less than or equal (<=)  NULL Test  employee_id first_name last_name Greater than (>)  IS NULL (is a null value) 203 Martha Woods  Greater than or equal (>=)  IS NOT NULL (is not a null value) 204 Ellen Owens  Not Equal (<>)(and others) 205 Henry Perkins employee_id first_name last_name manager_id

207 Dan Smith 203 select last_name last_name 208 Fred Campbell 203 from EMPLOYEES Brown 210 Nancy Hoffman 203 where manager_id is null; Rose

17 Lecture14 18 Lecture14

4.The Select Statement 4.The Select Statement last_name WHERE Clause Brown WHERE Clause select last_name Kern from EMPLOYEES  PATTERN MATCHING Test – where last_name like '%n%' Owens  BOOLEAN CONNECTORS – Joining simple conditions together Perkins  LIKE (matches a pattern) Hoffman  AND (both of the conditions are True)  NOT LIKE (does not match a pattern)  OR (one or both of the conditions is True)  “_” to match any single character  NOT (the condition is False) employee_id  “%” to match an arbitrary number of characters (including zero characters) 207 select employee_id from EMPLOYEES where first_name = 'DAN' and last_name = 'SMITH'; LIKE Operator Description

WHERE CustomerName LIKE 'a%' Finds any values that starts with "a"

WHERE CustomerName LIKE '%a' Finds any values that ends with "a"

WHERE CustomerName LIKE '%or%' Finds any values that have "or" in any position

WHERE CustomerName LIKE '_r%' Finds any values that have "r" in the second position

WHERE CustomerName LIKE 'a_%_%' Finds any values that starts with "a" and are at least 3 characters in length

WHERE ContactName LIKE 'a%o' Finds any values that starts with "a" and ends with "o"

19 Lecture14 20 Lecture14 4.The Select Statement 4.The Select Statement ORDER BY Clause Groups of Rows

 Syntax –  Dividing a Table into Groups of Rows and Summarizing Each  ORDER BY a list of column names Group  ORDER BY a list of numbers  The GROUP BY clause  Sort Order Options for each column  Each group is formed from  All of the Rows in the Table With the Same value(s) in specified Columns  ASC means ascending order (default) employee_id first_name last_name  Summary of groups of data within a column  DESC means descending order 201 Susan Brown 204 Ellen Owens  Groups formed on two or more columns  Example 203 Martha Woods  Nulls in groups when there are two or more grouping columns  ORDER BY last_name, first_name  Summarized data cannot be mixed with non-summarized data in the select employee_id, first_name, last_name same SELECT statement manager_id from EMPLOYEES where last_name in ('brown', 'woods', 'owens') null order by last_name; select manager_id 201 from EMPLOYEES group by manager_id 202 203

21 Lecture14 22 Lecture14

4.The Select Statement 4.The Select Statement Groups of data Eliminating Summarized Data

 Summary of groups of data within a column  The HAVING clause  Columns are summarized separately for each group  The HAVING clause contrasted with the WHERE clause  Result Table contains one row for each group along with the summarized  The whole process of the SELECT statement on a single table data for that group  The HAVING clause does not add any more power to the SELECT  All Rows with NULL values in the grouping column are placed within a statement single group called the NULL GROUP  Use a WHERE clause instead of a HAVING clause to eliminate raw data

select count(*), manager_id Column1 manager_id  When the Result Table contains data that are grouped and from EMPLOYEES 2 null group by manager_id 3 201 summarized order by manager_id; 2 202  The Having Clause can eliminate groups from the result table 3 203  The groups are still formed  All the calculations and summarizations are done  They are deleted at the end of the process

23 Lecture14 24 Lecture14 4.The Select Statement supplier_id product_code total_servings 4.The Select Statement Asp Sp 11 Example Cbc Gs 10 Exercise 2 : FOODS table (with where only) Frv Ff 10  Consider the LUNCH_ITEMS Jbr As 11  List the sum of price_increase of each product code for each Jbr Vr 17  There are 71 rows supplier  List the supplier_id and product_code of all the foods for which 10  filter by the price_increase greater than or equal to 0.5 servings or more have been ordered (i.e. most popular)  sorted by descending increase.

select supplier_id, product_code, sum(quantity) as total_servings from LUNCH_ITEMS supplier_id product_code increase group by supplier_id, product_code Asp Sw 0.4 having sum(quantity) >= 10 Asp Fs 0.25 order by supplier_id, product_code; Cbc Gs 0.7 Cbc Sw 0.3 Frv Br 0.05  Steps: Jbr As 0.25  Rows are grouped and processed Jbr Vr 0.15 supplier_id product_code increase Cbc Gs 0.7  Having clause eliminates rows from the result table Vsb As 0.5 Vsb As 0.5

25 Lecture14 26 Lecture14

4.The Select Statement 4.The Select Statement Exercise 3 (LUNCH_ITEMS) Having Vs Where

 List the sum of quantity of each product code for each supplier  The Where Clause can only eliminate rows from the beginning  Ignored those with quantity less than and equal to 1 table, the raw data, before any other processing occurs  Filter by the sum of quantity greater than 4  The Having Clause can only eliminate data that has been

SUPPLIER_ PRODUCT_ QUANTITY  sorted by descending sum ID CODE grouped and summarized, after most of the processing has CBC SW 2 LUNCH_ID ITEM_NUM SUPPLIER_ PRODUCT_ QUANTITY CBC SW 2 BER ID CODE SUPPLIER PRODUCT QUANTIT … _ID _CODE Y already taken place 42CBCSW2 93CBCSW2 CBC SW 4 16 3 CBC SW 1 JBR VR 12  The Where Clause cannot use column functions in the conditions 17 2 CBC GS 1 ASP SP 4 15 2 CBC GS 1 ASP SW 6 22 2 CBC GS 1 JBR AS 4 it sets 20 3 CBC GS 1 32CBCGS1 21 2 CBC GS 1  The Having Clause can use column functions in its conditions 12 2 CBC GS 1 82CBCGS1 62CBCGS1 73CBCGS1 …… supplier_id product_code increase JBR VR 12 ASP SW 6

27 Lecture14 28 Lecture14 4.The Select Statement Exercises  To find names beginning with “c”:

 To find names ending with “la”:

 To find names containing a “r”:

 To find names containing exactly five characters, use five instances of the “_” pattern character:

29 Lecture14