简体   繁体   English

无法从SQL Server表中删除主键

[英]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.

相关问题 SQL Server:删除表主键,但不知道其名称 - SQL Server: drop table primary key, without knowing its name 如何删除聚集属性但保留表中的主键。 SQL Server 2005 - How to drop clustered property but retain primary key in a table. SQL Server 2005 在没有SQL Server中的主键的现有表上创建主键 - create primary key on existing table without primary key in SQL Server 如何确定SQL Server中表的主键? - How to determine the primary key for a table in SQL Server? SQL Server 2008:使用主键更新表 - SQL Server 2008 : update the table with primary key 带有来自另一个表的主键的sql表 - sql table with primary key from another table 无法在 SQL 服务器中删除外键 - Unable to drop foreign key in SQL server SQL Server从表中选择主键在另一个表中不显示为外键的表 - SQL Server Select from table where primary key does not appear as foreign key in another table 需要删除并重新创建主键上具有identity(1:1)的SQL表 - Need to drop and recreate SQL table with identity(1:1) on primary key 我们可以在SQL Server表@Production环境中删除并重新创建主键吗? 我们是否需要关闭服务器,否则我们可以直播它? - Can we drop and recreate primary key in SQL Server table @ Production environment? Do we have to down the server for it or we can do it live?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM