473,385 Members | 1,780 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

What to use for primary key of a many-to-many table?

Hi.

What is the usual and what are the possible fields to use for the primary
key of an intersecting table of a many-to-many relationship?

I would think the typical, most common fields would be to set the
intersecting table to have its own unique primary key. Right?

OR

It was suggested using the 2 foreign keys from the two other tables.
Comments?

OR

How about including a date/time field in the primary key for history
tracking ?

Thanks....
Please email me too if possible.

Fred
Nov 12 '05 #1
4 9127
I usually use a regular good old regular autonumber?

There is no need for anything else...is there?
--
Albert D. Kallal (MVP)
Edmonton, Alberta Canada
ka****@msn.com
http://www.attcanada.net/~kallal.msn
=
Nov 12 '05 #2
fg***************@yahoo.com (Fred) wrote in
<bj************@ID-76446.news.uni-berlin.de>:
What is the usual and what are the possible fields to use for the
primary key of an intersecting table of a many-to-many
relationship?

I would think the typical, most common fields would be to set the
intersecting table to have its own unique primary key. Right?

OR

It was suggested using the 2 foreign keys from the two other
tables. Comments?

OR

How about including a date/time field in the primary key for
history tracking ?


Well, it all depends.

To me, the most common PK for a many-to-many join table would be
your option 2: a unique index on the two foreign keys.

I would only use an Autonumber surrogate key (your option 1) when I
needed to store the join table's PK as foreign key in another
table. This happens suprisingly often.

Your option 3 has nothing to do with determining the primary key,
and really is only about what other attributes the join between the
two tables happens to have in your data schema. If you were
thinking that a date/time field along with the two foreign keys
might give you a PK, I'd say you probably have a schema design
error. The real structure you should use is to have the join with
no attributes of its own, and then have the multiple date/time
records in a table attached to the join table.

I've had the kind of structure in many apps, where a join table
establishes a relationship between two entities and then a table
related to that join records the history of that relationship over
time, with multiple attributes.

In that case, I'd give the join table it's own AutoNumber surrogate
primary key and store that AutoNumber in the related history table.
I would, however, retain the unique index on the two foreign keys
in the join table -- even though they no longer function as PK in
the schema, they should still be unique.

--
David W. Fenton http://www.bway.net/~dfenton
dfenton at bway dot net http://www.bway.net/~dfassoc
Nov 12 '05 #3
For me, it depends.

If the junction table is further involved as the one in a one to many, then
I'll add an autonumber as the primary key (but also put a unique index on
the combination of the 2 foreign keys).

If it's not involved, then I'll just set the 2 foreign keys as the primary
key.

--
Joan Wild
"Fred" <fg***************@yahoo.com> wrote in message
news:bj************@ID-76446.news.uni-berlin.de...
Hi.

What is the usual and what are the possible fields to use for the primary
key of an intersecting table of a many-to-many relationship?

I would think the typical, most common fields would be to set the
intersecting table to have its own unique primary key. Right?

OR

It was suggested using the 2 foreign keys from the two other tables.
Comments?

OR

How about including a date/time field in the primary key for history
tracking ?

Thanks....
Please email me too if possible.

Fred

Nov 12 '05 #4
RE/
I would think the typical, most common fields would be to set the
intersecting table to have its own unique primary key. Right?

OR

It was suggested using the 2 foreign keys from the two other tables.
Comments?

OR

How about including a date/time field in the primary key for history
tracking ?


Not having any extra personal processing power to spare, when I create a table I
create a (usually autonumbered) numeric field as the PK. Period. Just a plain
old dumb number.

Hasn't failed me yet and the few times I haven't done it I've wished I did.

OTOH when I look at some of the multi-field joins some people have to do to get
the same results I get really confused really fast.
-----------------------
PeteCresswell
Nov 12 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: t53864 | last post by:
I need to reorder the primary key of a table. Say, Primary Key (field1, field2) changed to: Primary Key (field2, field1) Would like to know if this creates any problem to the database?
29
by: asj | last post by:
Can you guess what this is? http://www.blueboard.com/phone/apache_sept_2003.gif It's a history of the IIs "Titanic", which is being slowly and painfully sunk by the open source Apache web...
2
by: arijitchatterjee123 | last post by:
Dear All, I am new in MS Sql Server.I am facing a error "'PRIMARY' filegroup is full".I went through the all previous submitions.But still this error is coming.Please help me out. I have...
11
by: Geoff Jones | last post by:
Hi Can anybody help me with the following problem? I have a datasource i.e. some data, which I'm accessing via some VB. This I have done; with the help of you guys. However, the original data,...
6
by: Emma Middlebrook | last post by:
Hi there, I have created an XmlSchema to represent the tables in my database and I have specified a primary key for each of the tables. However, to be able to execute the following code: ...
9
by: Steven C. | last post by:
Hello: I'm getting an error, "primary key not defined" when trying to use the FIND method on the DataTable Rows collection. I have a typed dataset called 'MortgagesDS' that I created with the...
115
by: LurfysMa | last post by:
Most of the reference books recommend autonum primary keys, but the Access help says that any unique keys will work. What are the tradeoffs? I have several tables that have unique fields. Can...
3
by: sandy | last post by:
I cannot figure this out. I have the following code: <code> Private: /****************************************************************************** CreateList
6
by: Lawrence Spector | last post by:
I ran into a problem using g++. Visual Studio 2005 never complained about this, but with g++ I ran into this error. I can't figure out if I've done something wrong or if this is a compiler bug. ...
2
by: Kvara prasad | last post by:
For creating a user database three important files named primary, secondary & transaction log files. so my question is 'what is the difference between primary & secondary files?' and is 'what is the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.