Although I’m guilty of this crime, it seems to me there can’t be any good reason for a table to not have an identity field primary key.
Pros: – whether you want to or not, you can now uniquely identify every row in your table which previously you could not do – you can’t do sql replication without a primary key on your table
Cons: – an extra 32 bits for each row of your table
Consider for example the case where you need to store user settings in a table in your database. You have a column for the setting name and a column for the setting value. No primary key is necessary, but having an integer identity column and using it as your primary key seems like a best practice for any table you ever create.
Are there other reasons besides size that every table shouldn’t just have an integer identity field?
Sure, an example in a single-database solution is if you have a table of countries, it probably makes more sense to use the ISO 3166-1-alpha-2 country code as the primary key as this is an international standard, and makes queries much more readable (e.g.
CountryCode = 'GB'as opposed toCountryCode = 28). A similar argument could be applied to ISO 4217 currency codes.In a SQL Server database solution using replication, a
UNIQUEIDENTIFIERkey would make more sense as GUIDs are required for some types of replication (and also make it much easier to avoid key conflicts if there are multiple source databases!).