Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Designing a table's primary key.

Status
Not open for further replies.

Moss100

Technical User
Aug 10, 2004
584
GB
After 10 or so years of using Access the potential problem of releying on the Autonumber for my tables primary key has become clear. Following a table corrpution I called up a backup, pasted in some records and then realsied that the records did not share the same autonumber as before(Access had jumped the deleted records in the autonumber field). This then made the relationships in my tables loose their integrity.

I have not thought of this issue before and I have commonly constructed tables using an autonumer as my primary key and linked from this. i.e. CustomersID etc...

Access 2010 allows table data macros and so I see that I can have an autonumber at the start of a new record which sets a number field using a table level data macro when an insert event is triggered - (which I can set as primary key).

Would this be a safer and more desirable way of setting a tables primary key? Or if someone has a better suggestion?

Many thanks Mark
 
You should let the primary key be autonumbered. However, you can build your own keys as well. Your own key should be used to join to other tables. Classic example:

Order Table
OrderKey (autogenerated)
OrderNumber (primary key)
OrderDate
CustomerID
etc

OrderDetail Table
OrderDetailKey (autogenerated)
OrderNumber (with OrderItem the primary key)
OrderItem (with OrderNumber the primary key)
ItemQuantity
ItemPrice
etc

Customer Table
CustomerKey
CustomerID (primary key)
CustomerType
CustomerCity
etc.

==================================
The trouble with doing something right the first time is that nobody appreciates how difficult it was - Steven Wright


 
This is an old debate.
Do e Google search for natural key vs surrogate key

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
You may also find handy resetting the autonumber in Access.
More info about it here

Have fun.

---- Andy

A bus station is where a bus stops. A train station is where a train stops. On my desk, I have a work station.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top