CompSci 280 S Introduction to Software Development

Slides:



Advertisements
Similar presentations
CC SQL Utilities.
Advertisements

Virtual training week 4 structured query language (SQL)
Database Systems: Design, Implementation, and Management Tenth Edition
Introduction to Structured Query Language (SQL)
Introduction to Structured Query Language (SQL)
Introduction to Structured Query Language (SQL)
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 7 Introduction to Structured Query Language (SQL)
Microsoft Access 2010 Chapter 7 Using SQL.
A Guide to SQL, Eighth Edition Chapter Three Creating Tables.
Session 5: Working with MySQL iNET Academy Open Source Web Development.
Chapter 5 Introduction to SQL. Structured Query Language = the “programming language” for relational databases SQL is a nonprocedural language = the user.
Introduction to SQL Yong Choi School of Business CSU, Bakersfield.
ASP.NET Programming with C# and SQL Server First Edition
PHP Programming with MySQL Slide 8-1 CHAPTER 8 Working with Databases and MySQL.
AL-MAAREFA COLLEGE FOR SCIENCE AND TECHNOLOGY INFO 232: DATABASE SYSTEMS CHAPTER 7 INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL) Instructor Ms. Arwa.
 SQL stands for Structured Query Language.  SQL lets you access and manipulate databases.  SQL is an ANSI (American National Standards Institute) standard.
Chapter 7 Working with Databases and MySQL PHP Programming with MySQL 2 nd Edition.
Chapter 7 Working with Databases and MySQL PHP Programming with MySQL 2 nd Edition.
CHAPTER:14 Simple Queries in SQL Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्सजेंड़र ) PGT(CS),KV JHAGRAKHAND.
Introduction to MySQL Lab no. 10 Advance Database Management System.
Web Scripting [PHP] CIS166AE Wednesdays 6:00pm – 9:50pm Rob Loy.
CSC 2720 Building Web Applications Database and SQL.
CPS120: Introduction to Computer Science Lecture 19 Introduction to SQL.
Structure Query Language SQL. Database Terminology Employee ID 3 3 Last name Small First name Tony 5 5 Smith James
SQL SQL Server : Overview SQL : Overview Types of SQL Database : Creation Tables : Creation & Manipulation Data : Creation & Manipulation Data : Retrieving.
Concepts of Database Management Seventh Edition
1 Structured Query Language (SQL). 2 Contents SQL – I SQL – II SQL – III SQL – IV.
7 1 Chapter 7 Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
Chapter 5 MYSQL Database. Introduction to MYSQL MySQL is the world's most popular open-source database. Open source means that the source code, the programming.
6 1 Lecture 8: Introduction to Structured Query Language (SQL) J. S. Chou, P.E., Ph.D.
Topic 1: Introduction to SQL. SQL stands for Structured Query Language. SQL is a standard computer language for accessing and manipulating databases SQL.
SQL – Part I Yong Choi School of Business CSU, Bakersfield.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
1 DBS201: Introduction to Structure Query Language (SQL) Lecture 1.
Database Fundamental & Design by A.Surasit Samaisut Copyrights : All Rights Reserved.
Copyright © 2004, Oracle. All rights reserved. Lecture 4: 1-Retrieving Data Using the SQL SELECT Statement 2-Restricting and Sorting Data Lecture 4: 1-Retrieving.
Using SQL Connecting, Retrieving Data, Executing SQL Commands, … Svetlin Nakov Technical Trainer Software University
ITEC 3220A Using and Designing Database Systems Instructor: Prof. Z. Yang Course Website: 3220a.htm
>> Introduction to MySQL. Introduction Structured Query Language (SQL) – Standard Database Language – Manage Data in a DBMS (Database Management System)
Database: SQL, MySQL, LINQ and Java DB © by Pearson Education, Inc. All Rights Reserved.
Distribution of Marks For Second Semester Internal Sessional Evaluation External Evaluation Assignment /Project QuizzesClass Attendance Mid-Term Test Total.
Simple Queries DBS301 – Week 1. Objectives Basic SELECT statement Computed columns Aliases Concatenation operator Use of DISTINCT to eliminate duplicates.
LM 5 Introduction to SQL MISM 4135 Instructor: Dr. Lei Li.
1 Copyright © 2007, Oracle. All rights reserved. Retrieving Data Using the SQL SELECT Statement.
1 Copyright © 2009, Oracle. All rights reserved. Retrieving Data Using the SQL SELECT Statement.
COM621: Advanced Interactive Web Development Lecture 11 MySQL – Data Manipulation Language.
 MySQL is a database system used on the web  MySQL is a database system that runs on a server  MySQL is ideal for both small and large applications.
