+ All Categories
Home > Documents > While you are waiting for class to start...

While you are waiting for class to start...

Date post: 04-Feb-2016
Category:
Upload: tassos
View: 29 times
Download: 0 times
Share this document with a friend
Description:
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 . - PowerPoint PPT Presentation
Popular Tags:
40
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.
Transcript
Page 1: While you are waiting for class to start...

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.

Page 2: While you are waiting for class to start...

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.

Page 3: While you are waiting for class to start...

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.

Page 4: While you are waiting for class to start...

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.

Page 5: While you are waiting for class to start...

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?

Page 6: While you are waiting for class to start...

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.

Page 7: While you are waiting for class to start...

Order ID OrderDate CustID DueDate100 2/23/2014 1234 3/1/2014

200 2/24/2014 6773 4/1/2014

300 2/22/2014 1234 3/15/2014

CustID CustomerName1234 John Smith2555 Jane Doe6773 Bertie Wooster8372 Martin Cheng

ord cust

Order ID OrderDate DueDate CustomerName

100 2/23/2014 3/1/2014 John Smith200 2/24/2014 4/1/2014 Bertie Wooster300 2/22/2014 3/15/2014 John Smith

+

Result Table

=

Page 8: While you are waiting for class to start...

SELECT*

FROM ord, cust

Let’s try and create the result table on the previous page. Open a new query window and type the following statement.

Page 9: While you are waiting for class to start...

Order ID OrderDate CustID DueDate100 2/23/2014 1234 3/1/2014

200 2/24/2014 6773 4/1/2014

300 2/22/2014 1234 3/15/2014

CustID CustomerName1234 John Smith2555 Jane Doe6773 Bertie Wooster8372 Martin Cheng

OrderID OrderDate CustID DueDate CustID CustomerName100 2/23/2014 1234 3/1/2014 1234 John Smith

100 2/23/2014 1234 3/1/2014 2555 Jane Doe

100 2/23/2014 1234 3/1/2014 6773 Bertie Wooster

100 2/23/2014 1234 3/1/2014 8372 Martin Cheng200 2/24/2014 6773 4/1/2014 1234 John Smith

200 2/24/2014 6773 4/1/2014 2555 Jane Doe

200 2/24/2014 6773 4/1/2014 6773 Bertie Wooster200 2/24/2014 6773 4/1/2014 8372 Martin Cheng300 2/22/2014 1234 3/15/2014 1234 John Smith300 2/22/2014 1234 3/15/2014 2555 Jane Doe300 2/22/2014 1234 3/15/2014 6773 Bertie Wooster300 2/22/2014 1234 3/15/2014 8372 Martin Cheng

+

Page 10: While you are waiting for class to start...

Cartesian Product

Cross Join

Or

Page 11: While you are waiting for class to start...

OrderID OrderDate CustID DueDate CustID CustomerName100 2/23/2014 1234 3/1/2014 1234 John Smith

100 2/23/2014 1234 3/1/2014 2555 Jane Doe

100 2/23/2014 1234 3/1/2014 6773 Bertie Wooster

100 2/23/2014 1234 3/1/2014 8372 Martin Cheng200 2/24/2014 6773 4/1/2014 1234 John Smith

200 2/24/2014 6773 4/1/2014 2555 Jane Doe

200 2/24/2014 6773 4/1/2014 6773 Bertie Wooster200 2/24/2014 6773 4/1/2014 8372 Martin Cheng300 2/22/2014 1234 3/15/2014 1234 John Smith300 2/22/2014 1234 3/15/2014 2555 Jane Doe300 2/22/2014 1234 3/15/2014 6773 Bertie Wooster300 2/22/2014 1234 3/15/2014 8372 Martin Cheng

100 2/23/2014 1234 3/1/2014 1234 John Smith

200 2/24/2014 6773 4/1/2014 6773 Bertie Wooster

300 2/22/2014 1234 3/15/2014 1234 John Smith

Page 12: While you are waiting for class to start...

SELECT *FROM ordINNER JOIN custON ord.custID = cust.custIDORDER BY ord.orderID

Page 13: While you are waiting for class to start...

OrderID OrderDate CustID DueDate CustID CustomerName100 2/23/2014 1234 3/1/2014 1234 John Smith

100 2/23/2014 1234 3/1/2014 2555 Jane Doe

100 2/23/2014 1234 3/1/2014 6773 Bertie Wooster

