开发者

How do to massively alter primary keys to tables in sql server database

开发者 https://www.devze.com 2022-12-27 01:51 出处:网络
Is there any way to massively alter primary keys(uid) toall tables with no primary keys in sql server database ? I have a lot开发者_运维知识库 of tables and most of them don not have primary keys.

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.


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

  • The view Information_Schema.Tables
  • Undocumented Stored Procedure sp_MSforeachtable
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号