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 > Foreign Key Referencing more than 1 table?

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 02-16-04, 13:35
serogole serogole is offline
Registered User
 
Join Date: Feb 2004
Posts: 6
Foreign Key Referencing more than 1 table?

I have an address table, and employee and customer tables. I am thinking of adding a faddress_owner field to the address table and use it as a foreign key referencing both the customer_no and the employee_no number. I have just done that on mysql, but I do not feel that confident about it - is this acceptable? what are the pros and cons? and how else can I handle it?
Best Regards
Reply With Quote
  #2 (permalink)  
Old 02-16-04, 14:50
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
see the thread Multiple Possible Parents

or see this article: Supertypes and Subtypes

your supertype is "entities which have addresses"

you have two subtypes: customer and employee

each of these subtype tables should have the supertype id as its primary key, as well as being a foreign key to the supertype table

then your address table can have a foreign key to the supertype table too
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book

Last edited by r937; 02-16-04 at 14:53.
Reply With Quote
  #3 (permalink)  
Old 02-16-04, 15:18
serogole serogole is offline
Registered User
 
Join Date: Feb 2004
Posts: 6
Thanks for the reply...
Maybe I am a little slow - the tables look like this:

create table customer (cust_No int not null, blah, blah, primary key(cust_no));

create table employee (emp_No int not null, blah, blah, primary key(emp_No));

create table address(addr_No int not null, addr_Owner int not null, blah, blah, primary key(addr_No), index(addr_Owner), Foreign Key(addr_Owner) references customer(cust_No), Foreign Key(addr_Owner) references employee(emp_No));

Am I on the right track?
Thanks again for your time and help!

Quote:
Originally posted by r937
see the thread Multiple Possible Parents

or see this article: Supertypes and Subtypes

your supertype is "entities which have addresses"

you have two subtypes: customer and employee

each of these subtype tables should have the supertype id as its primary key, as well as being a foreign key to the supertype table

then your address table can have a foreign key to the supertype table too
Reply With Quote
  #4 (permalink)  
Old 02-16-04, 15:28
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
no, not the right track

a foreign key can reference only one primary key

you cannot have

, Foreign Key(addr_Owner) references customer(cust_No)
, Foreign Key(addr_Owner) references employee(emp_No)

in the address table
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #5 (permalink)  
Old 02-16-04, 15:39
serogole serogole is offline
Registered User
 
Join Date: Feb 2004
Posts: 6
Hi

From reading the other post, am I right to think that I can have the address table have cust_no and emp_no fields, i.e.

create table address(addr_No int not null, cust_no int not null, emp_no int not null, blah, blah, primary key(addr_No),
index(cust_No), index(emp_No),
Foreign Key(cust_No) references customer(cust_No),
Foreign Key(emp_No) references employee(emp_No));

Thanks again!
Reply With Quote
  #6 (permalink)  
Old 02-16-04, 16:32
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
yes you can do that

i wouldn't want to be around when you have to rewrite your code to accommodate a third entity having an address...

__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #7 (permalink)  
Old 02-16-04, 16:41
serogole serogole is offline
Registered User
 
Join Date: Feb 2004
Posts: 6
I agree, I also would not want to be there
my 1st design was using link tables:-
Customer_address, Employee_Address, etc to get around this, then I started thinking about the address_owner field. I still am not sure why it is not acceptable to have address_owner foreign key link to both customer and employee tables - I obviously need to spend more time on this forum and read more on dbs..
Best Regards

Quote:
Originally posted by r937
yes you can do that

i wouldn't want to be around when you have to rewrite your code to accommodate a third entity having an address...

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