Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

The Database Forum


You are currently viewing our The Database Forum as a guest. Please register to participate.
Login



Reply
Mapping table question
Old 06-23-2008, 12:52 PM Mapping table question
drew22299's Avatar
Skilled Talker

Posts: 93
Trades: 0
Hi,

This may be a stupid question but I can't work out how to get data from two tables when there is a joining table between them I know how to do joins, left joins etc but I'm having a bit of a mental block with this one, and I'm guessing it's an obvious question.

If I have tables called people, organisation and the joining table is called organisationPeople what would the query be to retrieve data from both? I queried the joining table but only got two attributes returned since that is what is stored in the joining table (personId and organisationId)

Any help will be appreciated!
drew22299 is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 06-23-2008, 01:55 PM Re: Mapping table question
nyef's Avatar
Ultra Talker

Posts: 265
Name: Lucas
Trades: 0
Something like:

Code:
select people.*,organisation.* 
from people P, organisation O, organisationPeople OP 
where P.id=OP.peopleid and O.id=OP.organisationid
__________________
~nyef

Please login or register to view this content. Registration is FREE
nyef is offline
Reply With Quote
View Public Profile Visit nyef's homepage!
 
Old 06-23-2008, 02:09 PM Re: Mapping table question
drew22299's Avatar
Skilled Talker

Posts: 93
Trades: 0
Quote:
select people.*,organisation.*
from people P, organisation O, organisationPeople OP
where P.id=OP.peopleid and O.id=OP.organisationid
Thanks again nyef, so basically you need a query that SELECTS everything from people and organisation WHERE the ID in people (P) and in organisation = the Id in the mapping table.

It took me by surprise when I realised I didn't know how to manipulate data in a mapped table because I thought I could do all that just with joining tables, and I haven't needed to use more than a few tables (without mapped tables) up until now.

Is the only advantage to having a mapped table being able to define many to many relationships or are there other advantages? What does roll up mean?

Thanks,
drew22299 is offline
Reply With Quote
View Public Profile
 
Old 06-23-2008, 02:28 PM Re: Mapping table question
Learning Newbie's Avatar
Defies a Status

Latest Blog Post:
Astounding Republican Paranoia
Posts: 5,662
Name: John Alexander
Trades: 0
Your actual data is still persisted in your people and organizations tables, so you still need to query them. The third table simply tells you what people go with what organizations.
__________________

Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE
Learning Newbie is offline
Reply With Quote
View Public Profile
 
Old 06-23-2008, 06:51 PM Re: Mapping table question
drew22299's Avatar
Skilled Talker

Posts: 93
Trades: 0
If I was to INSERT a new person into the people table for a specifc organisation (already listed in the organisation table) would I need to to insert the person data into the people table and the organisationPeople table (mapping table)

If this is the case, how can I INSERT the personId into the mapping table before it has been generated in the people table since the personId is auto_increment? Would I need to first INSERT the person data, and then select * from people table and get the personId and then INSERT the personId in the mapping table?

Thanks,
drew22299 is offline
Reply With Quote
View Public Profile
 
Old 06-24-2008, 02:24 PM Re: Mapping table question
Learning Newbie's Avatar
Defies a Status

Latest Blog Post:
Astounding Republican Paranoia
Posts: 5,662
Name: John Alexander
Trades: 0
Sounds like you've got it. You should also be using DRI. You shouldn't be able to add a record to your mapping table that doesn't exist in both of the tables it maps together. In other words, why would you allow a mapping between people who don't exist?
__________________

Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE
Learning Newbie is offline
Reply With Quote
View Public Profile
 
Old 06-24-2008, 03:12 PM Re: Mapping table question
drew22299's Avatar
Skilled Talker

Posts: 93
Trades: 0
What is a DRI?

When I populate the people table do I need to populate the mapping table between the people table and the organisation table? (each person belongs to an organisation)

Is the mapping table supposed to get updated automatically?

Since the personId is auto_increment, I don't understand how you can insert the personId into the mapping table at the same time it was inserted into the people table.
drew22299 is offline
Reply With Quote
View Public Profile
 
Old 06-24-2008, 03:46 PM Re: Mapping table question
Learning Newbie's Avatar
Defies a Status

Latest Blog Post:
Astounding Republican Paranoia
Posts: 5,662
Name: John Alexander
Trades: 0
Declarative referential integrity is cement holding the relationships between your data together. You would establish a primary key on your person table, say on the PersonID column. Then you would establish a foreign key on your mapping table, so that you can't map a person who doesn't exist. This prevents data corruption.

Your mapping table is exactly like any other table in any database. If you want a record to go into it, you have to code that through an insert query. Think about it. Your person table holds information about people. Your organization table holds information about organizations. Your person_in_an_organization table holds information about what people belong to what organizations. Your database server won't know that automatically, so you have to tell it. The fact that person #294 is in organization #19 is just like any other piece of data that you would interact with.

If you're using an identity sequence to generate ID values, you'll need to use an identity function to fetch the value that gets generated in your person table when you add a row to your mapping table. You'll probably want whatever is MySQL's version of Scope_Identity() because you may need to use triggers if your version of MySQL doesn't support DRI.
__________________

Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE
Learning Newbie is offline
Reply With Quote
View Public Profile
 
Old 06-24-2008, 05:14 PM Re: Mapping table question
drew22299's Avatar
Skilled Talker

Posts: 93
Trades: 0
Thanks for the detailed answer
drew22299 is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Mapping table question
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.85463 seconds with 12 queries