100 2/23/2014 1234 3/1/2014 8372 Martin Cheng200 2/24/2014 6773 4/1/2014 1234 John Smith

200 2/24/2014 6773 4/1/2014 2555 Jane Doe

200 2/24/2014 6773 4/1/2014 6773 Bertie Wooster200 2/24/2014 6773 4/1/2014 8372 Martin Cheng300 2/22/2014 1234 3/15/2014 1234 John Smith300 2/22/2014 1234 3/15/2014 2555 Jane Doe300 2/22/2014 1234 3/15/2014 6773 Bertie Wooster300 2/22/2014 1234 3/15/2014 8372 Martin Cheng

200 2/24/2014 6773 4/1/2014 6773 Bertie Wooster

100 2/23/2014 1234 3/1/2014 1234 John Smith

300 2/22/2014 1234 3/15/2014 1234 John Smith

Page 14: While you are waiting for class to start...

SELECT *FROM custINNER JOIN ordON ord.custID = cust.custIDORDER 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?

Page 15: While you are waiting for class to start...

SELECT ord.orderid,ord.orderdate,ord.duedate,cust.customername

FROM ord INNER JOIN cust ON Ord.custID = Cust.custIDORDER BY ord.orderid

Order ID OrderDate DueDate CustomerName

100 2/23/2014 3/1/2014 John Smith200 2/24/2014 4/1/2014 Bertie Wooster300 2/22/2014 3/15/2014 John Smith

Finalize the query by SELECTing only the required columns

Page 16: While you are waiting for class to start...

Ord Cust

Results of Inner Join

Page 17: While you are waiting for class to start...

Let’s make a new query!

Page 18: While you are waiting for class to start...

Order ID OrderDate CustID DueDate100 2/23/2014 1234 3/1/2014

200 2/24/2014 6773 4/1/2014

300 2/22/2014 1234 3/15/2014

CustID CustomerName1234 John Smith2555 Jane Doe6773 Bertie Wooster8372 Martin Cheng

ord cust

+

CustomerName OrderID DueDateBertie Wooster 200 4/1/2014

Jane Doe No order

John Smith 100 3/1/2014

John Smith 300 3/15/2014

Martin Cheng No order

=

Page 19: While you are waiting for class to start...

SELECT cust.CustomerName,ISNULL(ord.orderID, ‘No Order’) OrderID,ord.DueDate

FROM ord INNER JOIN cust ON Ord.custID = Cust.custIDORDER BY cust.customername

CustomerName OrderID DueDateBertie Wooster 200 4/1/2014

John Smith 100 3/1/2014

John Smith 300 3/15/2014

Page 20: While you are waiting for class to start...

Ord Cust

Results of Outer JoinResults of One-Sided Outer Join

Page 21: While you are waiting for class to start...

SELECT cust.CustomerName,ISNULL(ord.orderID, ‘No Order’) OrderID,ord.DueDate

FROM ord RIGHT OUTER JOIN cust ON Ord.custID = Cust.custIDORDER BY cust.customername

CustomerName OrderID DueDateBertie Wooster 200 4/1/2014

Jane Doe No order

John Smith 100 3/1/2014

John Smith 300 3/15/2014

Martin Cheng No order

Page 22: While you are waiting for class to start...

FROM ord RIGHT OUTER JOIN cust

ord cust+ = Result Table

Left Side of the join

Right Side of the join

Page 23: While you are waiting for class to start...

Let’s say that referential integrity is not enforced

and we have more rows in our tables...

Page 24: While you are waiting for class to start...

Order ID OrderDate CustID DueDate100 2/23/2014 1234 3/1/2014

200 2/24/2014 6773 4/1/2014

300 2/22/2014 1234 3/15/2014

400 2/27/2014 2555 3/16/2014

500 2/12/2014 8989 2/22/2014

600 2/23/2014 2555 2/27/2014

700 2/15/2014 2555 4/1/2014

CustID CustomerName1234 John Smith2555 Jane Doe6773 Bertie Wooster8372 Martin Cheng

Execute the script file called: SQLLab3Expand.sql on the k: drive in the IS475\ directory to create a table called “ord1”.

Page 25: While you are waiting for class to start...

How many rows and columns in the cross join?

SELECT

*FROM ord1,

cust

Page 26: While you are waiting for class to start...

What would the results look like from an inner join?

SELECT *FROM ord1INNER JOIN custON ord1.custID = cust.custID

Page 27: While you are waiting for class to start...

