User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Database Design section within the Web Development category of DaniWeb, a massive community of 375,238 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,123 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Database Design advertiser:
Views: 429 | Replies: 2
Reply
Join Date: Feb 2008
Posts: 5
Reputation: im.thatoneguy is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
im.thatoneguy im.thatoneguy is offline Offline
Newbie Poster

Uniqueness Dependent on another Column?

  #1  
Feb 8th, 2008
I have a database with let's say two tables as follows:

Products Table

ProductID | ProductName

and

Widgets Table
WidgetID | ProductID (Foreign Key) | Position


Now how do I set it up if I want a variable number of widgets that attach to a product. But I want to ensure uniqueness in the Position Column WHERE ProductID is XYZ.


So I want to be able to have the following entries in Widgets:

0 | 1 | 1
1 | 1 | 2
2 | 2 | 1
3 | 1 | 3

But not:

0 | 1 | 1
1 | 1 | 2
2 | 1 | 1
3 | 1 | 3

I'm using MySQL but I'm open to other databases. Or do I just need to handle this on the application side to check for a previous entry. I don't mind doing this either, but if there is a way to ensure that no duplicates get entered at all costs would be idea.

Thanks!
Gavin
Last edited by im.thatoneguy : Feb 8th, 2008 at 7:15 pm.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Sep 2007
Location: North Bay Ontario
Posts: 174
Reputation: trudge is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 20
trudge trudge is offline Offline
Junior Poster

Re: Uniqueness Dependent on another Column?

  #2  
Feb 10th, 2008
How are you choosing Position? If that is generated by the server, then it is guaranteed to be unique.
Amer Neely - Web Mechanic
"Others make web sites. We make web sites work!"
Reply With Quote  
Join Date: Feb 2008
Posts: 5
Reputation: im.thatoneguy is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
im.thatoneguy im.thatoneguy is offline Offline
Newbie Poster

Re: Uniqueness Dependent on another Column?

  #3  
Feb 10th, 2008
Well how it's actually layed out in my database something sort of like this.

Produt Libraries:

ProductLibrary Table
ProductGUID | ProductName
123 | Product123

ProductAccessoryLibrary Table
AccessoryGUID | ProductGUID | AccessoryName | AccessoryDisplayOrder
85 | 123 | Widget01 | 1
36 | 123 | Widget02 | 2

So when a customer adds 123 (Product123) to their cart they are presented with all of the widget options FROM ProductAccessoryLibrary WHERE ProductGUID = 123

Now they are presented with a list of check boxes.

So in our cart tables we have:

CartProducts:

CartProducts Table
CartProductID | ProductGUID | ProductName
225 | 123 | Product123

CartAccessories Table
CartAccessoryID | CartProductID | AccessoryGUID | Chosen
43 | 225 | 85 | 1
23 | 225 | 36 | 0


And I was thinking I could drop the "Chosen" binary flag if just added Cart Accessories when the checkbox was checked. And then deleted when the checkbox was unchecked. (Since some products could theoretically have 100s of accessories it would save a lot of space.) And of course if the application worked correctly 100% of the time you would never have any orphan accessories since the application should update the cart to the state it was before (with a checkbox checked). And I could add an application call to ensure that when someone checks a checkbox there actually isn't an entry for that CartProductID/AccesorryGUID already. But all of that is dependant on "should work"s.

If I could define in the CART Accessories Table that I only want only one unique entry between CartProductID and AccessoryGUID and it kicked up an error if that rule was ever violated it would be one more line of defense to ensure I don't have any problems. Especially during development when lots of interdependencies might go unnoticed if the application isn't working perfectly.

If I didn't operate on a Drop/Insert tracking system and stuck to a "visible boolean" the worst case scenario would be something like this some how getting created by some software glitch:
CartAccessories Table
CartAccessoryID | CartProductID | AccessoryGUID | Chosen
43 | 225 | 85 | 1
43 | 225 | 85 | 0
23 | 225 | 36 | 0

Then again maybe I'm just being paranoid.
Last edited by im.thatoneguy : Feb 10th, 2008 at 4:24 pm.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb Database Design Marketplace
Thread Tools Display Modes

Other Threads in the Database Design Forum

All times are GMT -4. The time now is 4:24 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC