Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
Log in  ::  Register  ::  Not logged in
Home       Members    Calendar    Who's On

Add to briefcase

Add primary key to an existing table Expand / Collapse
Posted Wednesday, July 2, 2014 8:26 AM


Group: General Forum Members
Last Login: Wednesday, May 27, 2015 9:33 AM
Points: 138, Visits: 236

I created a table in my Azure SQL Database, Part of the code is here..
CREATE TABLE [dbo].[DEVAppointments](
[dbPatFlowArcCnt] [int] NOT NULL,

I now wish to make the column dbPatFlowArcCnt a Primary Key column too but don't wish to drop the table as it has lots of data in it.

When I use this sql however..
alter table DEVAppointments drop CONSTRAINT PK_CI_DEVAppt

alter table DEVAppointments add primary key (PK_CI_DEVAppt, dbPatFlowArcCnt)

I get this error

Msg 40054, Level 16, State 2, Line 9
Tables without a clustered index are not supported in this version of SQL Server. Please create a clustered index and try again.

Msg 3727, Level 16, State 0, Line 9
Could not drop constraint. See previous errors.


Post #1588489
Posted Wednesday, July 2, 2014 8:40 AM


Group: General Forum Members
Last Login: Yesterday @ 8:38 AM
Points: 1,729, Visits: 6,334
I don't think you can change a primary key on Azure if the table has data in it. There's quite a few suggestions out there for workarounds but unfortunately it's not as simple as dropping the old one and creating a new one.

On two occasions I have been asked, "Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?" ... I am not able rightly to apprehend the kind of confusion of ideas that could provoke such a question.
—Charles Babbage, Passages from the Life of a Philosopher

How to post a question to get the most help
Post #1588497
Posted Wednesday, July 2, 2014 12:26 PM

SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Tuesday, October 6, 2015 1:14 AM
Points: 38, Visits: 159
Since Azure requires a clustered index for every table with data in it, you can't alter it to change the clustered index.
Post #1588588
Posted Wednesday, July 2, 2014 12:30 PM



Group: General Forum Members
Last Login: Yesterday @ 1:42 PM
Points: 14,790, Visits: 14,607
What you will need to do is create a new table with the structure you want. Then copy all the data from the current table into the new one. Drop the current table. Rename the new table to the name of the current one.


Need help? Help us help you.

Read the article at for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Post #1588592
Posted Thursday, July 3, 2014 7:29 AM



Group: General Forum Members
Last Login: Today @ 8:20 AM
Points: 15,917, Visits: 30,202
Sometimes I wish I could just vote for answers here.

Karen has the cause, Sean has the solution.

"The credit belongs to the man who is actually in the arena, whose face is marred by dust and sweat and blood..." Theodore Roosevelt
The Scary DBA
Author of:
SQL Server Query Performance Tuning
SQL Server Execution Plans

Product Evangelist for Red Gate Software
Post #1588880
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse