32
20152016

2015 2016 - Herefordshire and Ludlow Collegewiki.computing.hct.ac.uk › _media › computing › hnd › hndu... · Using a Function Using the COUNT ... 1003 Biotech 1002 Sales 1001

  • Upload
    others

  • View
    3

  • Download
    0

Embed Size (px)

Citation preview

Page 1: 2015 2016 - Herefordshire and Ludlow Collegewiki.computing.hct.ac.uk › _media › computing › hnd › hndu... · Using a Function Using the COUNT ... 1003 Biotech 1002 Sales 1001

2015‐2016

Page 2: 2015 2016 - Herefordshire and Ludlow Collegewiki.computing.hct.ac.uk › _media › computing › hnd › hndu... · Using a Function Using the COUNT ... 1003 Biotech 1002 Sales 1001

Phil Smith 

Page 3: 2015 2016 - Herefordshire and Ludlow Collegewiki.computing.hct.ac.uk › _media › computing › hnd › hndu... · Using a Function Using the COUNT ... 1003 Biotech 1002 Sales 1001

Today Learning outcomes – LO3 Queries and Reporting.

DDL, DML, DCL, TCL, DQL

More DQL – which stands for?

Data Query Language

Basically the Select Clause

Page 4: 2015 2016 - Herefordshire and Ludlow Collegewiki.computing.hct.ac.uk › _media › computing › hnd › hndu... · Using a Function Using the COUNT ... 1003 Biotech 1002 Sales 1001

SQL ‐ Select The SELECT clause is use to make queries against data held in one of more tables in your database.

The basic syntax of the select clause ‐

SELECT column-name1, column-name2, column-name3, column-nameN

from table-name;

Page 5: 2015 2016 - Herefordshire and Ludlow Collegewiki.computing.hct.ac.uk › _media › computing › hnd › hndu... · Using a Function Using the COUNT ... 1003 Biotech 1002 Sales 1001

SQL: SELECT Statement

A basic SELECT statement includes 3 clauses

SELECT <attribute name> FROM <tables> WHERE <condition>

SELECT

Specifies the attributes that are part of the resulting relation

FROM

Specifies the tables that serve as the input to the statement

WHERE

Specifies the selection condition, including the join condition.

Note: that you don't need to use WHERE

Page 6: 2015 2016 - Herefordshire and Ludlow Collegewiki.computing.hct.ac.uk › _media › computing › hnd › hndu... · Using a Function Using the COUNT ... 1003 Biotech 1002 Sales 1001

Using a “*” in a select statement indicates that every attribute of the input table is to be selected.Example: SELECT * FROM … WHERE …;

To get unique rows, type the keyword DISTINCT after SELECT.Example: SELECT DISTINCT * FROM …

WHERE …;

SQL: SELECT Statement (cont.)

Page 7: 2015 2016 - Herefordshire and Ludlow Collegewiki.computing.hct.ac.uk › _media › computing › hnd › hndu... · Using a Function Using the COUNT ... 1003 Biotech 1002 Sales 1001

7

SELECT Statement Clauses of the SELECT statement:

SELECT List the columns (and expressions) that should be returned from the query

FROM Indicate the table(s) or view(s) from which data will be obtained

WHERE Indicate the conditions under which a row will be included in the result

GROUP BY Indicate columns to group the results 

HAVING Indicate the conditions under which a group will be included

ORDER BY Sorts the result according to specified columns

Page 8: 2015 2016 - Herefordshire and Ludlow Collegewiki.computing.hct.ac.uk › _media › computing › hnd › hndu... · Using a Function Using the COUNT ... 1003 Biotech 1002 Sales 1001

8

Figure 7-8: SQL statement processing order

Page 9: 2015 2016 - Herefordshire and Ludlow Collegewiki.computing.hct.ac.uk › _media › computing › hnd › hndu... · Using a Function Using the COUNT ... 1003 Biotech 1002 Sales 1001

