SQL Server – Impacts of Converting Column to Primary Key

primary-keysql-server-2008

One of my MSSQL table don't have any primary key, I would like to add one. Just want to know:

First, if I convert an existing column (or multiple columns) to a primary key, what would be the impact? Will it affect the web applications rely on this table?

Furthermore, what if I add a new column and make it primary key on an existing table?

Last but not least, if none of the above is a good practice, what would be one? Creating a new table, migrating data and deleting old one and renaming the new table to old name?

the applications using this table is nothing but some CRUD operations.

Thank you in advance.

Best Answer

Having primary key is absolute necessity for any table.

But obviously before you do that you need to see whether adding a primary key will affect the application itself. If written in ASP.Net it will require model update. Also without primary key entity model looks at it as a view, which is mostly used for displaying data. Adding primary key would cause a conflict and you would have to explicitly create a new view.

If you already have a clustered index on some column, adding a primary key on another column would simply create a primary key constraint. Where as if you do not have a clustered index (heap table), and create a primary key it will implicitly create a clustered index as well on that column.

Probably worst option would be creating a new table and migrating data, simply due to the fact that you would loose all the indexes(if any) were on the table,also if the table had some relationship with the other table you wouldnt be able to delete it. Or if explicitly some one before you added some security rights on the table, would be lost.

If possible try to add primary key on already created table, and existing column.