Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lab 4: Displaying Data from Multiple Tables

Similar presentations


Presentation on theme: "Lab 4: Displaying Data from Multiple Tables"— Presentation transcript:

1 Lab 4: Displaying Data from Multiple Tables
CISB224 02A, 02B Semester I, 2009/2010 College of Information Technology, Universiti Tenaga Nasional

2 Introduction to Joins Syntax SELECT table.column, table.column, table.column… FROM table1, table 2 WHERE table1.column = table2.column Table.column: denotes the table and column from which data is being retrieved Table1.column is the condition that joins (or relates) Table2.column the table together College of Information Technology, Universiti Tenaga Nasional

3 Introduction to Joins Syntax SELECT table.column, table.column, table.column… FROM table1 JOIN table 2 ON table1.column = table2.column Table.column: denotes the table and column from which data is being retrieved Table1.column is the condition that joins (or relates) Table2.column the table together College of Information Technology, Universiti Tenaga Nasional

4 Introduction to Joins SELECT ID, CustID, PaymentID
Suppose you require the following query: SELECT ID, CustID, PaymentID CONVERT(char(10), TotalPrice, 1) AS TotalPrice FROM OrderInfo The payment type are displayed as ID numbers. College of Information Technology, Universiti Tenaga Nasional

5 Introduction to Joins – cont.
But what if you want to display payment description instead of the payment IDs i.e. Cash instead of 1? You’ll need to join the OrderInfo table and the PaymentType table (where the payment description are located). College of Information Technology, Universiti Tenaga Nasional

6 Introduction to Joins – cont.
Example 1 SELECT O.ID, CustID, PaymentID, CONVERT(char(10), TotalPrice, 1) AS TotalPrice FROM OrderInfo O JOIN PaymentType ON OrderInfo.PaymentID= PaymentType.ID [OrderInfo , PaymentType WHERE OrderInfo.PaymentID= PaymentType.ID] Once you have joined OrderInfo and PaymentType, you can use any columns from the two tables. OrderInfo and PaymentType are joined through the use of the PaymentID column in OrderInfo (the foreign key) and the ID column in PaymentType (the referenced primary key). College of Information Technology, Universiti Tenaga Nasional

7 Exercise Execute the command: SP_HELP OrderInfo Find out the foreign keys in the OrderInfo table. The SP_HELP command followed by a table name will display the details of the table such as: the columns in the table, their data types and lengths the table’s primary key the foreign keys in the table and the tables that they reference other tables that reference the table College of Information Technology, Universiti Tenaga Nasional

8 Joining More Than Two Tables
You may also join more than two tables in a query. Just add another JOIN condition. If you are joining two tables, you need one JOIN condition, if you are joining three tables, you need two JOIN conditions, etc. Suppose now you also want to display each product’s supplier by name and not by ID. College of Information Technology, Universiti Tenaga Nasional

9 Joining More Than Two Tables– cont.
Example 2 SELECT ID, Name, WarehouseID, AmountInStock, OrdId, Quantity FROM Product JOIN Inventory ON Product.ID = Inventory.ProductID JOIN OrderDetails ON Product.ID = OrderDetails.ProductID College of Information Technology, Universiti Tenaga Nasional

10 Qualifying Ambiguous Columns
Now, suppose that you require the query in Example 2, but you also want to display the Product IDs from OrderDetails table. SELECT ID, Name, WarehouseID, AmountInStock, OrdId, Quantity,ProductID FROM Product JOIN Inventory ON Product.ID = Inventory.ProductID JOIN OrderDetails ON Product.ID = OrderDetails.ProductID College of Information Technology, Universiti Tenaga Nasional

11 Qualifying Ambiguous Columns – cont.
You will get an error because both Inventory and OrderDetails have a column named ProductID and SQL does not know which one you mean. You need to qualify the ambiguous ProductID column. In this example, you may choose to use either the one in Inventory or the one in OrderDetails . College of Information Technology, Universiti Tenaga Nasional

12 Qualifying Ambiguous Columns – cont.
Example 3 SELECT ID, Name, WarehouseID, AmountInStock, OrdId, Quantity,Inventory.ProductID FROM Product JOIN Inventory ON Product.ID = Inventory.ProductID JOIN OrderDetails ON Product.ID = OrderDetails.ProductID College of Information Technology, Universiti Tenaga Nasional

13 Using Table Aliases When you need to qualify the ambiguous columns in your query, you may find it tiring to type the table names again and again. You can give your tables aliases that are shorter than their actual names. College of Information Technology, Universiti Tenaga Nasional

14 Using Table Aliases – cont.
Example 4 SELECT ID, Name, WarehouseID, AmountInStock, OrdId, Quantity,I.ProductID FROM Product P JOIN Inventory I ON P.ID = I.ProductID JOIN OrderDetails O ON P.ID = O.ProductID College of Information Technology, Universiti Tenaga Nasional

15 Joining a Table to Itself
You may also join a table to itself. By using table aliases, you can make it look as if you are using two different tables instead. In the next example, you want to display the employees as subordinates, and the names of their superiors. College of Information Technology, Universiti Tenaga Nasional

16 Joining a Table to Itself – cont.
Example 5 SELECT worker.name, ‘Reports to’, manager.name FROM emp worker, emp manager WHERE worker.MgrID = manager.ID College of Information Technology, Universiti Tenaga Nasional

17 Additional Search Condition
You may use WHERE clause for your search condition SELECT cust.CompName FROM cust JOIN orderinfo ON cust.ID = orderinfo.CustID WHERE orderinfo.SalesRepID = 'E40001‘ [ FROM cust, orderinfo WHERE cust.ID = orderinfo.CustID AND orderinfo.SalesRepID = 'E40001‘] College of Information Technology, Universiti Tenaga Nasional

18 Inner Joins The inner join is the default when you do not specify the type of join in the join condition i.e. emp worker JOIN emp manager. All the queries in the previous examples use inner joins. The inner join only joins records where the foreign key column have a match in the referenced column, and vice versa. College of Information Technology, Universiti Tenaga Nasional


Download ppt "Lab 4: Displaying Data from Multiple Tables"

Similar presentations


Ads by Google