如何使用 SQL 语法更改主键约束? [英] How can I alter a primary key constraint using SQL syntax?

查看:21
本文介绍了如何使用 SQL 语法更改主键约束?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表,它的主键约束中缺少一列.我不想通过 SQL Server 对其进行编辑,而是希望将其放入脚本中以将其添加为我们更新脚本的一部分.

I have a table that is missing a column in its primary key constraint. Instead of editing it through SQL Server, I want to put this in a script to add it as part of our update scripts.

我可以使用什么语法来做到这一点?我必须删除并重新创建键约束吗?

What syntax can I use to do this? Must I drop and recreate the key constraint?

推荐答案

是的.唯一的方法是使用 Alter 表删除约束,然后重新创建它.

Yes. The only way would be to drop the constraint with an Alter table then recreate it.

ALTER TABLE <Table_Name>
DROP CONSTRAINT <constraint_name>

ALTER TABLE <Table_Name>
ADD CONSTRAINT <constraint_name> PRIMARY KEY (<Column1>,<Column2>)

这篇关于如何使用 SQL 语法更改主键约束?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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