views:

102

answers:

5

Hi all,

I am trying to make a fashion boutique site. In this site each product (t-shirt,jeans etc) belongs to a collection. Each collection has looks (t-shirt,jean,accessories). A product can belong to one collection and to multiple looks. How should I design the database?

+1  A: 

Put a 'collection' field in your Product table. It will be a foreign key to your Collection table (which might just be an ID and a Name.)

Can a look have many products? If so, use a many-to-many relationship (intermediary table) to model that.

Edit: You will start with 3 main tables: Products, Collections, Looks

You will then need joining tables: Look-Products, Collection-Looks

Joe Philllips
Yes a look can have many products and a collection can have many looks but the product that belongs to a group can be on each own in a collection
chchrist
A: 

I would start with tables similar to these

product
----------
product_id
description

collection
----------
collection_id
name

look
----------
look_id
name

collection_look
---------------
collection_id
look_id

product_collection_look
---------------
product_id (fk1)
collection_id (fk2)
look_id (fk2)
Randy
A product can belong to a collection on each own. Should I enter a collection_id in the product table?
chchrist
i would not. to me, normalizing would say leave products as a standalone list, and then make link tables to categorizations.
Randy
each product can only have one name, make it a standalone list too? with a link to the product? no, it is just a column on the Product. The same applies to the product's collection, there is only one, so just put it on the product, it is a waste to make a new table for it.
KM
perhaps. I find that this type of thing usually evolves. For instance - at some point you want to manage the fall collection or the spring collection, and oops the product is already in one... much better to have this seperation already done, and add some dates if you need to, then no more rewriting of your apps because you had a limited view of how things should work. and no, this is not the same as the name nor the pk_id.
Randy
A: 

try something like this:

Products
ProductID    int auto increment PK
ProductName  string
CollectionID FK to Collections
...more columns if necessary...

Collections
CollectionID   int auto increment PK
CollectionName string
...more columns if necessary...

Looks
LookID        int auto increment PK
LookName      string
...more columns if necessary...

CollectionLooks
CollectionID  composite PK,FK to Collections
LookID        composite PK, Fk to Looks

ProductLooks
ProductID     composite PK, FK to Products
LookID        composite PK, Fk to Looks
KM
A: 

Here is one idea. I used SQL Server syntax for illustration. It was not completely clear whether a collection could have one look or many looks. I assumed many looks below, but it would really easy to change that. Again, this is just one idea using relationship tables, but there other equally valid possibilities.

create table Product
(
  ProductId int not null primary key,
  Name varchar(128) not null unique
)

create table Look
(
  LookId int not null primary key,
  Name varchar(128) not null unique
)

create table Collection
(
  CollectionId not null primary key,
  Name varchar(128) not null unique
  LookId int not null references Look (LookId)
)

create table CollectionLook
(
  SurrogateId int not null primary key,
  CollectionId int not null references Collection (CollectionId),
  LookId int not null references Look (LookId),
  constraint CollectionLookConstraint unique (CollectionId, LookId)  
  -- Change the above constraint if a collection can only have one look.
)

create table ProductCollection
(
  SurrogateId int not null primary key,
  ProductId int not null references Product (ProductId),
  CollectionId int references Collection (CollectionId),
  constraint ProductConstraint unqiue (ProductId)
)

create table ProductLook
(
  SurrogateId int not null primary key,
  ProductId int not null references Product (ProductId),
  LookId int not null references Look (LookId),
  constraint ProductLookConstraint unique (ProductId, LookId)
)
Brian Gideon
A: 

Given that:

  • A Collection contains Products, and Products "have" Looks.
  • A Collection also "has" Looks

and making no assumptions, I see two big questions.

Can a Collection have (be assigned) Looks, but contain no Products that have been assigned those Looks?

If true (a Collection can have Looks but no Products with those Looks), then the "multiple tables" models everyone's posted so far would work: tables Collection, Product [FK to Collection], Looks, CollectionLooks, ProductLooks.

If false (a collection cannot have Looks unless at least one of it's Products has those Looks), then you'd have to toss the CollectionLooks table, giving you a simple hierarchical structure. Whenever you needed to determine a Collection's Looks, you'd have to "query through" from Collection to Products to ProductLooks. If performance were somehow a problem, you could denormalize and set up the CollectionLooks table, but keeping it accurate would be awkward at best.

Can a Collection contain Products with a given Look, but not itself be assigned that Look?

If true (Collection A contains Product P with Look Z, but Collection A itself does not have Look Z), then you'd have to use the multiple tables model, to keep what has which Looks straight.

If false (If Collection A contains Product P with Look Z, then Collection A must have Look Z), then the hierarchical model is again most approrpiate.

Philip Kelley