SELECT Example Find products with standard price less than $275

SELECT PRODUCT_NAME, STANDARD_PRICE FROM PRODUCT_V WHERE STANDARD_PRICE < 275;

Page 10: 2015 2016 - Herefordshire and Ludlow Collegewiki.computing.hct.ac.uk › _media › computing › hnd › hndu... · Using a Function Using the COUNT ... 1003 Biotech 1002 Sales 1001

10

Page 11: 2015 2016 - Herefordshire and Ludlow Collegewiki.computing.hct.ac.uk › _media › computing › hnd › hndu... · Using a Function Using the COUNT ... 1003 Biotech 1002 Sales 1001

SELECT Example using Alias Alias is an alternative column or table name

SELECT CUST.CUSTOMER AS NAME, CUST.CUSTOMER_ADDRESS 

FROM CUSTOMER_V CUSTWHERE NAME = ‘Home Furnishings’;

Page 12: 2015 2016 - Herefordshire and Ludlow Collegewiki.computing.hct.ac.uk › _media › computing › hnd › hndu... · Using a Function Using the COUNT ... 1003 Biotech 1002 Sales 1001

SELECT Example Using a Function

Using the COUNT aggregate function to find totals Aggregate functions: SUM(), MIN(), MAX(), AVG(), COUNT()

SELECT COUNT(*) FROM ORDER_LINE_VWHERE ORDER_ID = 1004;

Page 13: 2015 2016 - Herefordshire and Ludlow Collegewiki.computing.hct.ac.uk › _media › computing › hnd › hndu... · Using a Function Using the COUNT ... 1003 Biotech 1002 Sales 1001

13

SELECT Example – Boolean Operators AND, OR, and NOT Operators for customizing conditions in WHERE clause

SELECT PRODUCT_DESCRIPTION, PRODUCT_FINISH, STANDARD_PRICE

FROM PRODUCT_VWHERE (PRODUCT_DESCRIPTION LIKE ‘%Desk’OR PRODUCT_DESCRIPTION LIKE ‘%Table’) AND UNIT_PRICE > 300;

Note: the LIKE operator allows you to compare strings using wildcards. For example, the % wildcard in ‘%Desk’ indicates that all strings that have any number of characters preceding the word “Desk” will be allowed

Page 14: 2015 2016 - Herefordshire and Ludlow Collegewiki.computing.hct.ac.uk › _media › computing › hnd › hndu... · Using a Function Using the COUNT ... 1003 Biotech 1002 Sales 1001

SELECT Example –Sorting Results with the ORDER BY Clause

Sort the results first by STATE, and within a state by CUSTOMER_NAME

SELECT CUSTOMER_NAME, CITY, STATEFROM CUSTOMER_VWHERE STATE IN (‘FL’, ‘TX’, ‘CA’, ‘HI’)ORDER BY STATE, CUSTOMER_NAME;

Note: the IN operator in this example allows you to include rows whose STATE value is either FL, TX, CA, or HI. It is more efficient than separate OR conditions

Page 15: 2015 2016 - Herefordshire and Ludlow Collegewiki.computing.hct.ac.uk › _media › computing › hnd › hndu... · Using a Function Using the COUNT ... 1003 Biotech 1002 Sales 1001

SELECT Example –Categorizing Results Using the GROUP BY Clause

SELECT STATE, COUNT(STATE) FROM CUSTOMER_VGROUP BY STATE;

Note: you can use single‐value fields with aggregate functions if they are included in the GROUP BY clause

Page 16: 2015 2016 - Herefordshire and Ludlow Collegewiki.computing.hct.ac.uk › _media › computing › hnd › hndu... · Using a Function Using the COUNT ... 1003 Biotech 1002 Sales 1001

SELECT Example –Qualifying Results by Categories Using the HAVING Clause

For use with GROUP BY

