Previous | Table of Contents | Next |
Relational database systems get their name from the relationships between tables. To exploit these relationships, you need a way to draw information from more than one table. To draw information from more than one table, you use table joins.
Table joins are the topic of the day. Understanding joins is central to your understanding of T-SQL, so todays lesson is about as important a lesson as there is in this book. These are the topics that are covered today:
Starting with MS SQL Server 6.5, there are two different ways to specify a join: the ANSI-92 compliant way and the T-SQL way. Because both are now T-SQL syntax, from now on I call them ANSI style and old style. Throughout this chapter, I provide two queries that do the same thing: the first using old style, the second using ANSI style. Ill talk a little later about the differences between these two styles.
To join two tables together, the tables must have a relationship. This relationship is indicated by a common key.
A common key, also called a join key, is the column (or columns) shared by two tables that allows them to be meaningfully joined together.
Usually, this relationship is logically defined, not physically defined. This means that the server doesnt know anything about the relationships between tables, and you need to specify those relationships when you submit your query. Its time for an example.
Both the sales table and the titles table contain a column called title_id (see Figure 7.1). Column title_id is the common key shared by these two tables. Figure 7.1 shows how the server reads each table and joins together rows sharing a common key (title_id).
Figure 7.1. Rows from titles are joined to rows from the sales table.
Here is the query that performs a join, as shown in Figure 7.1. (Ive converted the title to a char(20) so that it will fit on the page):
/* OLD STYLE */ select convert(char(20), titles.title) Title, sales.ord_date, sales.qty from titles, sales where titles.title_id = sales.title_id /* ANSI STYLE */ select convert(char(20), titles.title) Title, sales.ord_date, sales.qty from titles inner join sales on titles.title_id = sales.sales_id
Title ord_date qty ------------- ---------------- ------ The Busy Executives Sep 14 1994 12:00AM 5 The Busy Executives Sep 14 1994 12:00AM 10 Cooking with Compute Mar 11 1993 12:00AM 25 You Can Combat Compu Feb 21 1993 12:00AM 35 Straight Talk About Oct 28 1993 12:00AM 15 Silicon Valley Gastr Dec 12 1993 12:00AM 10 The Gourmet Microwav Sep 14 1994 12:00AM 25 The Gourmet Microwav Sep 14 1994 12:00AM 15 But Is It User Frien May 22 1993 12:00AM 30 Secrets of Silicon V May 24 1993 12:00AM 50 Computer Phobic AND May 29 1993 12:00AM 20 Is Anger the Enemy? Sep 13 1994 12:00AM 3 Is Anger the Enemy? Sep 13 1994 12:00AM 75 Is Anger the Enemy? Sep 14 1994 12:00AM 10 Is Anger the Enemy? Sep 14 1994 12:00AM 20 Life Without Fear May 29 1993 12:00AM 25 Prolonged Data Depri May 29 1993 12:00AM 15 Emotional Security: May 29 1993 12:00AM 25 Onions, Leeks, and G Jun 15 1992 12:00AM 40 Fifty Years in Bucki Jun 15 1992 12:00AM 20 Sushi, Anyone? Jun 15 1992 12:00AM 20 (21 row(s) affected)
In the SELECT list, columns are identified by specifying <table name>.<column name>, as in sales.qty. The JOIN clauses are specified either in the FROM clause (ANSI) or in the WHERE clause (old). (I explain more about syntax in the next section of this chapter.)
In the titles table, the title_id uniquely identifies each row in the table. In the sales table, each book order has a title_id associated with it to indicate which book was ordered. There may be zero corresponding rows in sales, as in the case of book MC3026 (The Psychology of...); there may be one corresponding row, as in the case of books BU1111 (Cooking with Computers) and BU2075 (Combat Computer Stress); or there may be many corresponding rows, as with BU1032 (Busy Executives...). Lets look at what happens in each of those three cases:
Previous | Table of Contents | Next |