Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Primary Key Generated From External System

What are the general guidelines for the following scenario:

You need to create a table where the PK is a value that is generated from an external system. It is unique, similar to using a SSN.

Does it make sense for this to be the primary key? Especially considering it will be used in foreign key relationships with two other tables. Or, would it be better to create an auto-incrementing ID field in addition to the unique key, and use the auto-incrementing ID in the table relationships?

like image 285
Bob123Moon Avatar asked Sep 26 '11 14:09

Bob123Moon


2 Answers

Personally, I wouldn't trust an ID from an external system. Sure, they'll promise you today that it will be unique, never change and never be reused. But then, some day in the future...

Create your own auto-incrementing ID and use that for the PK and FK relationships. Keep their ID only as a reference.

like image 145
Joe Stefanelli Avatar answered Sep 20 '22 00:09

Joe Stefanelli


I would always create my own surrogate primary key, and set the natural identifier as a secondary unique key.

Scott W Ambler has a good comparison of the two strategies here.

like image 41
Ian Nelson Avatar answered Sep 24 '22 00:09

Ian Nelson