473,385 Members | 1,766 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.

Composite Primary Key

I am a bit confused in creating a composite primary key. I have three table
with two of the tables containing primary keys. I have two of the tables
(each with a primary key) having one to many relations with the table
containing no primary key. Do I just create two primary keys on the table
that does not contain any primary key for this to become a composite primary
key?

Thank you in advance!

Still a newbie,
-Thomas
Nov 13 '05 #1
18 12600
Okay, you have 2 table with primary keys, and a junction table that has
foreign keys to the other 2 tables. You could select the 2 fields together
in Table Design view, and click the Key icon on the toolbar to make a
2-field primary key (i.e. the primary key consists of the combination of the
2 fields... there are not 2 primary keys.)

That would be a good approach if the junction table should not contain
duplicates. For example, if the 2 tables are Client and Newsletter, it would
be a good idea not to have the same person on the newsletter group twice.

It would not be a good idea where duplicates are desired. For example, if
the 2 tables are Member and Position, the person might be chairman in 2002
and again in 2005, so you could validly have 2 entries where the member
holds the same position, so using the 2-field primary key would not be
suitable. Instead, you could add an AutoNumber to the 3rd field and use that
as primary key.

If the 3rd table is also likely to be involved in relations to other tables,
you might elect to give it an AutoNumber rather than handle multi-field
joins to other tables, which potentially could themselves have further joins
to other tables and so on.

--
Allen Browne - Microsoft MVP. Perth, Western Australia.
Tips for Access users - http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.

"Thomas A. Anderson" <NO************@hotmail.com> wrote in message
news:di**********@gnus01.u.washington.edu...
I am a bit confused in creating a composite primary key. I have three
table with two of the tables containing primary keys. I have two of the
tables (each with a primary key) having one to many relations with the
table containing no primary key. Do I just create two primary keys on the
table that does not contain any primary key for this to become a composite
primary key?

Thank you in advance!

Still a newbie,
-Thomas

Nov 13 '05 #2
Per Thomas A. Anderson:
I am a bit confused in creating a composite primary key. I have three table
with two of the tables containing primary keys. I have two of the tables
(each with a primary key) having one to many relations with the table
containing no primary key. Do I just create two primary keys on the table
that does not contain any primary key for this to become a composite primary
key?


Being easily confused myself, I never, ever use either composite primary keys or
any "real" field for a primary key.

Instead, I slavishly assign an AutoNumber PK (or, if there are strategic
problems with AutoNumber for a given situation, a Long field that I populate
uniquely).

It's been 12 years and I've never had a problem - and it seems to me like it's a
*lot* simpler, especially when I've been away from an app for a year or so and
have to understand it quickly again.
--
PeteCresswell
Nov 13 '05 #3
On Wed, 12 Oct 2005 07:31:28 -0700, "(PeteCresswell)" <a@b.c.invalid.USA> wrote:
Per Thomas A. Anderson:
I am a bit confused in creating a composite primary key. I have three table
with two of the tables containing primary keys. I have two of the tables
(each with a primary key) having one to many relations with the table
containing no primary key. Do I just create two primary keys on the table
that does not contain any primary key for this to become a composite primary
key?


Being easily confused myself, I never, ever use either composite primary keys or
any "real" field for a primary key.

Instead, I slavishly assign an AutoNumber PK (or, if there are strategic
problems with AutoNumber for a given situation, a Long field that I populate
uniquely).

It's been 12 years and I've never had a problem - and it seems to me like it's a
*lot* simpler, especially when I've been away from an app for a year or so and
have to understand it quickly again.


But surely you must sometimes use a composite unique index, or how do you
control unwanted duplicates?

Nov 13 '05 #4

polite person wrote:
On Wed, 12 Oct 2005 07:31:28 -0700, "(PeteCresswell)" <a@b.c.invalid.USA> wrote:
Per Thomas A. Anderson:
I am a bit confused in creating a composite primary key. I have three table
with two of the tables containing primary keys. I have two of the tables
(each with a primary key) having one to many relations with the table
containing no primary key. Do I just create two primary keys on the table
that does not contain any primary key for this to become a composite primary
key?


Being easily confused myself, I never, ever use either composite primary keys or
any "real" field for a primary key.

