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
Author
Message
Posted Wednesday, July 2, 2014 8:26 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Monday, December 8, 2014 2:37 AM
Points: 104, Visits: 170
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,

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

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Yesterday @ 5:27 AM
Points: 1,268, Visits: 4,145
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, November 18, 2014 1:00 AM
Points: 36, Visits: 151
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


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Yesterday @ 2:05 PM
Points: 13,327, Visits: 12,820
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)
Post #1588592
Posted Thursday, July 3, 2014 7:29 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Yesterday @ 8:22 AM
Points: 14,205, Visits: 28,534
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
Post #1588880
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse