Presentation is loading. Please wait.

Presentation is loading. Please wait.

While you are waiting for class to start... (1) Login to SQL Server 2012 Management Studio (2) Execute the file called “SQLLab3.sql”. It is located on.

Similar presentations


Presentation on theme: "While you are waiting for class to start... (1) Login to SQL Server 2012 Management Studio (2) Execute the file called “SQLLab3.sql”. It is located on."— Presentation transcript:

1 While you are waiting for class to start... (1) Login to SQL Server 2012 Management Studio (2) Execute the file called “SQLLab3.sql”. It is located on the k: drive in the IS475\ directory. You will see two errors, and then the tables will create and populate. There are two tables created and populated with this file – one has three rows and one has four rows of data.

2 What is the goal of a SQL query? To produce an accurate result table. To produce an accurate result table that contains meaningful information. To produce an accurate result table that contains meaningful information that will help solve a business problem. To produce an accurate result table that contains meaningful information that will help solve a business problem and is capable of being viewed through a front-end visualization program to make an impact.

3 Getting data from multiple tables Why do you want to access data from multiple tables in a single query? – To provide more complete information in a result table. – To support decision making. SQL programmers need to understand what happens when multiple tables are accessed in a single query.

4 Time for some new tables!! These two tables above are created and populated with the file called “SQLLab3.sql”. If you were late to class, look back on the first page of this handout to see the location of the file and information about execution.

5 Questions about design on previous page Does the design indicate whether or not referential integrity is enforced in the database? Does the inclusion of a foreign key to relate tables imply that referential integrity is enforced in the database? What does it mean to say “referential integrity is enforced” vs. “referential integrity is not enforced” in a database? Is it necessary to enforce referential integrity to relate tables in a relational database?

6 What do we want to accomplish? A simple result table with a combination of data from the order and customer tables, as shown on the next page.

7 Order IDOrderDateCustIDDueDate 1002/23/201412343/1/2014 2002/24/201467734/1/2014 3002/22/201412343/15/2014 CustIDCustomerName 1234John Smith 2555Jane Doe 6773Bertie Wooster 8372Martin Cheng ord cust Order IDOrderDateDueDateCustomerName 1002/23/20143/1/2014 John Smith 2002/24/20144/1/2014 Bertie Wooster 3002/22/20143/15/2014 John Smith + Result Table =

8 SELECT * FROMord, cust Let’s try and create the result table on the previous page. Open a new query window and type the following statement.

9 Order IDOrderDateCustIDDueDate 1002/23/201412343/1/2014 2002/24/201467734/1/2014 3002/22/201412343/15/2014 CustIDCustomerName 1234John Smith 2555Jane Doe 6773Bertie Wooster 8372Martin Cheng OrderIDOrderDateCustIDDueDateCustIDCustomerName 1002/23/201412343/1/20141234John Smith 1002/23/201412343/1/20142555Jane Doe 1002/23/201412343/1/20146773Bertie Wooster 1002/23/201412343/1/20148372Martin Cheng 2002/24/201467734/1/20141234John Smith 2002/24/201467734/1/20142555Jane Doe 2002/24/201467734/1/20146773Bertie Wooster 2002/24/201467734/1/20148372Martin Cheng 3002/22/201412343/15/20141234John Smith 3002/22/201412343/15/20142555Jane Doe 3002/22/201412343/15/20146773Bertie Wooster 3002/22/201412343/15/20148372Martin Cheng +

10 Cartesian Product Cross Join Or

11 OrderIDOrderDateCustIDDueDateCustIDCustomerName 1002/23/201412343/1/20141234John Smith 1002/23/201412343/1/20142555Jane Doe 1002/23/201412343/1/20146773Bertie Wooster 1002/23/201412343/1/20148372Martin Cheng 2002/24/201467734/1/20141234John Smith 2002/24/201467734/1/20142555Jane Doe 2002/24/201467734/1/20146773Bertie Wooster 2002/24/201467734/1/20148372Martin Cheng 3002/22/201412343/15/20141234John Smith 3002/22/201412343/15/20142555Jane Doe 3002/22/201412343/15/20146773Bertie Wooster 3002/22/201412343/15/20148372Martin Cheng 1002/23/201412343/1/20141234John Smith 2002/24/201467734/1/20146773Bertie Wooster 3002/22/201412343/15/20141234John Smith

