A better schema

A better schema

Now that we’re done with that little digression, we’ll get back to the structure for this application. Figure 10-9 shows the preferred schema, the one that we actually use.

cat alog_admin

username passw ord

cat egories

product s

cat egory_id

st yles

cat egory descript ion

product _id

st yle_id

cat egory_id

product _id

product

st yle

descript ion

descript ion

price

price

image_src

image_src

subst yles

subst yle_id

st yle_subst yle_map

cat egory_id subst yle

st yle_id

price

subst yle_id,

descript ion

price descript ion image_src

Figure 10 - 9 : Catalog database schema

260 Part IV: Not So Simple Applications

For starters, notice that there is a direct relationship between the categories and the substyles tables. The reason for this was explained earlier. The tables in the top half of the figure should make sense. They are the part of the hierarchy that still made sense.

The neat and different thing in this application is the style_substyle_map table. If you are going through this book in order, this will be the first time that you encounter a many-to-many relationship. In this application a category can have many substyles (e.g., t-shirts come in S, M, L, and XL). And any style of shirt (e.g, “

I Love Milk” in red) can come in zero, one, or more than one substyle (size). The style_substyle_map tracks that intersection. Figure 10-10 illustrates how the data come together.

sub- st yle_id

sub- st yle

st yle_id

1 cat egory_ID

cat egory

1 1 t - shirt s

1 2 shoes

4 XL

product _id

product

cat egory_id

1 Just Say Oops

2 I Love M ilk

st yle_id

st yle

product _id

subst yle_id

st yle_id 1 1 3 1 1 2 2 2

Figure 10 - 10 : Sample data

In Figure 10-10 the “Just Say Oops” t-shirt in blue comes in two sizes: S and M. Listing 10-1 shows the statements that will create these tables.

Chapter 10: Catalog 261

Listing 10 - 1 : Create Statements for Catalog Application

# -------------------------------------------------------- # # Table structure for table ‘catalog_admin’ # CREATE TABLE catalog_admin (

username varchar(50) NOT NULL, password varchar(255) NOT NULL

); # -------------------------------------------------------- # # Table structure for table ‘categories’ # CREATE TABLE categories (

category_id int(11) NOT NULL auto_increment, category varchar(255) NOT NULL, description text, PRIMARY KEY (category_id)

); # -------------------------------------------------------- # # Table structure for table ‘products’ CREATE TABLE products (

product_id int(11) NOT NULL auto_increment, category_id int(11) NOT NULL, product varchar(255) NOT NULL, description text, price decimal(10,2), image_src varchar(255), PRIMARY KEY (product_id), KEY product_category_id (category_id)

); # -------------------------------------------------------- # # Table structure for table ‘styles’ # CREATE TABLE styles (

style_id int(11) NOT NULL auto_increment, product_id int(11) NOT NULL, style varchar(255) NOT NULL, description text, price decimal(10,2), image_src varchar(255), PRIMARY KEY (style_id), KEY style_product_key (product_id)

262 Part IV: Not So Simple Applications

); # -------------------------------------------------------- # # Table structure for table ‘substyles’ # CREATE TABLE substyles (

substyle_id int(11) NOT NULL auto_increment, category_id int(11) NOT NULL, substyle varchar(255) NOT NULL, price decimal(10,2), description text, PRIMARY KEY (substyle_id), KEY substyle_category_key (category_id)

); # -------------------------------------------------------- # # Table structure for table ‘style_substyle_map’ # CREATE TABLE style_substyle_map ( style_id int(11) NOT NULL, substyle_id int(11) NOT NULL, price decimal(10,2), description text, image_src varchar(255), KEY map_style_key (style_id), KEY map_substyle_key (substyle_id) );