Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Programming Talk / Other - Database design question

Author
Message
Phaelax
DBPro Master
21
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 8th Dec 2014 03:35
I'm making a kind of address book thingy and seem to have confused myself on the best way of organizing the tables. At the moment, I have two tables, Contact and Number.

Here's a basic representation of my tables:


The reason a contact's number are stored in a separate table is because each contact may have 0 or more numbers associated with it.

Is this a good design? Cause the way I'm looking at it now, I have to hit the database with an extra call for each contact. One query to get a list of all contacts, then I'll have to loop through each contact to do an additional query to get their numbers. Seems inefficient to me. Is there a better way to query for the results?

I tried something like this:


But not only does that return a lot of redundant data (repeating the contact info for each number belonging to the same person) but it also won't return any contacts if they don't have any numbers associated.


"I like offending people, because I think people who get offended should be offended." - Linus Torvalds
Dar13
15
Years of Service
User Offline
Joined: 12th May 2008
Location: Microsoft VisualStudio 2010 Professional
Posted: 8th Dec 2014 04:59
If a contact can have multiple numbers, then this way is probably the only way. Perhaps you could use foreign keys in the numbers tables to directly refer to the contacts table instead of using generic IDs. I'm not the best at database design myself.

spooky
21
Years of Service
User Offline
Joined: 30th Aug 2002
Location: United Kingdom
Posted: 9th Dec 2014 22:35
Look at LEFT OUTER JOIN. It will return all contacts whether there are any associated numbers. The number columns will just return as NULL where that cid does not exist for a given contact.

Something like;



Boo!

Login to post a reply

Server time is: 2024-04-19 15:44:03
Your offset time is: 2024-04-19 15:44:03