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 Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Modifying a Table to have a Primary Key 2

Status
Not open for further replies.

bepryor

Programmer
Jul 8, 2004
3
US
We have a simple table with hundreds of entries in it. It is now a requirement that the "sample number" field contain a unique value. Can the definition of this table be changed to designate the "sample number" as a primary key? If so, what will happen to any existing duplicates?
 
I do not believe you can add a unique index to a field that contains duplicates. You will have to remove the duplicates and then add the index.
 
You will have to copy the existing data into another table with a additional unique key field. Duplicates will no longer be of concern in the new table. If you did want to remove the duplications try a query with group by references. There are threads in the FAQ concerning duplication records.


Ian Mayor (UK)
Program Error
There's ALWAYS more than one way to skin a cat!
But only one way to get it RIGHT!
 
Hi ProgramError
Why is it necessary to copy the table?
 
Maybe he wants to keep the duplicated records.
Alternatively querying and renumber the duplicate indexes will give him a unique key but this will reorder the table unless he has another field on which he can reference its order. Copying the table will retain the original order.
I respect you opinion Remou as you have helped me out countless times. I just made another suggestion based on the question.


Ian Mayor (UK)
Program Error
There's ALWAYS more than one way to skin a cat!
But only one way to get it RIGHT!
 
ProgramError, no need of a new table for your suggestion:
DoCmd.RunSQL "ALTER TABLE myTable ADD COLUMN myID COUNTER(1,1) CONSTRAINT PrimaryKey PRIMARY KEY;"

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Program Error, indeed you are right, if bepryor wishes to keep the duplicates and a backup is always nice to have.

Also, I was curious. :)
 
Thank you one and all for your suggestions, references to FAQ's and comments.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top