Datasheet

252
The OrderDetails table is as follows:
OrderDetailsID OrderID Order Item Quantity Item Cost
1 1 Widget 10 3.50
2 1 Doodad 5 2.95
3 2 Thingy 1 15.98
4 3 Widget 1 3.50
5 4 Doodad 2 2.95
6 4 Thingamajig 1 8.50
We now have three tables that can be linked together by their ID fields as shown in Figure 8-1:
Figure 8-1
We now have links between the tables. The
CustomerID field in the Orders table is used to identify
which customer the order is for. Similarly, the
OrderID field in the OrderDetails table identifies which
order a particular order line belongs to.
The unique key in a table is defined as its Primary Key – it's what uniquely defines a row. When used in
another table it is called the Foreign Key, so called because it's a key, but one to a foreign table. The
foreign key is simply a column that is the primary key in another table. Because the values of the
primary key and the foreign key will be the same, we can use them to link the tables together. This
linking of the tables is done in Structured Query Language (SQL), usually as a query or a stored procedure.
SQL and Stored Procedures
Queries are the way in which we deal with data in a database, either to extract data or to manipulate it.
We can use an SQL statement or a stored procedure, which is an SQL statement wrapped to provide a
simple name. It's worth to note that a stored procedure is actually more than just wrapping an SQL
statement in a name, but that's a good enough description for what we need.
Chapter 8
57076_Ch 8 SAN.qxd 01/12/2003 6:43 PM Page 252