Table of Contents
Previous Section Next Section

Setting up the Catalog

Before you even start to think about data tables or any database-specific tasks, you need to clearly understand what you want to achieve. The way you design the product catalog and its data structures depends (as usual) on the client's requirements. For this case study, we'll cheat a little bit and suppose the client wants such a product catalog that allows just enough (but not too many) features to explore in this chapter.

So, what kind of product catalog does your client want? Well, the requirements for the structure of the product catalog are simple enough:

The relationships between products, categories, and departments are as follows:

For example, your store will have a Full Costumes department, which will contain a number of categories, such as Exotic Costumes and Scary Costumes. The product catalog will contain products such as Cleopatra Kit and Vampire Disguise. If you were to build an e-commerce store based on your database, the information in these tables might be used to generate a product catalog looking something like Figure 14-1.

Click To expand
Figure 14-1: The e-commerce store
Tip 

Figure 14-1 shows the e-commerce store created in Beginning ASP.NET E-Commerce, Second Edition (Apress, 2003).

You can see the list of departments listed in the left side of the page. Underneath, you can see the list of categories that belong to the currently selected department (which in this case is Jokes and Tricks). The rest of the page is filled with products.

When a department is selected, the page is filled with the products that are on promotion for that department, and its list of categories also appears. When a category is then selected, the page is populated with all the products that belong to that category.

You know a product can also be on catalog promotion. Such a product will be listed in the front page of the site.

To store this information, you'll need at least three data tables: a Department table, a Category table, and a Product table. To decide how to implement these tables and their relationships, let's first try to visualize them using some simple diagrams.

We decided earlier that a department can contain multiple categories, but a category always belongs to a single department. This is a one-to-many relationship, and it can be imagined as in Figure 14-2 (one department, many categories).

Click To expand
Figure 14-2: The relationship between the Department and Category tables

Remember that you implement one-to-many relationships by adding a FOREIGN KEY constraint to the table in the many side of the relationship, which references the primary key of the other table. In this case, you'd implement the relationship like in Figure 14-3.

Click To expand
Figure 14-3: Implementing the one-to-many relationship

Both the Department and Category tables contain two properties: Name and Description. For both tables, Name is obligatory, and Description accepts NULLs. The Category table also contains a column named DepartmentID, which references the DepartmentID column in Department.

Between products and categories, you have a different kind of relationship because a product can belong to (relate to) many categories, and also a category can contain (relate to) many products. Figure 14-4 represents how categories and products are related.

Click To expand
Figure 14-4: The relationship between the Category and Product tables

Yep, this looks like a many-to-many relationship. You'll recall that many-to-many relationships are physically implemented using a third table, called a junction table (often referred to as a mapping table). In this case, the junction table will be named ProductCategory.

Let's take a look at the complete database diagram now. Notice the two one-to-many relationships between the junction table (ProductCategory) and the Category and Product tables in Figure 14-5.

Click To expand
Figure 14-5: The four database tables

ProductCategory has a primary key made from two fields, ProductID and CategoryID (remember that you can't have more than one primary key in a table!). Each record of this table will associate a product with a category. Because the ProductID/CategoryID pair forms the primary key, you're not allowed to have identical entries in this table. In other words, each row in this table uniquely associates one product with one category.

The Product table has more fields than the other tables. The following summarizes each of the fields in the table:

Using fields such as OnCatalogPromotion and OnDepartmentPromotion is one of the ways of tracking which products are featured on a site and where they're featured. For example, in an e-commerce Web site, on the front page of the site you'd see the products that have the OnCatalogPromotion bit set to one. When the visitor is selecting one of the departments, only the products that have the OnDepartmentPromotion bit set to one appear. Finally, when a category is selected from one of the departments, all the products in that category are listed.


Table of Contents
Previous Section Next Section