3 A Guide to MySQL.
Fundamentals of DBMS Notes-1.
Web Systems & Technologies
CHAPTER 7 DATABASE ACCESS THROUGH WEB
Chapter 5 Introduction to SQL.
Presented by: Teererai Marange
Relational Database Design
Writing Basic SQL SELECT Statements
ATS Application Programming: Java Programming
Introduction to Web programming
ISC440: Web Programming 2 Server-side Scripting PHP 3
Chapter 7 Working with Databases and MySQL
Chapter 8 Working with Databases and MySQL
Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall
Chapter 7 Introduction to Structured Query Language (SQL)
Yong Choi School of Business CSU, Bakersfield
Introduction To Structured Query Language (SQL)
CS3220 Web and Internet Programming SQL and MySQL
PHP and MySQL.
MySQL Database System Installation Overview SQL summary
MySQL Database System Installation Overview SQL summary
Shelly Cashman: Microsoft Access 2016
Presentation transcript:

CompSci 280 S2 2107 Introduction to Software Development MySQL

Today’s Agenda Topics: Reading: Introduction Using your own MySQL database HeidiSQL phpMyAdmin What is SQL? The SELECT Statement Reading: How to use HeidiSQL How to use phpMyAdmin MySQL: https://dev.mysql.com/doc/refman/5.7/en/ Lecture14

MySQL Prodcuts Overview Lecture14

1.Introduction 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. Lecture14

1.Introduction What is MySQL MySQL software is Open Source. Open Source means that it is possible for anyone to use and modify the software. Anybody can download the MySQL software and use it without paying anything. MySQL server works in client/server. The MySQL Database Software is a client/server system that consists of a multi-threaded SQL server that supports server different client programs and administrative tools. We provide MySQL server that you can link into your assignment. Lecture14

2.Using your own database MySql Server: You should have received an e‐mail before this lecture giving you the access data to your own database on the server studdb‐mysql.fos.auckland.ac.nz Server: studdb-mysql-fos.auckland.ac.nz Database: stu_YOURUPI_COMPSCI_280_C_S2_2017 (Replace YOURUPI with your own UPI) Enter your User Name with your UPI Enter Password. Use LinqPad/HeidiSQL/phpMyAdmin to connect to the database file and execute SQL statements. Lecture14

2.Using your own database phpMyAdmin PhpMyAdmin is one of the most popular applications for MySQL databases management. It is a free tool written in PHP. Through this software you can create, alter, drop, delete, import and export MySQL database tables. Web access: https://www.sit.auckland.ac.nz/student_database/ Enter your username and password (** from the email) In phpMyAdmin, select your database: stu_YOURUPI_COMPSCI_280_C_S2_2017 Choose “SQL” Tab on the right panel. Enter a SQL statement Click the “Go” button to execute the SQL statement. (Note: the table name is case-sensitive in MySql.) Lecture14

2.Using your own database HeidiSQL HeidiSQL is a useful and reliable tool designed for web developers using the popular MySQL server Download & install: http://www.heidisql.com/download.php Click New Name the connection. Press the ENTER key. Network type: MySQL (TCP/IP). Hostname / IP: studdb-mysql.fos.auckland.ac.nz User: enter your User Name with your UPI Enter Password. Database: stu_YOURUPI_COMPSCI_280_C_S2_2017 (Replace YOURUPI with your own UPI) Click Open Choose “Query” Tab on the right panel. Enter a SQL statement Click the “Execute SQL” button Lecture14

3.What is SQL? SQL stands for Structured Query Language 4/21/2018 3.What is SQL? SQL stands for Structured Query Language A Computer Language designed to get information from a Relational Database SQL is a Declarative Computer Language Focus is on the RESULT SQL functions fit into two broad categories: Data definition language, commands to Create database objects, such as tables, indexes, and views Define access rights to those database objects Data manipulation language Includes commands to insert, update, delete, and retrieve data within database tables Lecture14 CIS4523-SQL PPT prepared by Alma Bane

3.SQL Data Types Data type selection is usually dictated by nature of data and by intended use Pay close attention to expected use of attributes for sorting and data retrieval purposes Characters: CHAR(20), VARCHAR(50), TEXT char/varchar: up to 255 characters char: Holds a fixed length string carchar: Holds a variable length string text: 65,535 characters Numbers: INT, BIGINT, SMALLINT, TINYINT, FLOAT, DOUBLE, DECIMAL Others: DATE, DATETIME Lecture14

