[英]Unable to drop Primary Key from SQL Server table
I have primary key for a table. 我有一张桌子的主键。 I checked constraints by using below query. 我通过使用以下查询检查了约束。
SELECT name
FROM sys.key_constraints
WHERE [type] = 'PK'
AND [parent_object_id] = Object_id('<TableName>');
Output of above query: PK_dbo.TableName (only 1 constraint of entire table) 以上查询的输出: PK_dbo.TableName (整个表只有1个约束)
I am trying to drop above constraint by using below query. 我试图通过使用下面的查询来摆脱约束。
ALTER TABLE TableName DROP CONSTRAINT PK_dbo.TableName;
I tried as like below but same error. 我尝试如下,但相同的错误。
ALTER TABLE dbo.TableName DROP CONSTRAINT PK_dbo.TableName;
Query execution failed with error message : 查询执行失败,并显示错误消息:
Failed to execute query. 无法执行查询。 Error: Incorrect syntax near '.' 错误:“。”附近的语法不正确。
What is wrong in above query? 上面的查询有什么问题?
Make sure you have the dot .
确保您有圆点 .
in your constraint name. 在您的约束名称中。
If it is there, then type the constraint name inside square brackets, otherwise make sure you are giving the correct name 如果存在,请在方括号内键入约束名称,否则请确保输入正确的名称
ALTER TABLE dbo.TableName DROP CONSTRAINT [PK_dbo.TableName];
Note : It is not recommended to use characters like dot, blank space etc in object names. 注意 :不建议在对象名称中使用点,空格等字符。 Instead, use underscores like PK_dbo_TableName
而是使用下划线,例如PK_dbo_TableName
It is because of the .
这是因为.
in your primary key name. 在您的主键名称中。 It can be avoided by using [ ]
like this: 可以通过使用[ ]
这样避免:
ALTER TABLE dbo.TableName DROP CONSTRAINT [PK_dbo.TableName];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.