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.
(
id INT,
name VARCHAR(100),
sal MONEY,
CONSTRAINT tbltest_PK PRIMARY KEY (id )
)
GO
CREATE TABLE tblProject
(
pid INT PRIMARY KEY,
pname VARCHAR(100)
)
(
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')
values(1,2),(1,3),(1,1)
-- Now lets try to insert some wrong data
values(1,'syam')
/*
Violation of PRIMARY KEY constraint 'tbltest_PK'. Cannot insert duplicate key in object 'dbo.tblTest'.
*/
values(1,5)
/*
The INSERT statement conflicted with the FOREIGN KEY constraint "personProject_pid_fk".
The conflict occurred in database "AdventureWorks", table "dbo.tblProject", column 'pid'.
*/
--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.
No comments:
Post a Comment