![]() |
Data Joins |
|
Introduction |
|
When studying relationships, we reviewed techniques of making data from one table available to the records of another table. This demonstrated to reduce data duplication and mistakes. Another issue that involves the combination of tables consists of creating records from more than one table and making the result into a single list. This is the basis of data joins. A data join is a technique of creating a list of records from more that one table, using all columns from all tables involved, or selecting only the desired columns from one or all of the tables involved. This means that a data join is essentially created in three steps: |
Before creating a join, you must have the tables that would be involved. The tables are created using the techniques we have seen in previous lessons. It is also important to create a primary key for each table. The parent table would usually need only this primary key that would be used to "link" it to a child table. Here is an example of such a table:
If needed, you can then create the necessary records for the table. Here is an example:
When creating the child table, remember to create a column that would serve as the link to the parent table. By a (good) habit as we saw when studying relationships, the name and the data type of this column are the same as the primary key of the parent table. Here is an example of a child table that would be joined to the above parent table:
Once again, if necessary, you can add the needed records to the table. Here is an example:
Equipped with the necessary tables and their columns, you can create the join. To do this in the Microsoft SQL Server Management Studio, you can right-click one of the tables involved and click Open Table. This would display the Table window. You should then display the Diagram and the SQL sections. Because the foundation of a join lies on at least two tables, you should add one. To do this
Any of these actions would display the Add Table dialog box. To select a table:
Alternatively, instead of using Add Table, you can drag the child table from the Object Explorer and drop it in the Diagram section. Here is an example of two tables that have been added:
Remember that you can drag the title bars of the tables to move them and position them to your liking. After selecting the table(s), on the Add Table dialog box, you can click Close. If a relationship was already established between the tables, a joining line would show it. In SQL code, the basic formula to create a join is: SELECT WhatColumn(s) FROM ChildTable TypeOfJoin ParentTable ON Condition The ChildTable factor specifies the table that holds the records that will be retrieved. It can be represented as follows: SELECT WhatColumn(s) FROM Persons TypeOfJoin ParentTable ON Condition The ParentTable factor specifies the table that holds the column with the primary key that will control what records, related to the child table, that will display. This factor would be represented as follows: SELECT WhatColumn(s) FROM Persons TypeOfJoin Sexes ON Persons.SexID = Sexes.SexID The Condition factor is a logical expression used to validate the records that will be isolated. To create the condition, you should assign the primary key column of the parent table to the foreign key column of the child table. Because both columns likely have the same name, to distinguish them, their names should be qualified. This would be done as follows: SELECT WhatColumn(s) FROM Persons TypeOfJoin Sexes ON Persons.SexID = Sexes.SexID The WhatColumn(s) factor of our formula allows you to make a list of the columns you want to include in your statement. As you should be aware, you can include all columns by using the * operator. Here is an example: SELECT *
FROM Persons
TypeOfJoin Sexes
ON Persons.SexID = Sexes.SexID
In this case, all columns from all tables would be included in the result. Instead of all columns, you may want a restricted list. In this case, create the list after the SELECT keyword separating them with commas. You can use the name of a column normally if that name is not duplicated in more than one column. Here is an example: SELECT LastName, FirstName, Sex FROM Persons TypeOfJoin Sexes ON Persons.SexID = Sexes.SexID If the same name of a column is found in more than one table, as is the case for a primary-foreign key combination, you should qualify the name of the column by preceding it with the name of its parent table followed by a period. Here are examples: SELECT LastName, FirstName, Persons.SexID,
Sexes.SexID, Sex
FROM Persons
TypeOfJoin Sexes
ON Persons.SexID = Sexes.SexID
In fact, to make your code easier to read, you should qualify the name of each column of your SELECT statement. Here are examples: SELECT Persons.LastName, Persons.FirstName, Persons.SexID,
Sexes.SexID, Sexes.Sex
FROM Persons
TypeOfJoin Sexes
ON Persons.SexID = Sexes.SexID
|
Published on Sunday 10 February 2008
|
|
||
| Home | Copyright © 2008-2010 FunctionX, Inc. | Next |
|
|
||