16

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:

DROP PRIMARY KEY:

Incorrect Syntax near PRIMARY. Expecting COLUMN, CONSTRAINT, ID, or QUOTED_ID

ADD PRIMARY KEY:

Incorrect Syntax near PRIMARY. Expecting ID

T-SQL code:

ALTER TABLE AgentIdentification 
DROP PRIMARY KEY Number, 
ADD PRIMARY KEY (AgentId, IdIndicator)

EDIT

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)

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.

6
  • 7
    Please tag this with the correct RDBMS. SQL is not a database; it's a language used by many (slightly differently in every case).
    – Ben
    Aug 28, 2012 at 21:20
  • Do you want to drop PK field or PK constraint? Aug 28, 2012 at 21:20
  • 3
    Please: 1) Specify your database, 2) Cut/paste the exact error message, 3) Consider breaking your command into two "alter table" statements, 4) Be aware that you'll probably need other statements (e.g. to drop constraints), or you might even have to drop and recreate the entire table
    – paulsm4
    Aug 28, 2012 at 21:21
  • 2
    @NealR: You did not answer. What RDBMS do you use? SQL-Server, MySQL, Oracle, Postgres, DB2, another one? Aug 28, 2012 at 21:32
  • 1
    I suspect it's SQL-Server, so take a look here on how to find the PK constraint name: Dropping a Primary Key Constraint in SQL Server – is it easy? May be. May be not! Aug 28, 2012 at 21:36

1 Answer 1

28
    /* For SQL Server/Oracle/MS ACCESS */
    ALTER TABLE  AgentIdentification 
    DROP CONSTRAINT PK_Table1_Col1


    /* For MySql */
    ALTER TABLE  AgentIdentification 
    DROP PRIMARY KEY

To Add primary key :

MySQL / SQL Server / Oracle / MS Access:

ALTER TABLE Table1
ADD CONSTRAINT pk_PersonID PRIMARY KEY (AgentId, IdIndicator)
4
  • 1
    ALTER TABLE .. DROP PRIMARY KEY also works for Oracle if I'm not mistaken
    – user330315
    Aug 28, 2012 at 22:00
  • 1
    @NealR: You did not answer. What RDBMS do you use? SQL-Server, MySQL, Oracle, Postgres, DB2, another one?
    – paulsm4
    Aug 29, 2012 at 1:31
  • It is SQL server. You'll have to excuse me, I'm interning right now and not familiar with all the terms.
    – NealR
    Aug 29, 2012 at 16:59
  • @Ruzbeh I know it it an old question, but I hope you can answer this: In Oracle, what is the impact of dropping and creating a composite primary key in a table that containes the data?? The data will be affected? (In my case, i am adding a field in the primary key) Sep 22, 2018 at 22:21

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.