If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > General > Database Concepts & Design > handling size, color, quantity in db design

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 01-23-04, 10:51
tomasz tomasz is offline
Registered User
 
Join Date: Jan 2004
Posts: 9
handling size, color, quantity in db design

I'm setting up a database for a school project, and I'm wondering how to handle sizes, colors, and quantity (OnHand).

Currently, in my Products table, I have:

ProductId, ProductName, Category, Description, Cost, Price, OnHand, and Supplier, as field. I'm a little confused though, because, one may have 22 of product x as one row, but that doesn't tell you what sizes and colours are amongst the 22 products of that type.

I guess a bad way would be to have a record for each product x of a certain colour and size, but that seems pretty bad.

How does one handle this?

Thanks.

Tomasz
Reply With Quote
  #2 (permalink)  
Old 01-23-04, 11:06
andrewst andrewst is offline
Moderator.
 
Join Date: Sep 2002
Location: UK
Posts: 5,171
Re: handling size, color, quantity in db design

Either attributes such as colour and size should be part of the Product, with different product IDs for each, or you need a separate table e.g. Product_Variant that has this information linked to the Product.
__________________
Tony Andrews
http://tinyurl.com/tonyandrews
Reply With Quote
  #3 (permalink)  
Old 01-23-04, 11:35
certus certus is offline
Registered User
 
Join Date: Dec 2003
Location: Canada
Posts: 710
The least of your worries is the number of rows you generate.

Yes, break them all down by size, color and quantity on hand.

Color(ColorID, ColorName)

Size(SizeID, SizeMeasure)

Product(ProductID, ..., OnHand, ColorID, SizeID)
__________________
visit: relationary
Reply With Quote
  #4 (permalink)  
Old 01-23-04, 13:20
tomasz tomasz is offline
Registered User
 
Join Date: Jan 2004
Posts: 9
Re: handling size, color, quantity in db design

Quote:
Originally posted by andrewst
Either attributes such as colour and size should be part of the Product, with different product IDs for each, or you need a separate table e.g. Product_Variant that has this information linked to the Product.
Thanks very much.
Reply With Quote
  #5 (permalink)  
Old 01-23-04, 13:28
tomasz tomasz is offline
Registered User
 
Join Date: Jan 2004
Posts: 9
Quote:
Originally posted by certus
The least of your worries is the number of rows you generate.

Yes, break them all down by size, color and quantity on hand.

Color(ColorID, ColorName)

Size(SizeID, SizeMeasure)

Product(ProductID, ..., OnHand, ColorID, SizeID)
Thanks. That is quite clear, but I'm wondering about one thing. If there is a ProductID 12345 that has different size, color variants, then the ProductID is no longer unique (currently I'm using it as primary key). What I mean is: there may be a ProductID 12345 with 10 in medium, red, and ProductID 12345 with 5 in small, black.

I guess, then, I should be using ProductNumber where I'm currently using ProductId, and then have ProductId unique?

Is this right? Excuse my ignorance. I'm learning...

Tomasz.
Reply With Quote
  #6 (permalink)  
Old 01-23-04, 15:03
certus certus is offline
Registered User
 
Join Date: Dec 2003
Location: Canada
Posts: 710
You would create a unique constraint on product number, size and color and use Product ID as a surrogate key.
__________________
visit: relationary
Reply With Quote
  #7 (permalink)  
Old 01-23-04, 17:16
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
certus, perhaps you meant to say use another field as a surrogate key

like, an autonumber

you can't "use ProductID as a surrogate key" because chances are, it has values like 'XHV203' and '0045YY'

you just automatically assumed ProductID was a meaningless number, didn't you



__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #8 (permalink)  
Old 01-23-04, 18:30
tomasz tomasz is offline
Registered User
 
Join Date: Jan 2004
Posts: 9
Quote:
Originally posted by r937
certus, perhaps you meant to say use another field as a surrogate key

like, an autonumber

