2015 2016 - herefordshire and ludlow collegewiki.computing.hct.ac.uk › _media › computing ›...
TRANSCRIPT
![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](https://reader033.vdocument.in/reader033/viewer/2022060410/5f106d767e708231d4490ebf/html5/thumbnails/1.jpg)
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](https://reader033.vdocument.in/reader033/viewer/2022060410/5f106d767e708231d4490ebf/html5/thumbnails/2.jpg)
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](https://reader033.vdocument.in/reader033/viewer/2022060410/5f106d767e708231d4490ebf/html5/thumbnails/3.jpg)
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](https://reader033.vdocument.in/reader033/viewer/2022060410/5f106d767e708231d4490ebf/html5/thumbnails/4.jpg)
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](https://reader033.vdocument.in/reader033/viewer/2022060410/5f106d767e708231d4490ebf/html5/thumbnails/5.jpg)
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](https://reader033.vdocument.in/reader033/viewer/2022060410/5f106d767e708231d4490ebf/html5/thumbnails/6.jpg)
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](https://reader033.vdocument.in/reader033/viewer/2022060410/5f106d767e708231d4490ebf/html5/thumbnails/7.jpg)
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](https://reader033.vdocument.in/reader033/viewer/2022060410/5f106d767e708231d4490ebf/html5/thumbnails/8.jpg)
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](https://reader033.vdocument.in/reader033/viewer/2022060410/5f106d767e708231d4490ebf/html5/thumbnails/9.jpg)
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](https://reader033.vdocument.in/reader033/viewer/2022060410/5f106d767e708231d4490ebf/html5/thumbnails/10.jpg)
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](https://reader033.vdocument.in/reader033/viewer/2022060410/5f106d767e708231d4490ebf/html5/thumbnails/11.jpg)
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](https://reader033.vdocument.in/reader033/viewer/2022060410/5f106d767e708231d4490ebf/html5/thumbnails/12.jpg)
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](https://reader033.vdocument.in/reader033/viewer/2022060410/5f106d767e708231d4490ebf/html5/thumbnails/13.jpg)
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](https://reader033.vdocument.in/reader033/viewer/2022060410/5f106d767e708231d4490ebf/html5/thumbnails/14.jpg)
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](https://reader033.vdocument.in/reader033/viewer/2022060410/5f106d767e708231d4490ebf/html5/thumbnails/15.jpg)
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](https://reader033.vdocument.in/reader033/viewer/2022060410/5f106d767e708231d4490ebf/html5/thumbnails/16.jpg)
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](https://reader033.vdocument.in/reader033/viewer/2022060410/5f106d767e708231d4490ebf/html5/thumbnails/17.jpg)
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](https://reader033.vdocument.in/reader033/viewer/2022060410/5f106d767e708231d4490ebf/html5/thumbnails/18.jpg)
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](https://reader033.vdocument.in/reader033/viewer/2022060410/5f106d767e708231d4490ebf/html5/thumbnails/19.jpg)
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](https://reader033.vdocument.in/reader033/viewer/2022060410/5f106d767e708231d4490ebf/html5/thumbnails/20.jpg)
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](https://reader033.vdocument.in/reader033/viewer/2022060410/5f106d767e708231d4490ebf/html5/thumbnails/21.jpg)
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](https://reader033.vdocument.in/reader033/viewer/2022060410/5f106d767e708231d4490ebf/html5/thumbnails/22.jpg)
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](https://reader033.vdocument.in/reader033/viewer/2022060410/5f106d767e708231d4490ebf/html5/thumbnails/23.jpg)
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](https://reader033.vdocument.in/reader033/viewer/2022060410/5f106d767e708231d4490ebf/html5/thumbnails/24.jpg)
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](https://reader033.vdocument.in/reader033/viewer/2022060410/5f106d767e708231d4490ebf/html5/thumbnails/25.jpg)
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](https://reader033.vdocument.in/reader033/viewer/2022060410/5f106d767e708231d4490ebf/html5/thumbnails/26.jpg)
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](https://reader033.vdocument.in/reader033/viewer/2022060410/5f106d767e708231d4490ebf/html5/thumbnails/27.jpg)
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](https://reader033.vdocument.in/reader033/viewer/2022060410/5f106d767e708231d4490ebf/html5/thumbnails/28.jpg)
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](https://reader033.vdocument.in/reader033/viewer/2022060410/5f106d767e708231d4490ebf/html5/thumbnails/29.jpg)
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](https://reader033.vdocument.in/reader033/viewer/2022060410/5f106d767e708231d4490ebf/html5/thumbnails/30.jpg)
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](https://reader033.vdocument.in/reader033/viewer/2022060410/5f106d767e708231d4490ebf/html5/thumbnails/31.jpg)
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](https://reader033.vdocument.in/reader033/viewer/2022060410/5f106d767e708231d4490ebf/html5/thumbnails/32.jpg)
Summary
What have we learnt today?