Planning and Creating the Database Tables

Before you tackle the process of creating database tables for a store, think about how you shop in real life. When you walk into a store, items are ordered in some fashion: The hardware and the baby clothes aren't mixed together, the electronics and the laundry detergent aren't side by side, and so on. Applying that knowledge to database normalization, already you know that you will need a table to hold categories and a table to hold items. These items will each belong to one category.

Next, think about the items themselves. Depending on the type of store you have, your items may or may not have colors, and may or may not have sizes. But all your items will have a name, a description, and a price. Again, thinking in terms of normalization, you know that you will have one general items table and two additional tables that relate to the general items table.

Table 20.1 shows sample table and field names to use for your online storefront. In a minute, you'll create the actual SQL statements, but first you should look at this information and try to see the relationships appear. Ask yourself which of the fields should be primary or unique keys.

Table 20.1. Storefront Table and Field Names

Table Name

Field Names

store_categories

id, cat_title, cat_desc

store_items

id, cat_id, item_title, item_price, item_desc, item_image

store_item_size

item_ id, item_size

store_item_color

item_id, item_color

As you can see in the following SQL statements, the store_categories table has two fields besides the id field: cat_title and cat_desc, for title and description. The id field is the primary key, and cat_title is a unique field because there's no reason you would have two identical categories.

 mysql> create table store_categories (     -> id int not null primary key auto_increment,     -> cat_title varchar (50) unique,     -> cat_desc text     -> ); Query OK, 0 rows affected (0.03 sec) 

The store_items table has five fields besides the id field, none of which are unique keys. The lengths specified in the field definitions are arbitrary; you should use whatever best fits your store. The cat_id field relates the item to a particular category in the store_categories table. This field is not unique because you will want more than one item in each category. The item_title, item_price, and item_desc (for description) fields are self-explanatory. The item_image field in this case will hold a filename in this case, the file is assumed to be local to your server which you will use to build an HTML <IMG> tag when it's time to display your item information.

 mysql> create table store_items (     -> id int  not null primary key auto_increment,     -> cat_id int not null,     -> item_title varchar (75),     -> item_price float (8,2),     -> item_desc text,     -> item_image varchar (50)     -> ); Query OK, 0 rows affected (0.00 sec) 

Both the store_item_size and store_item_color tables contain optional information: If you sell books, they won't have sizes or colors, but if you sell shirts, they will. For each of these tables, no keys are involved because you can associate as many colors and sizes with a particular item as you want.

 mysql> create table store_item_size (     -> item_id int not null,     -> item_size varchar (25)     -> ); Query OK, 0 rows affected (0.00 sec) 
 mysql> create table store_item_color (     -> item_id int not null,     -> item_color varchar (25)     -> ); Query OK, 0 rows affected (0.00 sec) 

These are all the tables necessary for a basic storefront that is, for displaying the items you have for sale. Hour 21, "Creating a Shopping Cart Mechanism," integrates the user experience into the mix. For now, just concentrate on your inventory.

In Hour 19, "Creating an Online Address Book," you learned how to use PHP forms and scripts to add or delete records in your tables. If you apply the same principles to this set of tables, you can easily create an administrative front end to your storefront. We won't go through that process in this book, but feel free to do it on your own. At this point, you know enough about PHP and MySQL to complete the tasks.

For now, simply issue MySQL queries via the MySQL monitor or other interface, to add information to your tables. Following are some examples, if you want to follow along with sample data.

Inserting Records into the store_categories Table

The following queries create three categories in your store_categories table: hats, shirts, and books.

 mysql> insert into store_categories values     -> ('1', 'Hats', 'Funky hats in all shapes and sizes!'); Query OK, 1 row affected (0.01 sec) mysql> insert into store_categories values ('2', 'Shirts', 'From t-shirts to sweatshirts to polo shirts and beyond, we have them all.'); Query OK, 1 row affected (0.00 sec) mysql> insert into store_categories values ('3', 'Books', 'Paperback, hardback, books for school and books for play, you name it, we have it.'); Query OK, 1 row affected (0.00 sec) 