3.SQL Create your own table The create table statement creates a new table. When it is first created, this table will not have any rows of data in it. The command has the following format: It consists of: A table name Name of the columns Datatypes of the columns A sequence to the columns CREATE TABLE table_name( column_name1 data_type_1, column_name2 data_type_2, . . . ); CREATE TABLE DEPARTMENTS (DEPT_CODE VARCHAR(3) not null primary key, DEPARTMENT_NAME VARCHAR(30)); Lecture14

3.SQL Data Manipulation Commands We will look at the following in this semester only: Adding table rows Saving table changes Listing table rows Other commands: Updating table rows Restoring table contents Deleting table rows Inserting table rows with a select subquery Lecture14

3.SQL Adding one new row to a table Method 1: Specifies a value for each column of the table Values MUST be in the same order as the columns of the table Method 2: Put values in only some of the columns of the table These columns are listed after the name of the table INSERT INTO table_name VALUES ( ...); INSERT INTO table_name (column_name1, column_name2) VALUES ( ...); INSERT INTO DEPARTMENTS VALUES ('ACT', 'ACCOUNTING'); Lecture14

4.The Select Statement Overview of the SELECT Statement 4/21/2018 4.The Select Statement Overview of the SELECT Statement SELECT – which columns of data to get FROM – which table has the data WHERE – which rows of data to get GROUP BY – HAVING – ORDER BY – which columns are used to sort the results SELECT EMPLOYEE_ID, LAST_NAME, CREDIT_LIMIT FROM EMPLOYEES WHERE CREDIT_LIMIT > 20.00 ORDER BY LAST_NAME; Lecture14 CIS 4523 - SQL prepared by Alma Bane

4.The Select Statement SELECT Clause 4/21/2018 4.The Select Statement SELECT Clause SELECT EMPLOYEE_ID, LAST_NAME FROM EMPLOYEES; SELECT a list of columns Get only the columns listed Put them in the order they are listed SELECT * Get ALL of the columns of the table Put the columns in same order they are in the table SELECT DISTINCT a list of columns You can rename them Eliminate duplicate rows from the result SELECT * FROM EMPLOYEES; SELECT DISTINCT Manager_Id FROM EMPLOYEES; manager_id null 201 202 203 Lecture14 CIS 4523 - SQL prepared by Alma Bane

4.The Select Statement SELECT Clause to List Some Columns 4/21/2018 4.The Select Statement SELECT Clause to List Some Columns Order of the columns within the Select clause determines their order within the result table Order can be different than order in original table Column can be renamed by giving it a column alias A Literal Value can be included – if text or date it should be within single quote marks. Note: SQL Aliases (“as”) are used to give a database table, or a column in a table, a temporary name. SELECT EMPLOYEE_ID AS NAME, LAST_NAME,CREDIT_LIMIT, 'EXCELLENT WORKER' As EVALUATION FROM EMPLOYEES; Lecture14 CIS 4523 - SQL prepared by Alma Bane

4.The Select Statement WHERE Clause 4/21/2018 4.The Select Statement WHERE Clause Specifies a condition that is True for all the rows you want in the result table EQUAL and other Comparison Conditions Equal (=) Less than (<) Less than or equal (<=) Greater than (>) Greater than or equal (>=) Not Equal (<>)(and others) select employee_id, first_name, last_name, manager_id from EMPLOYEES where manager_id = 203; employee_id first_name last_name manager_id 207 Dan Smith 203 208 Fred Campbell 210 Nancy Hoffman Lecture14 CIS 4523 - SQL prepared by Alma Bane

4.The Select Statement WHERE Clause 4/21/2018 4.The Select Statement WHERE Clause 202 Jim Kern 203 Martha Woods 207 Dan Smith 208 Fred Campbell 209 Paula Jacobs 210 Nancy Hoffman SET INCUSION Test – list of specific values IN (In a Set) NOT IN (Not In a Set) RANGE Test – between two values BETWEEN (in a range) NOT BETWEEN (not within a range) NULL Test IS NULL (is a null value) IS NOT NULL (is not a null value) select employee_id, first_name,last_name from EMPLOYEES where manager_id in (201, 203); select employee_id, first_name, last_name from EMPLOYEES where employee_id between 203 and 205; employee_id first_name last_name 203 Martha Woods 204 Ellen Owens 205 Henry Perkins select last_name from EMPLOYEES where manager_id is null; last_name Brown Rose Lecture14 CIS 4523 - SQL prepared by Alma Bane

4.The Select Statement WHERE Clause 4/21/2018 4.The Select Statement WHERE Clause last_name Brown Kern Owens Perkins Hoffman select last_name from EMPLOYEES where last_name like '%n%' PATTERN MATCHING Test – LIKE (matches a pattern) NOT LIKE (does not match a pattern) “_” to match any single character “%” to match an arbitrary number of characters (including zero characters) 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" Lecture14 CIS 4523 - SQL prepared by Alma Bane