SELECT STATE, COUNT(STATE) FROM CUSTOMER_VGROUP BY STATEHAVING COUNT(STATE) > 1;

Like a WHERE clause, but it operates on groups (categories), not on individual rows. Here, only those groups with total numbers greater than 1 will be included in final result

Page 17: 2015 2016 - Herefordshire and Ludlow Collegewiki.computing.hct.ac.uk › _media › computing › hnd › hndu... · Using a Function Using the COUNT ... 1003 Biotech 1002 Sales 1001

Using and Defining Views Views provide users controlled access to tables

Base Table – table containing the raw data

Dynamic View A “virtual table” created dynamically upon request by a user  No data actually stored; instead data from base table made available to user

Based on SQL SELECT statement on base tables or other views

Page 18: 2015 2016 - Herefordshire and Ludlow Collegewiki.computing.hct.ac.uk › _media › computing › hnd › hndu... · Using a Function Using the COUNT ... 1003 Biotech 1002 Sales 1001

Sample CREATE VIEWCREATE VIEW EXPENSIVE_STUFF_V ASSELECT PRODUCT_ID, PRODUCT_NAME, UNIT_PRICEFROM PRODUCT_TWHERE UNIT_PRICE >300;

View has a nameView is based on a SELECT statement

Page 19: 2015 2016 - Herefordshire and Ludlow Collegewiki.computing.hct.ac.uk › _media › computing › hnd › hndu... · Using a Function Using the COUNT ... 1003 Biotech 1002 Sales 1001

19

Advantages of Views

