SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Add identity field to an existing table, change clustered primary key to non-clustered ,and created...


Add identity field to an existing table, change clustered primary key to non-clustered ,and created a clustered index on the new identity field

Poll
How do i Add identity field to an existing table, change clustered primary key to non-clustered ,and created a clustered index on the new identity field

0% - 0 votes The database will be in use at the moment I try to change the indexes round - so my primary concern that I want to avoid, is that at some point in the process the PK constraint will not exist on the table. I want to be protected against any risk of duplicate keys being inserted.
0% 0 votes
0% - 0 votes i.e. I can't just drop the primary key and recreate it.
0% 0 votes
0% - 0 votes
0% 0 votes
Member votes: 0, Guest votes: 0. You don't have permission to vote in this poll
Author
Message
Maina-456685
Maina-456685
SSC Rookie
SSC Rookie (31 reputation)SSC Rookie (31 reputation)SSC Rookie (31 reputation)SSC Rookie (31 reputation)SSC Rookie (31 reputation)SSC Rookie (31 reputation)SSC Rookie (31 reputation)SSC Rookie (31 reputation)

Group: General Forum Members
Points: 31 Visits: 281
1) ALTER TABLE TableX ADD FieldA indentity(1,1)

2) Drop the existing clustered index first (IX_TableX_FieldB):
DROP INDEX TableX.IX_TableX_FieldB
3) Create a (temporary) UNIQUE constraint on the unique fields referenced in the primary key
ALTER TABLE TableX
ADD CONSTRAINT UQ_TableX UNIQUE(FieldA)
4) Drop the PRIMARY KEY
ALTER TABLE TableX
DROP CONSTRAINT PK_TableX
5) Recreate the PRIMARY KEY as CLUSTERED
ALTER TABLE TableX
ADD CONSTRAINT PK_TableX PRIMARY KEY CLUSTERED(FieldA)
6) Drop the temporary UNIQUE constraint
GilaMonster
GilaMonster
SSC Guru
SSC Guru (217K reputation)SSC Guru (217K reputation)SSC Guru (217K reputation)SSC Guru (217K reputation)SSC Guru (217K reputation)SSC Guru (217K reputation)SSC Guru (217K reputation)SSC Guru (217K reputation)

Group: General Forum Members
Points: 217609 Visits: 46278
Could you clearly state your question in your post please?

Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

We walk in the dark places no others will enter
We stand on the bridge and no one may pass


Maina-456685
Maina-456685
SSC Rookie
SSC Rookie (31 reputation)SSC Rookie (31 reputation)SSC Rookie (31 reputation)SSC Rookie (31 reputation)SSC Rookie (31 reputation)SSC Rookie (31 reputation)SSC Rookie (31 reputation)SSC Rookie (31 reputation)

Group: General Forum Members
Points: 31 Visits: 281
Hi,

I need to:

1) Add a new identity field on table TableA. TableA has a composite Primary key

2) Change the clustered primary key to non-clustered

3) Create clustered index on the new identity field


I need the solution that will take the shortest time to run.

Find the table below:


CREATE TABLE [dbo].[TableA](
[TableAGUID] [uniqueidentifier] NOT NULL CONSTRAINT [DF_TableA_TableAGUID] DEFAULT (newid()),
[QueryGUID] [uniqueidentifier] NOT NULL,
....

[CreatedOnDate] [datetime] NOT NULL

CONSTRAINT [PK_TableA_1] PRIMARY KEY CLUSTERED
(
[TableAGUID] ASC,
[QueryGUID] ASC
)
) ON [PRIMARY]

GO
ALTER TABLE [dbo].[TableA] CHECK CONSTRAINT [FK_TableA_MessageType]
GO
ALTER TABLE [dbo].[TableA] WITH CHECK ADD CONSTRAINT [FK_TableA_Query] FOREIGN KEY([QueryGUID])
REFERENCES [dbo].[Query] ([QueryGUID])
GO
ALTER TABLE [dbo].[TableA] CHECK CONSTRAINT [FK_TableA_Query]

ALTER TABLE dbo.TableA
ADD TableAID IDENTITY(1,1)
GO

DROP INDEX [_dta_index_TableA_1] ON [dbo].[TableA]
GO
---Temporary
ALTER TABLE dbo.TableA
ADD CONSTRAINT UQ_TableA_1 UNIQUE(TableAGUID,QueryGUID)
GO

ALTER TABLE dbo.TableA
DROP CONSTRAINT PK_TableA_1
GO

ALTER TABLE dbo.TableA
ADD CONSTRAINT PK_TableA_ID PRIMARY KEY CLUSTERED (TableAID)
ON PRIMARY
GO

ALTER TABLE dbo.TableA
DROP CONSTRAINT UQ_TableA_1
GO

CREATE NONCLUSTERED INDEX [_dta_index_TableA_1] ON [dbo].[TableA]
(
[QueryGUID] ASC,
[CreatedOnDate] ASC
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