Emergent Software

Using ID for Primary Key Column Name Database Development Best Practice | sp_Develop

by Kevin Martin

Using ID for Primary Key Column Name is check Id #7 in my SQL Server Assess open source project on GitHub. The project contains a stored procedure named sp_Develop that can be used by database developers, software developers and for performing database code (smell) assessment to adhere to SQL Server database development best practices.

For columns that are the primary key for a table and uniquely identify each record in the table, the name should be [TableName] + “Id” (e.g. On the Make table, the primary key column would be “MakeId”).

Though “MakeId” conveys no more information about the field than Make.Id and is a far wordier implementation, it is still preferable to “Id”.

Naming a primary key column “Id” is also “bad” when you query from several tables you will need to rename the “Id” columns so you can distinguish them in result set with aliases.

Using just “Id” can mask join issues that are not obvious at first sight.

 

 

 

 

 

Now you can see that MK.MakeId does not equal C.ColorId in the last table join below.

 

 

 

 

 

Interested in learning more? Visit the documentation website or the project directly on GitHub!

About Emergent Software

Emergent Software offers a full set of software-based services from custom software development to ongoing system maintenance & support serving clients from all industries in the Twin Cities metro, greater Minnesota and throughout the country.

Learn more about our team.

Let's Talk About Your Project

Contact Us