Instead, I slavishly assign an AutoNumber PK (or, if there are strategic
problems with AutoNumber for a given situation, a Long field that I populate
uniquely).

It's been 12 years and I've never had a problem - and it seems to me like it's a
*lot* simpler, especially when I've been away from an app for a year or so and
have to understand it quickly again.


But surely you must sometimes use a composite unique index, or how do you
control unwanted duplicates?


We're discussing something like this at the moment on another project,
where every Employee has a PIN (Personal Identification Number). PINs
are, by their nature, unique, and therefore a good candidate for
Primary Key. But what happens if the company decide in the future to
re-use PINs from retired or ex-employees?

In answer to your question, there is no reason why you cannot have a
composite unique index in addition to your PK. In fact, if there are
business rules like this, I prefer to enforce them at database level
(though currently I do much more work on SQL Server which has some
useful tools lacking in Access such as AFTER and INSTEAD OF Triggers)

Edward

Nov 13 '05 #5
On 12 Oct 2005 06:59:15 -0700, te********@hotmail.com wrote:

polite person wrote:

But surely you must sometimes use a composite unique index, or how do you
control unwanted duplicates?

In answer to your question, there is no reason why you cannot have a
composite unique index in addition to your PK. In fact, if there are
business rules like this, I prefer to enforce them at database level
(though currently I do much more work on SQL Server which has some
useful tools lacking in Access such as AFTER and INSTEAD OF Triggers)


Sorry, the point of my comment was that surely Pete must already be using composite unique indexes
therefore I was unable to see why he found it too difficult to ever use a composite primary key.

The question of whether or when to use articifical versus natural keys is a different one,
discussed here before. In the present dogmatic climate of the group discussions I don't
think reviving it would be useful.

Nov 13 '05 #6

(PeteCresswell) wrote:
I slavishly assign an AutoNumber PK


The availability of a unique integer that identifies each record in the
database, to the database, and that is available as an identifier to
anything that connects to the database enhances the reliability and
validity of the database.

Sometimes, I think, beginners try to make this PrimaryKey identify Joe,
Mary, or Tuck Number 2005-4. PrimaryKeys have nothing to do with
identifying Joe, Mary, or Truck 2005-04. They are the first designated
unique indexes of tables; as such they order the table in certain
situations (such as JET compact), determine the physical order of the
table in certain circumstances (clustered indexes), and the outcome of
the output of tables in many circumstances, and identify records,
nothing else but records.

I think your slavish assignment is one of the simplest and best things
one can do to create a sound database.

In my databases these are many of these AutoNumber PKs which I never
use explicitly. Does this slow my dbs? Not that I have noticed. Can
Access always find my records? Yes.

Nov 13 '05 #7
"Allen Browne" <Al*********@SeeSig.Invalid> wrote in
news:43***********************@per-qv1-newsreader-01.iinet.net.au:
Okay, you have 2 table with primary keys, and a junction table
that has foreign keys to the other 2 tables. You could select the
2 fields together in Table Design view, and click the Key icon on
the toolbar to make a 2-field primary key (i.e. the primary key
consists of the combination of the 2 fields... there are not 2
primary keys.)

That would be a good approach if the junction table should not
contain duplicates. For example, if the 2 tables are Client and
Newsletter, it would be a good idea not to have the same person on
the newsletter group twice.

It would not be a good idea where duplicates are desired. For
example, if the 2 tables are Member and Position, the person might
be chairman in 2002 and again in 2005, so you could validly have 2
entries where the member holds the same position, so using the
2-field primary key would not be suitable. Instead, you could add
an AutoNumber to the 3rd field and use that as primary key.


Er, why add an AutoNumber for that? Wouldn't you instead (assuming
you're going with natural keys) add the year to the composite key?
You'd need that as a unique index even if you added the AutoNumber.
I'd only add an AutoNumber to a junction table if the PK of the
junction table is a foreign key in a child table (which can
certainly happen, of course).

--
David W. Fenton http://www.bway.net/~dfenton
dfenton at bway dot net http://www.bway.net/~dfassoc
Nov 13 '05 #8
"(PeteCresswell)" <a@b.c.invalid.USA> wrote in
news:ua********************************@4ax.com:
Per Thomas A. Anderson:
I am a bit confused in creating a composite primary key. I have
three table with two of the tables containing primary keys. I
have two of the tables (each with a primary key) having one to
many relations with the table containing no primary key. Do I
just create two primary keys on the table that does not contain
any primary key for this to become a composite primary key?


