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 > One to one tables a waste of time?

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 02-19-06, 14:23
Ginger The Cat Ginger The Cat is offline
Registered User
 
Join Date: Feb 2006
Location: Liverpool, UK
Posts: 6
One to one tables a waste of time?

I am modelling shops and their adverts. Every shop is unique and every shop has 1 advert associated with it. Its a 1 to 1 relationship.


I had come up with the following tables (simplified version)

table shops
id
name
location
advert_id

table adverts
id
type of advert
text of advert

When I got round to coding it I have to choose a shop, look up its advert_id, go to the advert table and access the data. But as it is 1 to 1 the shopid and the advertid are always the same so I'm wondering what is the point of having advert_id as a key in my shops table. Why not just use shop_id. But if I'm doing that why have an advert table at all. Should I just put the advert type and text in the shop table.

Is the answer that if I need to change the design in the future separate tables would be more flexible?

Mike
Reply With Quote
  #2 (permalink)  
Old 02-19-06, 18:02
r937 r937 is online now
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
Quote:
Originally Posted by Ginger The Cat
Is the answer that if I need to change the design in the future separate tables would be more flexible?
yes



you said "every shop is unique" so i think it unlikely that you will ever have an advert that covers two shops

however, it's not unlikely that you might want to have a shop with a second advert

therefore the relationship should be one-to-many from shop to advert

therefore the foreign key should be in the advert table

remove advert_id from the shop table, and add shop_id to the advert table

i.e. basically switch your one-to-one relationship around, eh
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 02-20-06, 04:40
Ginger The Cat Ginger The Cat is offline
Registered User
 
Join Date: Feb 2006
Location: Liverpool, UK
Posts: 6
It just seems odd having inefficient code just to handle potential design changes in the future. The current requirements are clear, unique shops having 1, only 1 and always 1 advert.

It is possible that in future the requirements might change to require 1 shop to have many adverts but it is also possible that a shopping chain for example may want to standardise on the same advert at each of its shops or standard adverts may be created for use by more than 1 shop e.g. Buy One Get One Free Today Only!

So why should I try and guess the future. Wouldn't it be best to code for today.

Mike
Reply With Quote
  #4 (permalink)  
Old 02-20-06, 05:09
andrewst andrewst is offline
Moderator.
 
Join Date: Sep 2002
Location: UK
Posts: 5,171
If it is really true that "shops [have] 1, only 1 and always 1 advert" (for ever and ever, amen, so help me Codd) then you could certainly merge the two tables - it would serve no purpose to keep them separate.

Quote:
So why should I try and guess the future. Wouldn't it be best to code for today.
This is a cost-benifit analysis issue: how much would it cost you now to prepare for possible future needs? (very little); how much it would it cost to change it later when all sorts of code, reports etc. have been built on the current model? (much more); how likely is the requirement to change?

Of course you can't predict everything that might change, but the idea of a shop having 2 adverts at some point in the future doesn't sound outlandish, does it?
__________________
Tony Andrews
http://tinyurl.com/tonyandrews
Reply With Quote
  #5 (permalink)  
Old 02-20-06, 08:14
Ginger The Cat Ginger The Cat is offline
Registered User
 
Join Date: Feb 2006
Location: Liverpool, UK
Posts: 6
OK. I will ....

Leave it with separate tables.


Thanks for the advice people. Much appreciated.

Mike
Reply With Quote
  #6 (permalink)  
Old 02-20-06, 08:17
r937 r937 is online now
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
if you are going to keep the separate tables, please at least put the foreign key in the right one
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #7 (permalink)  
Old 02-20-06, 08:29
Ginger The Cat Ginger The Cat is offline
Registered User
 
Join Date: Feb 2006
Location: Liverpool, UK
Posts: 6
But I don't know which is the right one.

I suspect in my application that many shops to one advert is slightly more likely than the other way round with many to many being a possibility.


Mike
Reply With Quote
  #8 (permalink)  
Old 02-20-06, 09:50
r937 r937 is online now
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
oh, man, in that case, you really must implement the many-to-many structure right away

__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
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