PC Review


Reply
Thread Tools Rate Thread

Composite-key Primary Keys ==>> Foreign Keys

 
 
Michael
Guest
Posts: n/a
 
      6th Nov 2007
Hi Folks - Not too familiar with using composite keys as a primary key ...
If I have a 2 field composite key in a table, say lastname and firstname, do
I need to have both fields in a related table as foreign keys? Here's the
problem I have ... When I try to relate the lastname field in the
relationships window, no problem. But, when I try to relate the firstname
field, a message displays that says:

A relationship already exists, do you want to create a new relationship? If
I create a new relationship, then the relationships window displays the
relationship to a copy of the original table.

I figure I'm doing something wromg ....

Note: I know having a lastname and firstname is a bad choice for a composite
key. I'm using this as an example of my problem.

Thanks.


--
Michael Laferriere
HomeKey
401-921-2450
www.homekeyinc.com


 
Reply With Quote
 
 
 
 
RoyVidar
Guest
Posts: n/a
 
      6th Nov 2007
Michael wrote:
> Hi Folks - Not too familiar with using composite keys as a primary key ...
> If I have a 2 field composite key in a table, say lastname and firstname, do
> I need to have both fields in a related table as foreign keys? Here's the
> problem I have ... When I try to relate the lastname field in the
> relationships window, no problem. But, when I try to relate the firstname
> field, a message displays that says:
>
> A relationship already exists, do you want to create a new relationship? If
> I create a new relationship, then the relationships window displays the
> relationship to a copy of the original table.
>
> I figure I'm doing something wromg ....
>
> Note: I know having a lastname and firstname is a bad choice for a composite
> key. I'm using this as an example of my problem.
>
> Thanks.


Yes, lastname is a bad candidate for primary key.

If you're using a composite primary key, and you have a referencing
table (child table), you will need either the entire primary key as
foreign key, or you could for instance add a field to the referenced
table (parent table), give it a unique index, and use that for the
relationship (for instance an autonumber).

To "drag-n-drop" relationship in the relationships window, select all
the fields of the referenced table (parent table), then drag-em over to
the referencing table (child table). In the Edit Relationship dialog,
ensure all of the fields listed on the left side, matches a field in the
list for the related table/query.

Or, when you've created a relationship for one field, in the Edit
Relationship dialog, use the dropdowns below the alredy selected
field to select all fields within the composite primary/foreign key.

--
Roy-Vidar
 
Reply With Quote
 
John W. Vinson
Guest
Posts: n/a
 
      6th Nov 2007
On Tue, 6 Nov 2007 17:13:22 -0500, "Michael" <(E-Mail Removed)> wrote:

>Hi Folks - Not too familiar with using composite keys as a primary key ...
>If I have a 2 field composite key in a table, say lastname and firstname, do
>I need to have both fields in a related table as foreign keys?


Yes.

>Here's the
>problem I have ... When I try to relate the lastname field in the
>relationships window, no problem. But, when I try to relate the firstname
>field, a message displays that says:
>
>A relationship already exists, do you want to create a new relationship? If
>I create a new relationship, then the relationships window displays the
>relationship to a copy of the original table.
>
>I figure I'm doing something wromg ....


Be sure that the Primary Key of the "one" side table is in fact a composite,
with the key icon by each field.

Remove all relationships (by selecting the join line or lines and deleting,
not deleting the table icon). Then, drag the first field of the composite key
to the corresponding foreign key field; then do the same with the second (and
third, and tenth if it comes to that). After you have all the lines drawn,
select each line in turn and choose "Enforce Referential Integrity" and, if
desired, cascade updates and/or cascade deletes.

John W. Vinson [MVP]
 
Reply With Quote
 
Michael
Guest
Posts: n/a
 
      6th Nov 2007
Roy - Excellent ... Thanks!!!!



"RoyVidar" <(E-Mail Removed)> wrote in message
news:4730ec0c$0$13724$(E-Mail Removed)...
> Michael wrote:
>> Hi Folks - Not too familiar with using composite keys as a primary key
>> ... If I have a 2 field composite key in a table, say lastname and
>> firstname, do I need to have both fields in a related table as foreign
>> keys? Here's the problem I have ... When I try to relate the lastname
>> field in the relationships window, no problem. But, when I try to relate
>> the firstname field, a message displays that says:
>>
>> A relationship already exists, do you want to create a new relationship?
>> If I create a new relationship, then the relationships window displays
>> the relationship to a copy of the original table.
>>
>> I figure I'm doing something wromg ....
>>
>> Note: I know having a lastname and firstname is a bad choice for a
>> composite key. I'm using this as an example of my problem.
>>
>> Thanks.

>
> Yes, lastname is a bad candidate for primary key.
>
> If you're using a composite primary key, and you have a referencing
> table (child table), you will need either the entire primary key as
> foreign key, or you could for instance add a field to the referenced
> table (parent table), give it a unique index, and use that for the
> relationship (for instance an autonumber).
>
> To "drag-n-drop" relationship in the relationships window, select all
> the fields of the referenced table (parent table), then drag-em over to
> the referencing table (child table). In the Edit Relationship dialog,
> ensure all of the fields listed on the left side, matches a field in the
> list for the related table/query.
>
> Or, when you've created a relationship for one field, in the Edit
> Relationship dialog, use the dropdowns below the alredy selected
> field to select all fields within the composite primary/foreign key.
>
> --
> Roy-Vidar



 
Reply With Quote
 