4.The Select Statement WHERE Clause 4/21/2018 4.The Select Statement WHERE Clause BOOLEAN CONNECTORS – Joining simple conditions together AND (both of the conditions are True) OR (one or both of the conditions is True) NOT (the condition is False) employee_id 207 select employee_id from EMPLOYEES where first_name = 'DAN' and last_name = 'SMITH'; Lecture14 CIS 4523 - SQL prepared by Alma Bane

4.The Select Statement ORDER BY Clause 4/21/2018 4.The Select Statement ORDER BY Clause Syntax – ORDER BY a list of column names ORDER BY a list of numbers Sort Order Options for each column ASC means ascending order (default) DESC means descending order Example ORDER BY last_name, first_name employee_id first_name last_name 201 Susan Brown 204 Ellen Owens 203 Martha Woods select employee_id, first_name, last_name from EMPLOYEES where last_name in ('brown', 'woods', 'owens') order by last_name; Lecture14 CIS 4523 - SQL prepared by Alma Bane

4.The Select Statement Groups of Rows Dividing a Table into Groups of Rows and Summarizing Each Group The GROUP BY clause Each group is formed from All of the Rows in the Table With the Same value(s) in specified Columns Summary of groups of data within a column Groups formed on two or more columns Nulls in groups when there are two or more grouping columns Summarized data cannot be mixed with non-summarized data in the same SELECT statement manager_id null 201 202 203 select manager_id from EMPLOYEES group by manager_id Lecture14

4.The Select Statement Groups of data Summary of groups of data within a column Columns are summarized separately for each group Result Table contains one row for each group along with the summarized data for that group All Rows with NULL values in the grouping column are placed within a single group called the NULL GROUP Column1 manager_id 2 null 3 201 202 203 select count(*), manager_id from EMPLOYEES group by manager_id order by manager_id; Lecture14

4.The Select Statement Eliminating Summarized Data The HAVING clause The HAVING clause contrasted with the WHERE clause The whole process of the SELECT statement on a single table The HAVING clause does not add any more power to the SELECT statement Use a WHERE clause instead of a HAVING clause to eliminate raw data When the Result Table contains data that are grouped and summarized The Having Clause can eliminate groups from the result table The groups are still formed All the calculations and summarizations are done They are deleted at the end of the process Lecture14

4.The Select Statement Example supplier_id product_code total_servings Asp Sp 11 Cbc Gs 10 Frv Ff Jbr As Vr 17 Consider the LUNCH_ITEMS There are 71 rows List the supplier_id and product_code of all the foods for which 10 servings or more have been ordered (i.e. most popular) Steps: Rows are grouped and processed Having clause eliminates rows from the result table select supplier_id, product_code, sum(quantity) as total_servings from LUNCH_ITEMS group by supplier_id, product_code having sum(quantity) >= 10 order by supplier_id, product_code; Lecture14

4.The Select Statement Exercise 2 : FOODS table (with where only) List the sum of price_increase of each product code for each supplier filter by the price_increase greater than or equal to 0.5 sorted by descending increase. supplier_id product_code increase Asp Sw 0.4 Fs 0.25 Cbc Gs 0.7 0.3 Frv Br 0.05 Jbr As Vr 0.15 Vsb 0.5 supplier_id product_code increase Cbc Gs 0.7 Vsb As 0.5 Lecture14

4.The Select Statement Exercise 3 (LUNCH_ITEMS) List the sum of quantity of each product code for each supplier Ignored those with quantity less than and equal to 1 Filter by the sum of quantity greater than 4 sorted by descending sum SUPPLIER_ID PRODUCT_CODE QUANTITY CBC SW 2 … LUNCH_ID ITEM_NUMBER SUPPLIER_ID PRODUCT_CODE QUANTITY 4 2 CBC SW 9 3 16 1 17 GS 15 22 20 21 12 8 6 7 … SUPPLIER_ID PRODUCT_CODE QUANTITY CBC SW 4 JBR VR 12 ASP SP 6 AS supplier_id product_code increase JBR VR 12 ASP SW 6 Lecture14

4.The Select Statement Having Vs Where The Where Clause can only eliminate rows from the beginning table, the raw data, before any other processing occurs The Having Clause can only eliminate data that has been grouped and summarized, after most of the processing has already taken place The Where Clause cannot use column functions in the conditions it sets The Having Clause can use column functions in its conditions 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: Lecture14