SQL Clone
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
Ten Centuries
Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)

Group: General Forum Members
Points: 1120 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,
Neil Burton
Neil Burton
SSChampion
SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)

Group: General Forum Members
Points: 12114 Visits: 11423
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-Addicted
SSC-Addicted (456 reputation)SSC-Addicted (456 reputation)SSC-Addicted (456 reputation)SSC-Addicted (456 reputation)SSC-Addicted (456 reputation)SSC-Addicted (456 reputation)SSC-Addicted (456 reputation)SSC-Addicted (456 reputation)

Group: General Forum Members
Points: 456 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
SSC Guru
SSC Guru (148K reputation)SSC Guru (148K reputation)SSC Guru (148K reputation)SSC Guru (148K reputation)SSC Guru (148K reputation)SSC Guru (148K reputation)SSC Guru (148K reputation)SSC Guru (148K reputation)

Group: General Forum Members
Points: 148154 Visits: 18570
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 Modens 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
SSC Guru
SSC Guru (221K reputation)SSC Guru (221K reputation)SSC Guru (221K reputation)SSC Guru (221K reputation)SSC Guru (221K reputation)SSC Guru (221K reputation)SSC Guru (221K reputation)SSC Guru (221K reputation)

Group: General Forum Members
Points: 221297 Visits: 33555
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