Being easily confused myself, I never, ever use either composite
primary keys or any "real" field for a primary key.

Instead, I slavishly assign an AutoNumber PK (or, if there are
strategic problems with AutoNumber for a given situation, a Long
field that I populate uniquely).

It's been 12 years and I've never had a problem - and it seems to
me like it's a *lot* simpler, especially when I've been away from
an app for a year or so and have to understand it quickly again.


But surely you create unique indexes on those natural key
candidates? If not, how do you prevent the addition of duplicates?

Junction tables are the only place where I'll tend to use composite
keys (though they are hardly ever "natural keys," since the joins
are almost always on Autonumber fields), except where there is some
natural unique index that needs to be and *can* be enforced.

You may not use those as *primary keys* and use surrogate keys
instead (i.e., Autonumber), but you still need the unique index, or
your schema is really incomplete.

--
David W. Fenton http://www.bway.net/~dfenton
dfenton at bway dot net http://www.bway.net/~dfassoc
Nov 13 '05 #9
David W. Fenton wrote:
"(PeteCresswell)" <a@b.c.invalid.USA> wrote in
news:ua********************************@4ax.com:
Per Thomas A. Anderson:
I am a bit confused in creating a composite primary key. I have
three table with two of the tables containing primary keys. I
have two of the tables (each with a primary key) having one to
many relations with the table containing no primary key. Do I
just create two primary keys on the table that does not contain
any primary key for this to become a composite primary key?


Being easily confused myself, I never, ever use either composite
primary keys or any "real" field for a primary key.

Instead, I slavishly assign an AutoNumber PK (or, if there are
strategic problems with AutoNumber for a given situation, a Long
field that I populate uniquely).

It's been 12 years and I've never had a problem - and it seems to
me like it's a *lot* simpler, especially when I've been away from
an app for a year or so and have to understand it quickly again.


But surely you create unique indexes on those natural key
candidates? If not, how do you prevent the addition of duplicates?

Junction tables are the only place where I'll tend to use composite
keys (though they are hardly ever "natural keys," since the joins
are almost always on Autonumber fields), except where there is some
natural unique index that needs to be and *can* be enforced.

You may not use those as *primary keys* and use surrogate keys
instead (i.e., Autonumber), but you still need the unique index, or
your schema is really incomplete.

--
David W. Fenton http://www.bway.net/~dfenton
dfenton at bway dot net http://www.bway.net/~dfassoc


I suppose it never hurts to have the composite key around to help
ensure uniqueness. Any extra checks and balances that Access can
provide are good. I never want to trap an error saying I have a key
violation so I end up with the same amount of coding either way. The
composite key is just a final safety check. Even better would be not
to present choices on the form that could cause a duplicate in a
junction table. If anyone is allowed to edit the junction table
directly the composite key would be more important. I just don't think
there's a tempest in this teapot.

James A. Fortune

Nov 13 '05 #10
Per polite person:
But surely you must sometimes use a composite unique index, or how do you
control unwanted duplicates?


Frequently. I usually name such a key something like "UniquenessEnforcer".
But, even though it's redundant, I still assign the PrimaryKey role to a dumb,
blind, random number. i.e. you'll never see a composite join in any of my DB
diagrams.
--
PeteCresswell
Nov 13 '05 #11
Per te********@hotmail.com:
We're discussing something like this at the moment on another project,
where every Employee has a PIN (Personal Identification Number). PINs
are, by their nature, unique, and therefore a good candidate for
Primary Key. But what happens if the company decide in the future to
re-use PINs from retired or ex-employees?


Some people think the same thing about social security numbers.

I was one of them, but soon learned better.

Right now, I wouldn't even *think* about using a real, live value as a PK ....
Ok, maybe two-digit state abbreviations in tlkpState.... but for the other
99.9% of the cases to me it just isn't worth wasting the time thinking about
whether a given real-world value that is touted as unique really is so; when it
comes to DB design, I don't waste the time worrying about it.

