将主键更改为复合键(主键已存在) [英] Change Primary Key to Composite Key (Primary Key already exists)
问题描述
我正在尝试将我的 SQL 数据库中表的主键从现有键更改为复合键,其中不包括现有列.由于以下错误消息,以下代码不起作用:
I am trying to change the primary key of a table in my SQL database from the existing key to a composite key, which does not include the existing column. The following code is not working due to the following error messages:
删除主键:
PRIMARY 附近的语法不正确.期待 COLUMN、CONSTRAINT、ID 或QUOTED_ID
Incorrect Syntax near PRIMARY. Expecting COLUMN, CONSTRAINT, ID, or QUOTED_ID
添加主键:
PRIMARY 附近的语法不正确.期待 ID
T-SQL 代码:
ALTER TABLE AgentIdentification
DROP PRIMARY KEY Number,
ADD PRIMARY KEY (AgentId, IdIndicator)
编辑
我可以通过使用以下两个查询语句来完成此操作
I was able to accomplish this by using the following two query statements
ALTER TABLE AgentIdentification
DROP CONSTRAINT [PK_AgentId_Id]
GO
ALTER TABLE AgentIdentification
ADD CONSTRAINT pk_PersonID PRIMARY KEY (AgentId, IdIndicator)
我不需要请求 SQLDROP PRIMARY KEY",而是告诉它DROP CONSTRAINT",将这两个操作分成两个查询也有帮助.
Instead of requesting that SQL "DROP PRIMARY KEY" I needed to tell it to "DROP CONSTRAINT", also separating these two actions into two queries helped.
推荐答案
/* For SQL Server/Oracle/MS ACCESS */
ALTER TABLE AgentIdentification
DROP CONSTRAINT PK_Table1_Col1
/* For MySql */
ALTER TABLE AgentIdentification
DROP PRIMARY KEY
添加主键:
MySQL / SQL Server / Oracle / MS Access:
ALTER TABLE Table1
ADD CONSTRAINT pk_PersonID PRIMARY KEY (AgentId, IdIndicator)
这篇关于将主键更改为复合键(主键已存在)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!