12 SELECT* FROMord INNER JOINcust ONord.custID = cust.custID ORDER BY ord.orderID

13 OrderIDOrderDateCustIDDueDateCustIDCustomerName 1002/23/201412343/1/20141234John Smith 1002/23/201412343/1/20142555Jane Doe 1002/23/201412343/1/20146773Bertie Wooster 1002/23/201412343/1/20148372Martin Cheng 2002/24/201467734/1/20141234John Smith 2002/24/201467734/1/20142555Jane Doe 2002/24/201467734/1/20146773Bertie Wooster 2002/24/201467734/1/20148372Martin Cheng 3002/22/201412343/15/20141234John Smith 3002/22/201412343/15/20142555Jane Doe 3002/22/201412343/15/20146773Bertie Wooster 3002/22/201412343/15/20148372Martin Cheng 2002/24/201467734/1/20146773Bertie Wooster 1002/23/201412343/1/20141234John Smith 3002/22/201412343/15/20141234John Smith

14 SELECT* FROMcust INNER JOINord ONord.custID = cust.custID ORDER BY ord.orderID Open a new query window, and type the SQL code below. This code has the customer table placed first in the FROM statement. How do the results contrast with the SQL code on slide #13?

15 SELECTord.orderid, ord.orderdate, ord.duedate, cust.customername FROMord INNER JOINcust ONOrd.custID = Cust.custID ORDER BYord.orderid Order IDOrderDateDueDateCustomerName 1002/23/20143/1/2014 John Smith 2002/24/20144/1/2014 Bertie Wooster 3002/22/20143/15/2014 John Smith Finalize the query by SELECTing only the required columns

16 Ord Cust Results of Inner Join

17 Let’s make a new query!

18 Order IDOrderDateCustIDDueDate 1002/23/201412343/1/2014 2002/24/201467734/1/2014 3002/22/201412343/15/2014 CustIDCustomerName 1234John Smith 2555Jane Doe 6773Bertie Wooster 8372Martin Cheng ord cust + CustomerNameOrderIDDueDate Bertie Wooster2004/1/2014 Jane DoeNo order John Smith1003/1/2014 John Smith3003/15/2014 Martin ChengNo order =

19 SELECTcust.CustomerName, ISNULL(ord.orderID, ‘No Order’) OrderID, ord.DueDate FROMord INNER JOINcust ONOrd.custID = Cust.custID ORDER BYcust.customername CustomerNameOrderIDDueDate Bertie Wooster2004/1/2014 John Smith1003/1/2014 John Smith3003/15/2014

20 Ord Cust Results of Outer JoinResults of One-Sided Outer Join

21 SELECTcust.CustomerName, ISNULL(ord.orderID, ‘No Order’) OrderID, ord.DueDate FROMord RIGHT OUTER JOINcust ONOrd.custID = Cust.custID ORDER BYcust.customername CustomerNameOrderIDDueDate Bertie Wooster2004/1/2014 Jane DoeNo order John Smith1003/1/2014 John Smith3003/15/2014 Martin ChengNo order

22 FROMord RIGHT OUTER JOINcust ord cust + = Result Table Left Side of the join Right Side of the join

23 Let’s say that referential integrity is not enforced and we have more rows in our tables...

24 Order IDOrderDateCustIDDueDate 1002/23/201412343/1/2014 2002/24/201467734/1/2014 3002/22/201412343/15/2014 4002/27/201425553/16/2014 5002/12/201489892/22/2014 6002/23/201425552/27/2014 7002/15/201425554/1/2014 CustIDCustomerName 1234John Smith 2555Jane Doe 6773Bertie Wooster 8372Martin Cheng Execute the script file called: SQLLab3Expand.sql on the k: drive in the IS475\ directory to create a table called “ord1”.

25 How many rows and columns in the cross join? SELECT * FROMord1, cust

26 What would the results look like from an inner join? SELECT* FROMord1 INNER JOINcust ONord1.custID = cust.custID

27 Order IDOrderDateCustIDDueDateC.CustIDCustomerName 1002/23/201412343/1/20141234John Smith 2002/24/201467734/1/20146773Bertie Wooster 3002/22/201412343/15/20141234John Smith 4002/27/201425553/16/20142555Jane Doe 6002/23/201425552/27/20142555Jane Doe 7002/15/201425554/1/20142555Jane Doe Why is OrderID 500 missing?