If something like SSN seems unique and the turns out tb not... there's still
some work to be done, but a lot less work than if it were functioning as a PK.
--
PeteCresswell
Nov 13 '05 #12
Per polite person:
Sorry, the point of my comment was that surely Pete must already be using composite unique indexes
therefore I was unable to see why he found it too difficult to ever use a composite primary key.

The question of whether or when to use articifical versus natural keys is a different one,


To me, at least, compound keys add an additional layer of understanding needed
to grasp a DB's design; so I avoid them based on that issue alone.

My experience in earlier days of computing has been that some of the smartest
people wrote some of the hardest-to-maintain systems. The reason I saw was
that their minds were so quick, so big, and so facile that things that were
convoluted and hard to understand for most people were dirt simple/patently
obvious to them.

Being on the other end of the scale from those guys, I probably err on the side
of simplicity. Working with JET helps - looking at an Oracle-based project I
was on the fringes of a couple years ago, it seemed like that approach wouldn't
have washed.
--
PeteCresswell
Nov 13 '05 #13
What are your opinions on using the (unnatural) PKs as foreign keys in
other tables?
For example, with an Order Header of
tblOrder(OrderPK,OrderNumber,CustomerNumber,OrderD ate,etc)
(OrderNumber being the identifier quoted to the outside world)
would you recommend the OrderDetail be:
(a)
tblOrderDetail(OrderDetailPK,OrderNumber,ItemId,Qt yOrdered,Price,etc)
or
(b) tblOrderDetail(OrderDetailPK, OrderPK,ItemId,QtyOrdered,Price,etc)?
I currently prefer (a) and enforce RI on the join between
tblOrder.OrderNumber and tblOrderDetail.OrderNumber.
With (b) you don't get to see the outside world OrderNumber on
tblOrderDetail which makes me a bit nervous (you have to join to get at
it).
But I must admit I don't feel this is a well informed choice ...
Terry Bell

Nov 13 '05 #14
b) always;
You probably should access data from tblOrderDetail with a query or
view that joins tblOrder and tblOrderDetail and exposes the fields of
both tables, including OrderNumber. If you are using a Form-SubForm
then OrderNumber will show in the main object and can be coded to show
in the subobject too if that's wanted.

Nov 13 '05 #15
David, you understand that this was just an example which would probably get
more complex in practice, e.g. where the person holds a position for a date
range which may not be the full year.

You could come up with better examples, I'm sure.

--
Allen Browne - Microsoft MVP. Perth, Western Australia.
Tips for Access users - http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.

"David W. Fenton" <dX********@bway.net.invalid> wrote in message
news:Xn**********************************@216.196. 97.142...
"Allen Browne" <Al*********@SeeSig.Invalid> wrote in
news:43***********************@per-qv1-newsreader-01.iinet.net.au:
Okay, you have 2 table with primary keys, and a junction table
that has foreign keys to the other 2 tables. You could select the
2 fields together in Table Design view, and click the Key icon on
the toolbar to make a 2-field primary key (i.e. the primary key
consists of the combination of the 2 fields... there are not 2
primary keys.)

That would be a good approach if the junction table should not
contain duplicates. For example, if the 2 tables are Client and
Newsletter, it would be a good idea not to have the same person on
the newsletter group twice.

It would not be a good idea where duplicates are desired. For
example, if the 2 tables are Member and Position, the person might
be chairman in 2002 and again in 2005, so you could validly have 2
entries where the member holds the same position, so using the
2-field primary key would not be suitable. Instead, you could add
an AutoNumber to the 3rd field and use that as primary key.


Er, why add an AutoNumber for that? Wouldn't you instead (assuming
you're going with natural keys) add the year to the composite key?
You'd need that as a unique index even if you added the AutoNumber.
I'd only add an AutoNumber to a junction table if the PK of the
junction table is a foreign key in a child table (which can
certainly happen, of course).

Nov 13 '05 #16
Thank you so much everyone in expanding my knowledge! From the replies on
this topic, composite Primary Key seems to be more complicated than I
perceived the topic to be. Can anyone provide me with book reference
(titles), web links, or other type of documentation that I may continue to
read up on this topic? I googled the topic and could not really find
anything that was relevant to the topic, or did not explain the topic as
well as the posts in this forum. I am currently taking beginning DB with MS
Access at a local college and would like to become more familiar with this
topic before moving on to the next topic, Queries.