Order ID OrderDate CustID DueDate C.CustID CustomerName

100 2/23/2014 1234 3/1/2014 1234 John Smith200 2/24/2014 6773 4/1/2014 6773 Bertie Wooster300 2/22/2014 1234 3/15/2014 1234 John Smith

400 2/27/2014 2555 3/16/2014 2555 Jane Doe

600 2/23/2014 2555 2/27/2014 2555 Jane Doe

700 2/15/2014 2555 4/1/2014 2555 Jane Doe

Why is OrderID 500 missing?

Page 28: While you are waiting for class to start...

What would the results look like from a right outer join?

SELECT *FROM ord1RIGHT OUTER JOIN custON ord1.custID = cust.custID

Page 29: While you are waiting for class to start...

Order ID OrderDate CustID DueDate C.CustID CustomerName100 2/23/2014 1234 3/1/2014 1234 John Smith200 2/24/2014 6773 4/1/2014 6773 Bertie Wooster300 2/22/2014 1234 3/15/2014 1234 John Smith400 2/27/2014 2555 3/16/2014 2555 Jane Doe600 2/23/2014 2555 2/27/2014 2555 Jane Doe700 2/15/2014 2555 4/1/2014 2555 Jane Doe

8372 Martin Cheng

The row is still missing...

Page 30: While you are waiting for class to start...

What would the results look like from a left outer join?

SELECT *FROM ord1LEFT OUTER JOIN custON ord1.custID = cust.custID

Page 31: While you are waiting for class to start...

Ord Cust

Results of Outer JoinResults of One-Sided Outer Join

Page 32: While you are waiting for class to start...

Order ID OrderDate CustID DueDate C.CustID CustomerName

100 2/23/2014 1234 3/1/2014 1234 John Smith200 2/24/2014 6773 4/1/2014 6773 Bertie Wooster300 2/22/2014 1234 3/15/2014 1234 John Smith

400 2/27/2014 2555 3/16/2014 2555 Jane Doe

500 2/12/2014 8989 2/22/2014

600 2/23/2014 2555 2/27/2014 2555 Jane Doe

700 2/15/2014 2555 4/1/2014 2555 Jane Doe

Page 33: While you are waiting for class to start...

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’)

FROM ord1LEFT OUTER JOIN custON ord1.custID = cust.custID

Open a new query window and type the following query:

Page 34: While you are waiting for class to start...

All rows from both tables!

SELECT *FROM ord1FULL OUTER JOIN custON ord1.custID = cust.custID

Page 35: While you are waiting for class to start...

Ord Cust

Results of Outer JoinResults of Outer JoinResults of Both-Sided Outer Join

Page 36: While you are waiting for class to start...

Order ID OrderDate CustID DueDate C.CustID CustomerName100 2/23/2014 1234 3/1/2014 1234 John Smith200 2/24/2014 6773 4/1/2014 6773 Bertie Wooster300 2/22/2014 1234 3/15/2014 1234 John Smith400 2/27/2014 2555 3/16/2014 2555 Jane Doe500 2/12/2014 8989 2/22/2014600 2/23/2014 2555 2/27/2014 2555 Jane Doe700 2/15/2014 2555 4/1/2014 2555 Jane Doe

8372 Martin Cheng

Page 37: While you are waiting for class to start...

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 ID OrderDate CustID CustomerName

100 2/23/2014 1234 John Smith

200 2/24/2014 6773 Bertie Wooster

300 2/22/2014 1234 John Smith

400 2/27/2014 2555 Jane Doe

500 2/12/2014 8989 Missing Name

600 2/23/2014 2555 Jane Doe

700 2/15/2014 2555 Jane Doe

Page 38: While you are waiting for class to start...

Write a new queryWrite 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 ID OrderDate DueDate

400 2/27/2014 3/16/2014

600 2/23/2014 2/27/2014

700 2/15/2014 4/1/2014

Page 39: While you are waiting for class to start...

Write another queryWrite 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 ID OrderDate CustID DueDate C.CustID CustomerName

500 2/12/2014 8989 2/22/2014 n/a Missing Name

Page 40: While you are waiting for class to start...

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.

CustID CustomerName CountofOrders

1234 John Smith 2

2555 Jane Doe 3

6773 Bertie Wooster 1

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.

CustID CustomerName CountofOrders

1234 John Smith 2

2555 Jane Doe 3

6773 Bertie Wooster 1

8372 Martin Cheng 0


Recommended