Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
WidgetMan
Starting Member
1 Post |
Posted - 2009-07-28 : 18:27:54
|
I'm having a heck of a time trying to figure out the best table structure for a site I'm working on.The site will be selling products that are highly customizable - we'll call them widgets. Each widget can have one or more colors, varying widths and lengths, and one or more accessories. Pricing for the widgets depends on these factors as well as the quantity ordered. So it's not as simple as a table with product - quantity - price.What's the best way to store all of this information? Should each element be separated into its own table or should there just be a generic "options" table that can store all the different elements of each widget? Or is there a better way?Thanks! |
|
ScottWhigham
Starting Member
49 Posts |
Posted - 2009-09-03 : 08:52:55
|
I would favor creating separate tables for each attribute provided that there are known number of attributes. I might even create them in their own schema if there are a bunch of them:CREATE SCHEMA AttributesGOCREATE TABLE Attributes.Color (ColorId INT NOT NULL IDENTITY(1,1) PRIMARY KEY, Color_Hex VARCHAR(6))GOINSERT Attributes.Color VALUES ('FFF'), ('000000')GOCREATE TABLE Product (ProductId INT NOT NULL IDENTITY(1,1) PRIMARY KEY, Name NVARCHAR(256) NOT NULL)GOCREATE TABLE ProductColor (ProductId INT NOT NULL, ColorId INT NOT NULL)GOALTER TABLE ProductColor ADD CONSTRAINT PK_ProductColor PRIMARY KEY (ProductId, ColorId)GOALTER TABLE ProductColor ADD CONSTRAINT FK_ProductColor_ProductFOREIGN KEY(ProductId) REFERENCES Product(ProductId)GOALTER TABLE ProductColor ADD CONSTRAINT FK_ProductColor_ColorsFOREIGN KEY(ColorId) REFERENCES Attributes.Color(ColorId)========================================================I have about 1,000 video tutorials on SQL Server 2008, 2005, and 2000 over at http://www.learnitfirst.com/Database-Professionals.aspx |
|
|
|
|
|
|
|