Again, thank you for the insight on this topic!

-Thomas
"Thomas A. Anderson" <NO************@hotmail.com> wrote in message
news:di**********@gnus01.u.washington.edu...
I am a bit confused in creating a composite primary key. I have three
table with two of the tables containing primary keys. I have two of the
tables (each with a primary key) having one to many relations with the
table containing no primary key. Do I just create two primary keys on the
table that does not contain any primary key for this to become a composite
primary key?

Thank you in advance!

Still a newbie,
-Thomas

Nov 13 '05 #17
On Wed, 12 Oct 2005 22:57:15 -0700, "Thomas A. Anderson" <NO************@hotmail.com> wrote:
Thank you so much everyone in expanding my knowledge! From the replies on
this topic, composite Primary Key seems to be more complicated than I
perceived the topic to be. Can anyone provide me with book reference
(titles), web links, or other type of documentation that I may continue to
read up on this topic? I googled the topic and could not really find
anything that was relevant to the topic, or did not explain the topic as
well as the posts in this forum. I am currently taking beginning DB with MS
Access at a local college and would like to become more familiar with this
topic before moving on to the next topic, Queries.

Again, thank you for the insight on this topic!

-Thomas

Hi
Here is a web reference which covers this topic briefly, it is really about the pros and cons of
natural versus artificial keys but this is often the same thing as no one (?) uses composite
artifial keys.
http://www.bcarter.com/intsurr1.htm
It dates from 1997 and is fairly generic.

This is a topic where most people fairly soon take a view (and then stick to it fairly dogmatically)
the commonest of which is "always use a single artificial key".

Note that as an artifical or surrogate key is not part of the data (one of its advantages) it is
important that two records are not identical in their other fields.

Though in principle a table doesn't need to have a primary key (eg linking tables for many-to-many
joins) , in many situations this will make the data engine prevent you from editing the table. This
is an example of where, when you add an artifical key, you should add a composite index on the other
fields or use soime other method to ensure that records are not duplicated.

HTH

Nov 13 '05 #18
On Fri, 14 Oct 2005 22:37:24 GMT, polite person <si*****@ease.com> wrote:

Here is a web reference which covers this topic briefly, it is really about the pros and cons of
natural versus artificial keys but this is often the same thing as no one (?) uses composite
artifial keys.

Not sure what I was thnking of when I wrote the last bit, ignore it. Personally I always use
artifical keys but only sometimes add an extra key to replace a composite (artificial) key on a join
table.
Nov 13 '05 #19

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

Similar topics

0
by: Girish Agarwal | last post by:
--0-474210375-1058976151=:31789 Content-Type: text/plain; charset=us-ascii Content-Id: Content-Disposition: inline Note: forwarded message attached. __________________________________
5
by: John | last post by:
Specifically for joint tables... tblStudents tblClasses tblClasses_Students Is it be good programming to use a composite primary key in tblClasses_Students (where the key is ClassID and...
10
by: deko | last post by:
I understand it's possible to make a composite Primary Key by holding down the control key and selecting multiple fields, then right-clicking and selecting Primary Key. But I've heard that's not a...
4
by: Ismail Rajput | last post by:
Is there any option we can use Composite DataKeyField in the DataList and DataGrid?
2
by: Peter Lindquist | last post by:
I had a fun issue this morning, and now I'm wondering if I violated a rule I didn't know about. Any insight would be much appreciated. All operations involving this particular table may be assumed...
7
by: Ronald S. Cook | last post by:
My client manager likes concatenated/composite primary keys. I don't. Can anyone forward any arguments pro or con? Thanks, Ron
4
by: Wolfgang Keller | last post by:
Hello, so far it seems to me as if the only ORM module for Python which supports composite primary/foreign keys was SQLAlchemy. Which looks a little bit "overbloated" for my needs: I "just" need...
2
by: lfhenry | last post by:
Hi All, I am about to make some changes to a process we have. The new process will have a new table who's job it is to store temporarily data such as customer, clerkno,productno, storeno , amount,...
54
by: csolomon | last post by:
Hello: I was wondering if I could get some input on how to address a design issue, involving my composite table. I have one portion of my project complete. The following forms and reports I...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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,...

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.