Michael
Guest
Posts: n/a
 
      7th Nov 2007
John - Thanks ... But, the technique you describe is what creates the
problem for me. What I needed to do is drag the first field of the composite
key onto the first field of the foreign key, then in the relationship
dialog, set all the other fields. So, in essence, I only needed to drag
once.

Michael


"John W. Vinson" <jvinson@STOP_SPAM.WysardOfInfo.com> wrote in message
news:(E-Mail Removed)...
> On Tue, 6 Nov 2007 17:13:22 -0500, "Michael" <(E-Mail Removed)> wrote:
>
>>Hi Folks - Not too familiar with using composite keys as a primary key ...
>>If I have a 2 field composite key in a table, say lastname and firstname,
>>do
>>I need to have both fields in a related table as foreign keys?

>
> Yes.
>
>>Here's the
>>problem I have ... When I try to relate the lastname field in the
>>relationships window, no problem. But, when I try to relate the firstname
>>field, a message displays that says:
>>
>>A relationship already exists, do you want to create a new relationship?
>>If
>>I create a new relationship, then the relationships window displays the
>>relationship to a copy of the original table.
>>
>>I figure I'm doing something wromg ....

>
> Be sure that the Primary Key of the "one" side table is in fact a
> composite,
> with the key icon by each field.
>
> Remove all relationships (by selecting the join line or lines and
> deleting,
> not deleting the table icon). Then, drag the first field of the composite
> key
> to the corresponding foreign key field; then do the same with the second
> (and
> third, and tenth if it comes to that). After you have all the lines
> drawn,
> select each line in turn and choose "Enforce Referential Integrity" and,
> if
> desired, cascade updates and/or cascade deletes.
>
> John W. Vinson [MVP]



 
Reply With Quote
 
Michael
Guest
Posts: n/a
 
      7th Nov 2007
Revision .... If I drag the first field, no problem. When I drag the second
field, I am prompted if I want to edit the relationship. I need to say yes.
Then, in the relationships dialog, I need to ADD the second field to the
list of defined relationships. I get it ..... Thanks.

Michael



"Michael" <(E-Mail Removed)> wrote in message
news:WmhYi.2113$(E-Mail Removed)...
> John - Thanks ... But, the technique you describe is what creates the
> problem for me. What I needed to do is drag the first field of the
> composite key onto the first field of the foreign key, then in the
> relationship dialog, set all the other fields. So, in essence, I only
> needed to drag once.
>
> Michael
>
>
> "John W. Vinson" <jvinson@STOP_SPAM.WysardOfInfo.com> wrote in message
> news:(E-Mail Removed)...
>> On Tue, 6 Nov 2007 17:13:22 -0500, "Michael" <(E-Mail Removed)> wrote:
>>
>>>Hi Folks - Not too familiar with using composite keys as a primary key
>>>...
>>>If I have a 2 field composite key in a table, say lastname and firstname,
>>>do
>>>I need to have both fields in a related table as foreign keys?

>>
>> Yes.
>>
>>>Here's the
>>>problem I have ... When I try to relate the lastname field in the
>>>relationships window, no problem. But, when I try to relate the firstname
>>>field, a message displays that says:
>>>
>>>A relationship already exists, do you want to create a new relationship?
>>>If
>>>I create a new relationship, then the relationships window displays the
>>>relationship to a copy of the original table.
>>>
>>>I figure I'm doing something wromg ....

>>
>> Be sure that the Primary Key of the "one" side table is in fact a
>> composite,
>> with the key icon by each field.
>>
>> Remove all relationships (by selecting the join line or lines and
>> deleting,
>> not deleting the table icon). Then, drag the first field of the composite
>> key
>> to the corresponding foreign key field; then do the same with the second
>> (and
>> third, and tenth if it comes to that). After you have all the lines
>> drawn,
>> select each line in turn and choose "Enforce Referential Integrity" and,
>> if
>> desired, cascade updates and/or cascade deletes.
>>
>> John W. Vinson [MVP]

>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

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 On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
composite keys and foreign keys Fred Microsoft Access Getting Started 3 6th Nov 2008 01:15 AM
Primary Keys and Foreign Keys - Kick me out of the Access club Dazed And Confused Microsoft Access Getting Started 6 3rd Sep 2007 04:43 PM
Re: primary keys and foreign keys John Vinson Microsoft Access Database Table Design 3 3rd Apr 2004 03:33 AM
Re: primary keys and foreign keys Allen Browne Microsoft Access Database Table Design 0 31st Mar 2004 03:45 AM
how do i display primary keys, foreign keys that a table has. raj Microsoft Dot NET 1 8th Jul 2003 04:12 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 03:27 PM.