I’m running into an issue with a join: getting back too many records. I added a table to the set of joins and the number of rows expanded. Usually when this happens I add a select of all the ID fields that are involved in the join. That way it’s pretty obvious where the expansion is happening and I can change the ON of the join to fix it. Except in this case, the table that I added doesn’t have an ID field. This is a problem. But perhaps I’m wrong.
Should every table in a database have an IDENTITY field that’s used as the PK? Are there any drawbacks to having an ID field in every table? What if you’re reasonably sure this table will never be used in a PK/FK relationship?
When having an identity column is not a good idea?
There are two concepts that are close but should not be confused:
IDENTITYandPRIMARY KEYEvery table (except for the rare conditions) should have a
PRIMARY KEY, that is a value or a set of values that uniquely identify a row.See here for discussion why.
IDENTITYis a property of a column inSQL Serverwhich means that the column will be filled automatically with incrementing values.Due to the nature of this property, the values of this column are inherently
UNIQUE.However, no
UNIQUEconstraint orUNIQUEindex is automatically created onIDENTITYcolumn, and after issuingSET IDENTITY_INSERT ONit’s possible to insert duplicate values into anIDENTITYcolumn, unless it had been explicityUNIQUEconstrained.The
IDENTITYcolumn should not necessarily be aPRIMARY KEY, but most often it’s used to fill the surrogatePRIMARY KEYsIt may or may not be useful in any particular case.
Therefore, the answer to your question:
is this:
No. There are cases when a database table should NOT have an
IDENTITYfield as aPRIMARY KEY.Three cases come into my mind when it’s not the best idea to have an
IDENTITYas aPRIMARY KEY:PRIMARY KEYis composite (like in many-to-many link tables)PRIMARY KEYis natural (like, a state code)PRIMARY KEYshould be unique across databases (in this case you useGUID/UUID/NEWID)All these cases imply the following condition:
You shouldn’t have
IDENTITYwhen you care for the values of yourPRIMARY KEYand explicitly insert them into your table.Update:
Many-to-many link tables should have the pair of
id‘s to the table they link as the composite key.It’s a natural composite key which you already have to use (and make
UNIQUE), so there is no point to generate a surrogate key for this.I don’t see why would you want to reference a
many-to-manylink table from any other table except the tables they link, but let’s assume you have such a need.In this case, you just reference the link table by the composite key.
This query:
is much more efficient than this one:
, for obvious reasons.