Posts tagged primary keys
A colleague of mine wrote to ask me for advice with updating a 50,000 record strong Access database. The database was originally built to use a customers Social Security Number as the primary key. This was smart thinking since a persons SSN is guaranteed to be unique (miskeys and identity theft excluded) and requires no other arbitrary identifier to describe or reference the patient record. Now they found themselves in the midst of a wave of recent patient privacy legislature and sought to get rid of the SSN field.
Her initial thought was to use a combination of the first four letters of the last name and the last four digits of the original SSN. Though the chances of finding a duplicate are slim (there are 10^4 possible combinations of the last-4 digits in a SSN alone, not including the 24^4 possible combinations of the first four letters of the last name), but there is still a chance of two records being created with “smit1234″ as their new key so this method is best avoided. (I have some other thoughts on arbitrary vs. rational, singular vs. compound keys which I’ll post later). I instead urged her to to go with the old fashioned auto-incremented numeric identity column and offered her the following steps for going about the conversion:
- Make a backup copy of the database – just in case
- In your main table, create a new column named ID or patientID or whatever is appropriate. I’ll assume it’s just ID for now. Create it as type AutoNumber. Under the General properties tab set New Values to “increment” and Indexed to “Yes (no duplicates)”. Move the column so that it is the first one in the table, then save your changes.
- Look at the data to make sure the new column is populated with an incrementing ID number.
If you have data in tables that are linked to the main table, then for each linked table
- Create a new column named patientID or whatever is appropriate. Set is as type Number. Set the Indexed property to “Yes (duplicates OK)”. Save your changes.
- Create a new Query (just accept all of the default options) and switch to the SQL view. We are going to write a query that will update the new patientID column with the new AutoNumber that we created above. The query will be different depending on the name of the linked table, but it should be similar to this:
UPDATE [linked_table_name], [main_table_name] SET [linked_table_name].[patientID] = [main_table_name].[ID] WHERE [main_table_name].[old_social_security_column_name_from_main_table] = [linked_table_name].[old_social_security_column_name_from_linked_table]
- Verify that the data was updated.
- Remove the old SS# column from the main table
- Set the new ID column as the Primary Key
- Remove the old SS# from any linked tables
- Update your old queries, forms or reports to reflect the changes.
* This post was originally published on July 17, 2006 at http://www.csb7.com/whyblogwhy/index.php/2006/07/17/changing-the-primary-key-field-in-an-access-database/