Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 SQL Server Administration (2008)
 changing a pk set as noncluster to cluster

Author  Topic 

sarahmfr
Posting Yak Master

214 Posts

Posted - 2012-02-05 : 17:39:10
Hi All
I have in my database a table that has a primary key that was set by mistake to be a noncluster and I have a lot of foreign key in other tables pointint to this primary key.
If I changed now its type from noncluster to cluster will this affect the other tables will this affect my data
Thanks

sarah

robvolk
Most Valuable Yak

15732 Posts

Posted - 2012-02-05 : 18:16:08
To make that change will require dropping the foreign keys and the primary key, then re-creating them after the primary key is rebuilt as a clustered index. During the time the constraints are dropped you could insert or update data that would cause subsequent violations. The best way to do this is to shut down any applications that can do such operations, or find the quietest time to make the change.
Go to Top of Page

sarahmfr
Posting Yak Master

214 Posts

Posted - 2012-02-05 : 18:41:59
What you mean by the following
During the time the constraints are dropped you could insert or update data that would cause subsequent violations.

If i shut down the system no one will be using it and dropped all the foreign keys referencing this primary key and dropped this primary key and then recreated the primary key as cluster and recreated all the foreign keys will that work

Another thing is the fragmentation high because my mistake the primary key was set to be a noncluster one
a lot of the composite keys that consist of this primary key and another one also are highly fragmented
is all that because the primary key was set by mistake to be non clustered


sarah
Go to Top of Page

robvolk
Most Valuable Yak

15732 Posts

Posted - 2012-02-05 : 19:17:47
quote:
If i shut down the system no one will be using it and dropped all the foreign keys referencing this primary key and dropped this primary key and then recreated the primary key as cluster and recreated all the foreign keys will that work
Yes, as I said this is the safest option.

Keep in mind that clustered/non-clustered indexes are separate things from the primary key. If you create or drop a clustered index it will rebuild all non-clustered indexes on the table. This rebuild should also correct the fragmentation of the non-clustered indexes. If not, you can run ALTER INDEX...REORGANIZE on your indexes to defragment them.
Go to Top of Page

GilaMonster
Master Smack Fu Yak Hacker

4507 Posts

Posted - 2012-02-05 : 20:26:44
quote:
Originally posted by sarahmfr

Another thing is the fragmentation high because my mistake the primary key was set to be a noncluster one
a lot of the composite keys that consist of this primary key and another one also are highly fragmented
is all that because the primary key was set by mistake to be non clustered


That won't be the cause of fragmentation.

--
Gail Shaw
SQL Server MVP
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2012-02-06 : 03:17:24
Fragmentation issue also being asked in this thread:

http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=170992

One thread only per problem please, otherwise we all waste time answering things that have already been answered elsewhere.
Go to Top of Page

GilaMonster
Master Smack Fu Yak Hacker

4507 Posts

Posted - 2012-02-06 : 05:24:53
He's also posted all 3 over at SSC.

--
Gail Shaw
SQL Server MVP
Go to Top of Page

sarahmfr
Posting Yak Master

214 Posts

Posted - 2012-02-07 : 12:55:20
Thanks a lot

sarah
Go to Top of Page
   

- Advertisement -