Simplify query commands Assist with data security (but don't rely on views for security, there are more important security measures)

Enhance programming productivity Contain most current base table data Use little storage space Provide customized view for user

Page 20: 2015 2016 - Herefordshire and Ludlow Collegewiki.computing.hct.ac.uk › _media › computing › hnd › hndu... · Using a Function Using the COUNT ... 1003 Biotech 1002 Sales 1001

20

Disadvantages of Views Use processing time each time view is referenced May or may not be directly updateable

Page 21: 2015 2016 - Herefordshire and Ludlow Collegewiki.computing.hct.ac.uk › _media › computing › hnd › hndu... · Using a Function Using the COUNT ... 1003 Biotech 1002 Sales 1001

SQL: Join operationA join can be specified in the FROM clause which list the two input relations and the WHERE clause which lists the join condition.Example:

Biotech1003Sales1002IT1001DivisionID

TN1002MA1001CA1000StateID

Emp Dept

Page 22: 2015 2016 - Herefordshire and Ludlow Collegewiki.computing.hct.ac.uk › _media › computing › hnd › hndu... · Using a Function Using the COUNT ... 1003 Biotech 1002 Sales 1001

SQL: Join operation (cont.)

Sales1002IT1001Dept.DivisionDept.ID

TN1002MA1001Emp.StateEmp.ID

inner join = joinSELECT *

FROM emp join dept (or FROM emp, dept) on emp.id = dept.id;

Page 23: 2015 2016 - Herefordshire and Ludlow Collegewiki.computing.hct.ac.uk › _media › computing › hnd › hndu... · Using a Function Using the COUNT ... 1003 Biotech 1002 Sales 1001

SQL: Join operation (cont.)

IT1001Sales1002

nullnullDept.DivisionDept.ID

CA1000

TN1002MA1001

Emp.StateEmp.ID

left outer join = left joinSELECT *

FROM emp left join depton emp.id = dept.id;

Page 24: 2015 2016 - Herefordshire and Ludlow Collegewiki.computing.hct.ac.uk › _media › computing › hnd › hndu... · Using a Function Using the COUNT ... 1003 Biotech 1002 Sales 1001

SQL: Join operation (cont.)

Sales1002Biotech1003

IT1001Dept.DivisionDept.ID

MA1001

nullnullTN1002

Emp.StateEmp.ID

right outer join = right joinSELECT *

FROM emp right join depton emp.id = dept.id;

Page 25: 2015 2016 - Herefordshire and Ludlow Collegewiki.computing.hct.ac.uk › _media › computing › hnd › hndu... · Using a Function Using the COUNT ... 1003 Biotech 1002 Sales 1001

SQL Union1. You can combine the output from two or more 

queries/views by using the union clause.2. The general form is ‐Select col1, col2 from table1 where col1 = ‘xxx’UnionSelect col3, col4 from table2 where col3 = ‘yyy’

Rules, the selected columns from each query must match in type, the output column must be the same type.

Page 26: 2015 2016 - Herefordshire and Ludlow Collegewiki.computing.hct.ac.uk › _media › computing › hnd › hndu... · Using a Function Using the COUNT ... 1003 Biotech 1002 Sales 1001

SQL Correlated sub queryConsider this query ‐ we want to list all customers who have place no orders.

We could try and do this with left outer joins.But we can to a correlated subquery.

Select CustomerID From Customer CWhere not exists (select ‘a’ from CustomerOrder CO where CO.Customerid = C.Customerid)

The sub query returns a true/false condition.Select ‘a’ is faster then returning a column.

Page 27: 2015 2016 - Herefordshire and Ludlow Collegewiki.computing.hct.ac.uk › _media › computing › hnd › hndu... · Using a Function Using the COUNT ... 1003 Biotech 1002 Sales 1001

Learning outcomes – LO21. Be able to implement user Interfaces.

1. requirements e.g. 1. functionality, 2. reliability, 3. consistency, 4. performance, 5. menu driven, 6. HCI interface 

Most of the SQL in DML and DQL will be needed to address the functional requirements of the user interface (1 thru 4).

Page 28: 2015 2016 - Herefordshire and Ludlow Collegewiki.computing.hct.ac.uk › _media › computing › hnd › hndu... · Using a Function Using the COUNT ... 1003 Biotech 1002 Sales 1001

Learning outcomes – LO2LO22.3 apply a range of database tools and techniques to enhance the user interface.This is putting our SQL knowledge to use in an interface.3.3 critically evaluate how meaningful data has been extracted through the use of query tools.Here we need to examine the use of SQL has been used to deliver business data to the user.  

Page 29: 2015 2016 - Herefordshire and Ludlow Collegewiki.computing.hct.ac.uk › _media › computing › hnd › hndu... · Using a Function Using the COUNT ... 1003 Biotech 1002 Sales 1001

Learning outcomes – LO2A couple of terms we need to know -

Verification –Verification means to check that the data on the original source document is identical to the data that you have entered into the system or object document.

Validation –Validation aims to make sure that data is sensible, reasonable, complete and within acceptable boundaries.

Validation is a user constraint not a database constraint!

Page 30: 2015 2016 - Herefordshire and Ludlow Collegewiki.computing.hct.ac.uk › _media › computing › hnd › hndu... · Using a Function Using the COUNT ... 1003 Biotech 1002 Sales 1001

HCIUser interface: requirements eg functionality, reliability, consistency, performance, menu driven, HCI interface

We need to create a simple interface! Sorry.

For this we can use Excel forms or Microsoft Access or php if you are able or something else.

I will demonstrate how to connect a Sql Server and MySql database to Microsoft Access 2013.

Page 31: 2015 2016 - Herefordshire and Ludlow Collegewiki.computing.hct.ac.uk › _media › computing › hnd › hndu... · Using a Function Using the COUNT ... 1003 Biotech 1002 Sales 1001

Learning outcomes – LO2Now you can do Lab07.

Assignment 2 is now issued.

Page 32: 2015 2016 - Herefordshire and Ludlow Collegewiki.computing.hct.ac.uk › _media › computing › hnd › hndu... · Using a Function Using the COUNT ... 1003 Biotech 1002 Sales 1001

Summary

What have we learnt today?