Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Add primary key to an existing table


Add primary key to an existing table

Author
Message
mattech06
mattech06
SSC-Enthusiastic
SSC-Enthusiastic (138 reputation)SSC-Enthusiastic (138 reputation)SSC-Enthusiastic (138 reputation)SSC-Enthusiastic (138 reputation)SSC-Enthusiastic (138 reputation)SSC-Enthusiastic (138 reputation)SSC-Enthusiastic (138 reputation)SSC-Enthusiastic (138 reputation)

Group: General Forum Members
Points: 138 Visits: 236
Hi,

I created a table in my Azure SQL Database, Part of the code is here..

CREATE TABLE [dbo].[DEVAppointments](
[ClusterInd] [int] IDENTITY(1,1) CONSTRAINT PK_CI_DEVAppt PRIMARY KEY CLUSTERED NOT NULL,
[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.

thanks,
BWFC
BWFC
SSCrazy
SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)

Group: General Forum Members
Points: 2418 Visits: 9639
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
Karen Lopez @datachick
Karen Lopez @datachick
SSC Rookie
SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)

Group: General Forum Members
Points: 46 Visits: 193
Since Azure requires a clustered index for every table with data in it, you can't alter it to change the clustered index.
Sean Lange
Sean Lange
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16576 Visits: 17021
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 http://www.sqlservercentral.com/articles/Best+Practices/61537/ 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)
Grant Fritchey
Grant Fritchey
SSCoach
SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)

Group: General Forum Members
Points: 17597 Visits: 32267
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 and SQL Server Execution Plans
Product Evangelist for Red Gate Software
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search