views:

45

answers:

1

Hi

Is there any way to massively alter primary keys(uid) to all tables with no primary keys in sql server database ? I have a lot of tables and most of them don not have primary keys.

Thanks in advance.

+3  A: 

Add an identity field to the table

Alter Table TableName
Add TableId Integer Identity (1, 1)

Here is how to add the Primary Key

ALTER TABLE TableName
ADD CONSTRAINT pk_TableKeyName PRIMARY KEY (TableID)

To generate this script for each table in your database, you can use one of the following

Raj More
I was looking a way to massively alter tables with primary key(PK), i have many tables and need to maintain them adding PK with single SQL script.
shuxer
@shuxer: Answer edited to show you a way to do this for multiple tables.
Raj More
Thanks Raj More for your useful hints
shuxer