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 > Data Access, Manipulation & Batch Languages > ANSI SQL > Selecting across a many to many relationship

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 09-28-04, 15:19
nik101 nik101 is offline
Registered User
 
Join Date: Sep 2004
Posts: 6
Selecting across a many to many relationship

hi, I'm very new to sql, and figure this should be a common question, but I've had little luck getting anywhere with tutorials etc.

I've three tables representing a many to many relationship, so let's say (using arbitary names);

create table people (
name varchar(70) not null primary key,
age integer)

create table tasks (
job varchar(100) not null primary key,
pay integer)

create table many2manylink (
index_PK integer not null primary key,
name varchar(70),
job varchar(100),
foreign key name references people(name),
foreign key job reference tasks(job))

How would I select all the ages of people who are getting paid a certain amount?

I was trying to hazard a guess by
select people.age from people inner join many2manylink on many2manylink.name = people name inner join tasks where tasks.pay = 500

but it wasn't getting me very far, and I was beginning to just take stabs in the dark.

Can anyone help break through this mental block?

thanks,
nik

ps Also, do the foreign keys play an important part in the selection process, or just to ensure that no unique fields are broken on data entry?
Reply With Quote
  #2 (permalink)  
Old 09-28-04, 19:29
r937 r937 is online now
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
Quote:
Originally Posted by nik101
How would I select all the ages of people who are getting paid a certain amount?
you were quite close!!
Code:
select people.name 
     , people.age 
  from tasks
inner 
  join many2manylink 
    on tasks.job
     = many2manylink.job
inner 
  join people
    on many2manylink.name
     = people.name
 where tasks.pay = 500
note i added name to the SELECT list, because a result set of just the ages doesn't seem all that useful

Quote:
ps Also, do the foreign keys play an important part in the selection process, or just to ensure that no unique fields are broken on data entry?
foreign keys ensure that you cannot assign a job to a person that doesn't exist, nor give a person a job that doesn't exist

note in your many2manylink table, you should remove the index_PK column, and insert at the end:
Code:
 , primary key (name,job)
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 09-29-04, 04:13
nik101 nik101 is offline
Registered User
 
Join Date: Sep 2004
Posts: 6
Many many thanks! I will really be able to progress now.

nik

ps The indenting in your SQL does make it lot clearer to read, I hadn't thought of applying that! Why I dropped my developer hat just because it was SQL I don't know....
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