Keys |
First, let's discuss the concept of keys>. A primary key is a
column or set of columns that uniquely idenifies the rest of the data in any
given row. For example, in the AntiqueOwners table, the OwnerID column uniquely
identifies that row. This means two things: no two rows can have the same
OwnerID, and, even if two owners have the same first and last names, the OwnerID
column ensures that the two owners will not be confused with each other, because
the unique OwnerID column will be used throughout the database to track the
owners, rather than the names.
A foreign key is a column in a table where that column is a primary
key of another table, which means that any data in a foreign key column must
have corresponding data in the other table where that column is the primary key.
In DBMS-speak, this correspondence is known as referential integrity. For
example, in the Antiques table, both the BuyerID and SellerID are foreign keys
to the primary key of the AntiqueOwners table (OwnerID; for purposes of
argument, one has to be an Antique Owner before one can buy or sell any items),
as, in both tables, the ID rows are used to identify the owners or buyers and
sellers, and that the OwnerID is the primary key of the AntiqueOwners table. In
other words, all of this "ID" data is used to refer to the owners, buyers, or
sellers of antiques, themselves, without having to use the actual names.
Performing a Join
The purpose of these keys is so that data can be related across
tables, without having to repeat data in every table--this is the power of
relational databases. For example, you can find the names of those who bought a
chair without having to list the full name of the buyer in the Antiques
table...you can get the name by relating those who bought a chair with the names
in the AntiqueOwners table through the use of the OwnerID, which relates
the data in the two tables. To find the names of those who bought a chair, use
the following query:
SELECT OWNERLASTNAME, OWNERFIRSTNAME
FROM ANTIQUEOWNERS, ANTIQUES
WHERE BUYERID = OWNERID AND ITEM = 'Chair';
Note the following about this query...notice that both tables involved in the
relation are listed in the FROM clause of the statement. In the WHERE clause,
first notice that the ITEM = 'Chair' part restricts the listing to those who
have bought (and in this example, thereby owns) a chair. Secondly, notice how
the ID columns are related from one table to the next by use of the BUYERID =
OWNERID clause. Only where ID's match across tables and the item purchased is a
chair (because of the AND), will the names from the AntiqueOwners table be
listed. Because the joining condition used an equal sign, this join is called an
equi join. The result of this query is two names: Smith, Bob & Fowler,
Sam.
Dot notation refers to prefixing the table names to column names, to
avoid ambiguity, as such:
SELECT ANTIQUEOWNERS.OWNERLASTNAME, ANTIQUEOWNERS.OWNERFIRSTNAME
FROM ANTIQUEOWNERS, ANTIQUES
WHERE ANTIQUES.BUYERID = ANTIQUEOWNERS.OWNERID AND ANTIQUES.ITEM = 'Chair';
As the column names are different in each table, however, this wasn't
necessary.
DISTINCT and Eliminating Duplicates
Let's say that you want to list the ID and names of only those people
who have sold an antique. Obviously, you want a list where each seller is only
listed once--you don't want to know how many antiques a person sold, just the
fact that this person sold one (for counts, see the Aggregate Function section
below). This means that you will need to tell SQL to eliminate duplicate sales
rows, and just list each person only once. To do this, use the DISTINCT keyword.
First, we will need an equijoin to the AntiqueOwners table to get the detail
data of the person's LastName and FirstName. However, keep in mind that since
the SellerID column in the Antiques table is a foreign key to the AntiqueOwners
table, a seller will only be listed if there is a row in the AntiqueOwners table
listing the ID and names. We also want to eliminate multiple occurences of the
SellerID in our listing, so we use DISTINCT on the column where the
repeats may occur.
To throw in one more twist, we will also want the list alphabetized by
LastName, then by FirstName (on a LastName tie), then by OwnerID (on a LastName
and FirstName tie). Thus, we will use the ORDER BY clause:
>SELECT DISTINCT SELLERID, OWNERLASTNAME, OWNERFIRSTNAME
FROM ANTIQUES, ANTIQUEOWNERS
WHERE SELLERID = OWNERID
ORDER BY OWNERLASTNAME, OWNERFIRSTNAME, OWNERID
In this example, since everyone has sold an item, we will get a listing of
all of the owners, in alphabetical order by last name. For future reference (and
in case anyone asks), this type of join is considered to be in the category of
inner joins.
|