Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Organization Id Field as a Composite Primary Key

We currently have a system where each client gets their own database when they sign-up. Not surprisingly this is getting out of hand.

We are preparing merge all these databases together to a single db. In order to do this we need to note which organization the row belongs. How best is this handled on the database and what pros/cons (Speed, Maintainability, etc) are there to the methods below?

Option 1: Make the Organization Id to all tables and make it part of the primary key (making all keys composite).

Option 2: Add Organization Id to all tables as a column with a Foreign Key to the Organization Table.

Option 3: Something else.

We are looking at moving to NHibernate with this move, if that has an effect on what is done.

like image 851
Thad Avatar asked Dec 23 '22 12:12

Thad


1 Answers

Option 1 and 2 are not mutually exclusive and you should do both. A foreign key constraint on the organization helps ensure that your organization keys are valid. A composite primary key is the only way to achieve uniqueness, unless you want to re-key all your records, and it is also valuable for lookup performance. Alternatively, you'd have to create a new primary key column on the combined data, an identity column, for instance. Such a change would likely require more changes in other places.

like image 199
cdonner Avatar answered Dec 27 '22 03:12

cdonner