Friday 19 April 2013

Disabling clustered Index may create problem


 Disabling clustered INDEX may create problem. Once we disable the clustered index it will disable all the foreign key constraints of that table. And even after enable/rebuild clustered index the all foreign key constraints still disable it will not get enable, we have to enable all foreign key constraints manually.
CREATE TABLE tblTest
(
id INT,
name VARCHAR(100),
sal MONEY,
CONSTRAINT tbltest_PK PRIMARY KEY  (id )
)
GO
CREATE TABLE tblProject
(
pid INT PRIMARY KEY,
pname VARCHAR(100)
)

GO
CREATE TABLE tblPerosn_Project
(
ID INT constraint PersonProject_id_fk references  tblTest(id),
pid INT constraint personProject_pid_fk references tblProject(pid)
)
GO
Create view dbo.vtest
with schemabinding
as
select id ,name from dbo.tbltest
go
create unique clustered index vidx on dbo.vtest(id)
GO
Insert into tblTest(id,name)
values(1,'arun'),(2,'dhiraj'),(3,'chunu')
Insert into tblProject(pid,pname)
values(1,'CT'),(2,'wy'),(3,'UT')

Insert into tblPerosn_Project(id, pid)
values(1,2),(1,3),(1,1)
-- Now lets try to insert some wrong data

Insert into tblTest(id,name)
values(1,'syam')
/*

Msg 2627, Level 14, State 1, Line 1
Violation of PRIMARY KEY constraint 'tbltest_PK'. Cannot insert duplicate key in object 'dbo.tblTest'.
*/

Insert into tblPerosn_Project(id, pid)
values(1,5)
/*

Msg 547, Level 16, State 0, Line 1
The INSERT statement conflicted with the FOREIGN KEY constraint "personProject_pid_fk".
The conflict occurred in database "AdventureWorks", table "dbo.tblProject", column 'pid'.
*/

-- Here we are getting error, it means constraints are working properly.
 

--==========================================================
--Now , disable the below indexes
select object_name(object_id) as tablename, * from sys.indexes where object_id in (object_id('tblTest')
,object_id('tblProject'),object_id('tblPerosn_Project'),object_id('vtest'))
/*
Tablename object_id name      index_id  type      type_desc
------------       ----------         -----------------  ------    ----------
tblProject         132195521 PK__tblProje__DD37D91A09C96D33                  CLUSTERED
tblPerosn_Project  196195749 NULL                                            HEAP
tblTest                               2039678314         tbltest_PK                   CLUSTERED
vtest                                  vidx                                             CLUSTERED
ALTER INDEX tbltest_PK ON tblTest DISABLE;
/*
Warning: Foreign key 'PersonProject_id_fk' on table 'tblPerosn_Project'
referencing table 'tblTest' was disabled as a result of disabling the index 'tbltest_PK'.
*/
--================================================================
     --1. Disabling clustered index will disable all foreign key relation

================================================================
--Again try to insert wron record rows
Insert into tblPerosn_Project(id, pid)
values(10,2)
/*
(1 row(s) affected)
*/
================================================================
--2 Metadata  will still availble of that index even after disable,
--But for index on view metadata will get deleted.
================================================================
select object_name(object_id) as tablename, * from sys.indexes where object_id in (object_id('tblTest')
,object_id('tblProject'),object_id('tblPerosn_Project'))
 /*
tablename object_id name
tblProject    132195521 PK__tblProje__DD37D91A09C96D33
tblPerosn_Project  228195863 NULL
tblTest   2039678314    tbltest_PK
*/

--================================================================
 --3.Data will not available for process of that table
--================================================================
select * from tblTest
/*
Msg 8655, Level 16, State 1, Line 2
The query processor is unable to produce a plan because the index 'tbltest_PK' on table
or view 'tblTest' is disabled.
*/
--==========================================================
-- 4 Even after enable/rebuild clustered index the foreigen key
--   will be disable.
--===========================================================
-- Now made correction enable your index
Alter index tbltest_PK on tblTest rebuild
--Again try to insert wrong record
Insert into tblPerosn_Project(id, pid)
values(10,2)
/*
(1 row(s) affected)
*/
-- Still its accepting wrong records even we enable the the clustered index.

 

 


 

 

Data Mesh

  Data Mesh is a relatively new approach to managing and organizing data within organizations, especially large enterprises. It advocates fo...