You use SQL joins to perform operations and extract data from relational databases. As such, an understanding of relational database design is a prerequisite to successfully using joins. Relational databases are sets of tables that each store parts of a complete data set; these parts relate to each other, and thus are termed "relational". The underlying principle here is that data should be organized so that it never has to be repeated (stored more than once), while at the same time keeping it in small, manageable sets. NOTE Databases that are not relational are often referred to as being "flat." Some examples will help you understand this concept:
When you're planning relational databases, there is no real right or wrong design, and there is always more than one way to lay out the tables. The following are some of the issues that you need to consider when planning relational table design:
NOTE Primary keys, which were introduced in Chapter 44, "Basic SQL," are columns within tables that uniquely identify every row within those tables. Primary keys are an important part of relational database design because they are the values that connect tables to each other. NOTE A full discussion of relational database design is beyond the scope of this chapter and book. For an explanation of relational databases from a ColdFusion perspective, see ColdFusion MX 7 Web Application Construction Kit (ISBN: 0-321-22367-5). Another important type of key used in relational databases is the foreign key. It is a column within a table whose value is that of another table's primary key. For example, an orders table might use the order number as a primary key, in which case the order number stored in the related order items table is a foreign key. Unlike primary keys, foreign keys need not be unique, and they can be updated as needed. Like primary keys, they always have values (and should not be NULL). |