In the next section, we'll add some items to the categories.

Inserting Records into the store_items Table

The following queries add three item records to each category. Feel free to add many more.

 mysql> insert into store_items values ('1', '1', 'Baseball Hat', '12.00', 'Fancy, low-profile baseball hat.', 'baseballhat.gif'); Query OK, 1 row affected (0.00 sec) mysql> insert into store_items values ('2', '1', 'Cowboy Hat', '52.00', '10 gallon variety', 'cowboyhat.gif'); Query OK, 1 row affected (0.01 sec) mysql> insert into store_items values ('3', '1', 'Top Hat', '102.00', 'Good for costumes.', 'tophat.gif'); Query OK, 1 row affected (0.00 sec) mysql> insert into store_items values ('4', '2', 'Short-Sleeved T-Shirt', '12.00', '100% cotton, pre-shrunk.', 'sstshirt.gif'); Query OK, 1 row affected (0.00 sec) mysql> insert into store_items values ('5', '2', 'Long-Sleeved T-Shirt', '15.00', 'Just like the short-sleeved shirt, with longer sleeves.', 'lstshirt.gif'); Query OK, 1 row affected (0.00 sec) mysql> insert into store_items values ('6', '2', 'Sweatshirt', '22.00', 'Heavy and warm.', 'sweatshirt.gif'); Query OK, 1 row affected (0.00 sec) mysql> insert into store_items values ('7', '3', 'Jane\'s Self-Help Book', '12.00', 'Jane gives advice.', 'selfhelpbook.gif'); Query OK, 1 row affected (0.00 sec) mysql> insert into store_items values ('8', '3', 'Generic Academic Book', '35.00', 'Some required reading for school, will put you to sleep.', 'boringbook.gif'); Query OK, 1 row affected (0.00 sec) mysql> insert into store_items values ('9', '3', 'Chicago Manual of Style', '9.99', 'Good for copywriters.', 'chicagostyle.gif'); Query OK, 1 row affected (0.00 sec) 

Inserting Records into the store_item_size Table

The following queries associate sizes with one of the three items in the shirts category and a generic "one size fits all" size to each of the hats (assume they're strange hats). On your own, insert the same set of size associations for the remaining items in the shirts category.

 mysql> insert into store_item_size values (1, 'One Size Fits All'); Query OK, 1 row affected (0.00 sec) mysql> insert into store_item_size values (2, 'One Size Fits All'); Query OK, 1 row affected (0.00 sec) mysql> insert into store_item_size values (3, 'One Size Fits All'); Query OK, 1 row affected (0.00 sec) mysql> insert into store_item_size values (4, 'S'); Query OK, 1 row affected (0.00 sec) mysql> insert into store_item_size values (4, 'M'); Query OK, 1 row affected (0.00 sec) mysql> insert into store_item_size values (4, 'L'); Query OK, 1 row affected (0.00 sec) mysql> insert into store_item_size values (4, 'XL'); Query OK, 1 row affected (0.00 sec) 

Inserting Records into the store_item_color Table

The following queries associate colors with one of the three items in the shirts category. On your own, insert color records for the remaining shirts and hats.

 mysql> insert into store_item_color values (1, 'red'); Query OK, 1 row affected (0.00 sec) mysql> insert into store_item_color values (1, 'black'); Query OK, 1 row affected (0.00 sec) mysql> insert into store_item_color values (1, 'blue'); Query OK, 1 row affected (0.00 sec) 


Sams Teach Yourself PHP, MySQL and Apache in 24 Hours
Sams Teach Yourself PHP, MySQL and Apache in 24 Hours
ISBN: 067232489X
EAN: 2147483647
Year: 2005
Pages: 263

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net