28 What would the results look like from a right outer join? SELECT* FROMord1 RIGHT OUTER JOINcust ONord1.custID = cust.custID

29 Order IDOrderDateCustIDDueDateC.CustIDCustomerName 1002/23/201412343/1/20141234John Smith 2002/24/201467734/1/20146773Bertie Wooster 3002/22/201412343/15/20141234John Smith 4002/27/201425553/16/20142555Jane Doe 6002/23/201425552/27/20142555Jane Doe 7002/15/201425554/1/20142555Jane Doe 8372Martin Cheng The row is still missing...

30 What would the results look like from a left outer join? SELECT* FROMord1 LEFT OUTER JOINcust ONord1.custID = cust.custID

31 Ord Cust Results of Outer JoinResults of One-Sided Outer Join

32 Order IDOrderDateCustIDDueDateC.CustIDCustomerName 1002/23/201412343/1/20141234John Smith 2002/24/201467734/1/20146773Bertie Wooster 3002/22/201412343/15/20141234John Smith 4002/27/201425553/16/20142555Jane Doe 5002/12/201489892/22/2014 6002/23/201425552/27/20142555Jane Doe 7002/15/201425554/1/20142555Jane Doe

33 How do you make the NULL data more meaningful? SELECT ord1.OrderID, ord1.OrderDate, ord1.CustID, ord1.DueDate, ISNULL(cust.CustID, ‘n/a’), ISNULL(cust.CustomerName, ‘Missing Name’) FROMord1 LEFT OUTER JOINcust ON ord1.custID = cust.custID Open a new query window and type the following query:

34 All rows from both tables! SELECT* FROMord1 FULL OUTER JOINcust ONord1.custID = cust.custID

35 Ord Cust Results of Outer Join Results of Both-Sided Outer Join

36 Order IDOrderDateCustIDDueDateC.CustIDCustomerName 1002/23/201412343/1/20141234John Smith 2002/24/201467734/1/20146773Bertie Wooster 3002/22/201412343/15/20141234John Smith 4002/27/201425553/16/20142555Jane Doe 5002/12/201489892/22/2014 6002/23/201425552/27/20142555Jane Doe 7002/15/201425554/1/20142555Jane Doe 8372Martin Cheng

37 Write a query on your own! Normally, we want to see all the rows in the child table, rather than the parent. Write a query that displays all the orders in the ord1 table, but include only the columns shown below: Order IDOrderDateCustIDCustomerName 1002/23/20141234John Smith 2002/24/20146773Bertie Wooster 3002/22/20141234John Smith 4002/27/20142555Jane Doe 5002/12/20148989Missing Name 6002/23/20142555Jane Doe 7002/15/20142555Jane Doe

38 Write a new query Write a query that displays all the orders placed by the customer “Jane Doe”. Assume that you don’t know Jane Doe’s customer ID and have to use her name in the WHERE clause. The join condition is the same, but you are adding a WHERE clause. The goal is to realize that once you have joined tables, you have all fields from all tables available in all parts of the query. The result table should look like the one provided below. Order IDOrderDateDueDate 4002/27/20143/16/2014 6002/23/20142/27/2014 7002/15/20144/1/2014

39 Write another query Write a query that displays all the orders that don’t have a valid customer. The result table should look like the one provided below. Order IDOrderDateCustIDDueDateC.CustIDCustomerName 5002/12/201489892/22/2014n/aMissing Name

40 Last 2 queries of the day… First, write a query that summarizes order data by customer. The result table should look like the one provided below. Hint: Use the GROUP BY clause. CustIDCustomerNameCountofOrders 1234John Smith2 2555Jane Doe3 6773Bertie Wooster1 Second, change it so that all customers are displayed, whether or not they have an order. The result table should look like the one provided below. CustIDCustomerNameCountofOrders 1234John Smith2 2555Jane Doe3 6773Bertie Wooster1 8372Martin Cheng0


Download ppt "While you are waiting for class to start... (1) Login to SQL Server 2012 Management Studio (2) Execute the file called “SQLLab3.sql”. It is located on."

Similar presentations


Ads by Google