Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Lab 4: Displaying Data from Multiple Tables CISB224 02A, 02B Semester I, 2009/2010 College of Information Technology, Universiti Tenaga Nasional 1."— Presentation transcript:

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

2 Introduction to Joins College of Information Technology, Universiti Tenaga Nasional 2 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.columnis the condition that joins (or relates)  Table2.column the table together

3 Introduction to Joins College of Information Technology, Universiti Tenaga Nasional 3 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.columnis the condition that joins (or relates)  Table2.column the table together

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

5 Introduction to Joins – cont. College of Information Technology, Universiti Tenaga Nasional 5 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).

6 Introduction to Joins – cont. College of Information Technology, Universiti Tenaga Nasional 6 Example 1 PaymentID, SELECTO.ID, CustID, PaymentID, CONVERT(char(10), TotalPrice, 1) AS TotalPrice OrderInfo O JOIN PaymentType ON OrderInfo.PaymentID= PaymentType.ID FROMOrderInfo 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).

7 Exercise 1. Execute the command: SP_HELP OrderInfo 2. Find out the foreign keys in the OrderInfo table. College of Information Technology, Universiti Tenaga Nasional7 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

8 Joining More Than Two Tables College of Information Technology, Universiti Tenaga Nasional 8 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.

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

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

11 Qualifying Ambiguous Columns – cont. College of Information Technology, Universiti Tenaga Nasional 11 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.

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

13 Using Table Aliases College of Information Technology, Universiti Tenaga Nasional 13 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.

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

15 Joining a Table to Itself College of Information Technology, Universiti Tenaga Nasional 15 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.

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

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

18 Inner Joins College of Information Technology, Universiti Tenaga Nasional 18 JOIN 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.


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

Similar presentations


Ads by Google