将主键添加到现有表 [英] Add primary key to existing table

查看:104
本文介绍了将主键添加到现有表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为 Persion 的表。在此表中,我有5列:

I have an existing table called Persion. In this table I have 5 columns:


  • persionId

  • Pname

  • PMid

  • 描述

  • Pamt

  • persionId
  • Pname
  • PMid
  • Pdescription
  • Pamt

创建此表时,我将 PersionId Pname 设置为主键

When I created this table, I set PersionId and Pname as the primary key.

我现在想在主键-PMID中再增加一列。如何编写 ALTER 语句来做到这一点? (我的表中已经有1000条记录)

I now want to include one more column in the primary key - PMID. How can I write an ALTER statement to do this? (I already have 1000 records in the table)

推荐答案

放置约束并重新创建

alter table Persion drop CONSTRAINT <constraint_name>

alter table Persion add primary key (persionId,Pname,PMID)

编辑:

您可以通过以下查询找到约束名称:

you can find the constraint name by using the query below:

select OBJECT_NAME(OBJECT_ID) AS NameofConstraint
FROM sys.objects
where OBJECT_NAME(parent_object_id)='Persion'
and type_desc LIKE '%CONSTRAINT'

这篇关于将主键添加到现有表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