views:

107

answers:

3

Hello there.

I am developing a clothes web application and would appreciate advice on how to structure the data in my mysql database.

Every product (item of clothing) will be photograped in a number of ways, let's call them 'modes'. For example a shirt would be photographed buttoned or unbuttoned, and/or tucked in/not tucked in. A pair of trousers would have a different set of possible attributes. I want to store information on the way these items are photographed so I can later use that information to display the item of clothing in particular way.

So one method would be just to store all the possible attributes in a single table, something like:

productId (FK,PK) modeId (PK) isLoose isTuckedIn Size HasSmthUnderneath

Where the attributes could be a value or a code defined in another table or NULL if it does not apply to a particular mode.

Then given a particular productId and modeId, I imagine I could filter out the NULL values for attributes which do not apply and use only the relevant ones.

However, I am not sure if that is the ideal way to store this kind of values as I would have alot of NULL values, for example in a pair of trousers which are only photographed in one way. I've heard of the EAV model, is this appropriate?

It's probably worth noting that the number of attributes will be decided by me and not the user and should not change considerably; and that my end goal is to extract the attributes of a particular mode so I can use that data in my application.

Sorry if anything is unclear!

A: 

I think your design is fine. It would be possible to apply database normalization to it, which may give you the following designs alternatively:

  1. have one table per property, each with (id, propvalue) pairs. Only add rows into these tables for items where the property actually applies.
  2. have generic tables (id, propname, propvalue), perhaps one such table per property datatype (boolean, number, string).

With your description, I feel that either is overkill. The only exception would be cases were properties are multi-valued (e.g. list of available colors)

Martin v. Löwis
A: 

I personally think plain old key/value pairs for this type of thing are underrated, so if you're happy to control it more in the application itself you could also do something like this:

create table ProductStates
(
  ProductId int PK
  ModeState nvarchar(200) PK
)

Nice and simple in my mind. You get no redundant null values; if the product has that mode then there's a row, if not there's no row. Also means no schema changes required if there's a new state. If you wanted to you could have ModeState instead link out to a ModeStates lookup table, if you think integrity is going to be a problem.

create table ProductStates
(
  ProductId int PK
  ModeStateId int PK
)

create table ModeStates
(
  ModeStateId int PK
  ModeStateDescription nvarchar(500)
  (...whatever else you might need here)
)

... though that's probably redundant.

Just an alternative, not sure if I'd do it that way myself (depends on the brief(s)). Did I get the specification right?

Gavin Schultz-Ohkubo
+2  A: 

I would be tempted to have the following normalized schema design

Mode Table

id | mode_style
---------------
1  | buttoned
2  | unbuttoned
3  | tucked in
4  | untucked

Clothes Table

id | name      | description
----------------------------
1  | shirt     | mans shirt...
2  | dress     | short sleeve

Clothes_mm_Mode Table (Junction/Map table)

mode_id | clothes_id
--------------------
1       | 1
1       | 2
3       | 3

Then you can easily query those clothes that have an unbuttoned display

SELECT
    c.id,
    c.name,
    c.description
FROM
    Clothes c
INNER JOIN
    Clothes_Mode cm
    ON c.id = cm.clothes_id
WHERE
    cm.mode_id = 2

If certain types of clothes are always displayed in the same way i.e. all shirts always have a buttoned and unbuttoned display, you could take out the Clothes_mm_Mode Table and introduce a Common Mode table that maps Modes to a Common Mode id

Common_Modes Table

id | name           | description
--------------------------------------------------
1  | Men's Shirt    | Common Modes for a Mens shirt
2  | Women's Shirt  | Common Modes for a Womens shirt

Common_Modes_mm_Mode Table (Junction/Map table)

common_mode_id | mode_id
--------------------------------------------------
1              | 1
1              | 2
2              | 1
2              | 2

and then associate each item of Clothing with a Common Mode type

Clothing_Common_Modes Table

clothing_id | common_mode_id
----------------------------
1           | 1

The advantage of this design would be that when adding a new item of clothing, only one record need be entered into the Common Modes table to associate that item of clothing with the Modes common to the clothing type. Of course this could be handled without a common modes table by having a procedure that inserts the appropriate records into the original Clothes_mm_Mode Table for a new item of clothing, but by having the relationship in the database, it will be more prominent, visible and easier to maintain.

Russ Cam