you can't "use ProductID as a surrogate key" because chances are, it has values like 'XHV203' and '0045YY'

you just automatically assumed ProductID was a meaningless number, didn't you



Okay. so I use ProductNumber (autonumber) for key, and then have as many records as there are variants of ProductID 12345?

So, if my Products table schema was:
ProductNumber, ProductID, ProductName, Color, Size, Cost, Price, OnHand, then my records would look like this:

1, 12345, My Widget, Red, S, 99.00 , 125.00, 33
2, 12345, My Widget, Red, M, 99.00 , 125.00, 22
3, 12345, My Widget, Blue, S, 99.00 , 125.00, 10
4, 12345, My Widget, Blue, M, 99.00 , 125.00, 19

Is that the way? By the way, what exactly is a "surrogate key". Is it simply a primary key that is not from the business data, but created to provide a primary key?

Thank you all for helping this newbie.

Tomasz
Reply With Quote
  #9 (permalink)  
Old 01-23-04, 19:04
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
tomasz, that is right, your design is good, and yes, a surrogate key is "not from business data" -- a key from business data, like ProductID, is called a natural key

it is not necessary that the surrogate key be the primary key, but that is by far the most common application
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #10 (permalink)  
Old 01-23-04, 22:10
tomasz tomasz is offline
Registered User
 
Join Date: Jan 2004
Posts: 9
Quote:
Originally posted by r937
tomasz, that is right, your design is good, and yes, a surrogate key is "not from business data" -- a key from business data, like ProductID, is called a natural key

it is not necessary that the surrogate key be the primary key, but that is by far the most common application
Thanks Rudy. I will set up this way. Just wondering, though, back a bit in this thread certus said:

Quote:
The least of your worries is the number of rows you generate.

Yes, break them all down by size, color and quantity on hand.

Color(ColorID, ColorName)

Size(SizeID, SizeMeasure)

Product(ProductID, ..., OnHand, ColorID, SizeID)

What would be the benefit of seperate tables for Color and Size, with
foreign keys to those tables in the Products table? Just wondering.

Thanks again.

Tomasz.
Reply With Quote
  #11 (permalink)  
Old 01-23-04, 22:34
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
the benefit of those separate color and size tables is that they hold the definitions that the main product table "links to" with colorID and sizeID

this means you don't put the color name and size measures into the product table, and this in turn means that if you wanted to change the name of "fuschia" to "flaming purple" for example, you can do it in one place, and all products which have that color will, when they are printed out, show that new color name

tables like that, consisting of an ID and a description, are very often called "lookup" tables
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #12 (permalink)  
Old 01-23-04, 23:32
tomasz tomasz is offline
Registered User
 
Join Date: Jan 2004
Posts: 9
Quote:
Originally posted by r937
the benefit of those separate color and size tables is that they hold the definitions that the main product table "links to" with colorID and sizeID

this means you don't put the color name and size measures into the product table, and this in turn means that if you wanted to change the name of "fuschia" to "flaming purple" for example, you can do it in one place, and all products which have that color will, when they are printed out, show that new color name

tables like that, consisting of an ID and a description, are very often called "lookup" tables
So, lookup tables are not a necessity but a convenience? Do you think lookup tables are a better idea, rather than putting color name and size name in the product table? What is your opinion? Do it, don't do it? This is an exercise I'm doing for Coldfusion programming class, so I'm trying to model real world 'best practices'. I guess it might depend on the number of products a business has, how many variants, etc.

Thanks again.

Tomasz.
Reply With Quote
  #13 (permalink)  
Old 01-24-04, 02:11
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
definitely, do it
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #14 (permalink)  
Old 01-24-04, 13:07
tomasz tomasz is offline
Registered User
 
Join Date: Jan 2004
Posts: 9
many thanks helpers

Quote:
Originally posted by r937
definitely, do it
Thanks, I did. Works like a charm. Many thanks Rudy and all.

Tomasz.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On