Defining Primary Key from two fields (1 Viewer)

dbmanalser84

Registered User.
Local time
Today, 22:19
Joined
Feb 26, 2008
Messages
52
I have this table:

intSalesID (Number, Indexed: (Yes No Duplicates)
intCarID (Number, Indexed: (Yes No Duplicates)
intQuantitySales (Number, Indexed: (No)

This table is a cross table from the two tables tblCars and tblSales where the intSalesID and intCarID are Primary Keys. In this table I didn't set the P.K. I saw databases that have P.K. in tables like this deined from the two fields (intSalesID and intCarID, would be in my case). I wanted to ask what is the difference if I leave this table like this and if I set this two fields to form a P.K.? What is the difference in these twi cases? What do I get and what I don't, if I do either way? Thnx.
 

ashishprem

Registered User.
Local time
Today, 13:19
Joined
Mar 18, 2008
Messages
35
if the data in table gets populated by only cross product from the other two tables where intsalesID and intcarid are column then it should not affect the population of third table if we do not have PK defined. But if we have PK defined then probably the search will be faster on the table as it will be indexes.
I dont see any harm if you define the two columns as composite PK on the table
Ashish
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 16:19
Joined
Feb 19, 2002
Messages
43,266
You need a compound primary key because it is the combination of the two columns that must be unique, not the individual instances as you have currently defined. To create a compound key, click once to highlight the first column and while holding the control key, click again to select the second column (you can select up to 10 columns if necessary to acheive uniqueness). When the two columns are highlighted, press the key icon on the toolbar.

You may also want to add an index on the second of the two pk fields to aid with searching but this index must allow duplicates.
 

Users who are viewing this thread

Top Bottom