Database Research & Development

  • Home
  • NoSQL
    • NoSQL
    • Cassandra
  • Databases
    • Database Theory
    • Database Designing
    • SQL Server Coding Standards
    • SQL Server
    • PostgreSQL
    • MySQL
    • Greenplum
    • Linux
  • Interviews
    • SQL Server Interviews
    • MySQL Interviews
    • SQL Puzzles
  • DBA Scripts
    • SQL Server DBA Scripts
    • PostgreSQL DBA Scripts
    • MySQL DBA Scripts
    • Greenplum DBA Scripts
  • Home
  • Blog Archives !
  • (: Laugh@dbrnd :)
  • Contact Me !
sqlserverinterviews
Home 2017 February SQL Server Interview: A Golden question, Can we disable the Clustered Index?

SQL Server Interview: A Golden question, Can we disable the Clustered Index?

SQLServerSadClusteredIndex

This article is half-done without your Comment! *** Please share your thoughts via Comment ***

 Many people asked me, Are you doing an only blogging or consulting? Every time my clear answer was, I am doing full-time Job as DB Lead in one of the leading IT Companies (working for 9 hours to 10 hours) and

As you guys know, that I have shared loads of SQL Server Interview questions and answers.
Let me tell you a story behind this,

Whenever I free in a weekend; I always try to attend walk-in interviews which have been arranged by most of the IT Companies so that I can find some real interview questions and can share with you guys.

Wait wait wait…. I know most of the people are thinking that I am running for a job… which is false.

I am not going to change my current company, but I am attending most of the interviews because I want to measure myself so that can share something more and can also take superb interviews that I am also doing in my current company. Additionally spending minimum 3 hours to 4 hours every day for blogging…

I know, you guys are suffering my blah blah blah…. from the above two paragraphs, but for a golden question, you guys could suffer sometimes.

Last week, I attended a walk-in interview for DBA position at Tech Mahindra Hyderabad, India.
They asked few questions on Clustered Index, and I was empty entirely.

Questions were:

  • Can we disable the Clustered Index?
  • Have you ever tried to disable Clustered Index?
  • Once you disable the Clustered Index, Can we access the table?
  • Your table has Clustered Index + NonClustered Indexes. If you turn off the Clustered Index, Can you access your table using NonClustered Indexes?
  • Can we take the table in offline mode by disabling the Clustered Index?
  • Ok, If you disable the Clustered Index, how can you enable it again?

My simple answers were:

  • I don’t know it is possible or not
  • I never tried to disable the Primary key, and if it is possible
  • I don’t know how we can find the data path to access the table

Now, I don’t want to write more blah, blah, blah… Find the below full demonstration and test it yourself and at the end, you will get all answers to above questions.

All the best....

Now, one more question to you guys. If you have heap table, what ?, Can anyone share their thoughts via putting some comments?

Create a sample table:

1
2
3
4
5
6
7
CREATE TABLE tbl_Students
(
StudID INT IDENTITY(1,1)
,Name VARCHAR(10)
,CONSTRAINT pk_tbl_Students_StudID PRIMARY KEY(StudID)
)
GO

Create a NonClustered index on Name column:

1
2
CREATE NONCLUSTERED INDEX idx_tbl_Students_Name ON tbl_Students(Name)
GO

Insert 10000 dummy records:

1
2
INSERT INTO tbl_Students(Name) VALUES ('Anvesh')
GO 10000

Now, Disable the Clustered Index:
It also disables the all Nonclustered Indexes of a table. You can check below warning message***

1
2
ALTER INDEX pk_tbl_Students_StudID ON dbo.tbl_Students DISABLE
GO
1
Warning: Index 'idx_tbl_Students_Name' on table 'tbl_Students' was disabled as a result of disabling the clustered index on the table.

Try to select a table:
You will get a below error, You cannot select the table data because of query optimizer unable to find the access path of data.

1
2
SELECT *FROM tbl_Students
GO
1
2
Msg 8655, Level 16, State 1, Line 1
The query processor is unable to produce a plan because the index 'pk_tbl_Students_StudID' on table or view 'tbl_Students' is disabled.

The solution is, Rebuild your Clustered Index and you will get your table back:

1
2
ALTER INDEX pk_tbl_Students_StudID ON dbo.tbl_Students REBUILD
GO
Feb 18, 2017Anvesh Patel
SQL Server: You must know about the sys.dm_os_wait_statsSQL Server Interview: How to prevent 'SELECT *' using T-SQL Script?
Comments: 7
  1. Amit
    February 23, 2017 at 10:28 am

    Thanks for sharing the update, in general, if we have heap table in database so it doesn’t contain any dependency with indexes. It does work faster in bulk operation.

  2. Jay
    February 23, 2017 at 9:34 pm

    Perfect !!! Loved to learn this and another true facts there is no ENABLE OPTION you can only REBUILD or DROP and CREATE the index.

    This is not the case with non clustered indexes.

    • Anvesh Patel
      Anvesh Patel
      February 24, 2017 at 6:55 pm

      Yes true…

    • pabhat
      April 25, 2017 at 5:51 am

      yes good, love to visit this site..at the same time we can disable the non clustered index and we can select the table.

  3. Anuraag
    May 26, 2017 at 12:34 pm

    If a table is created with a Primary key, a clustered index will be created automatically. We cannot drop a clustered index through T-script but we can drop through Object explorer(Table–>Indexes–>Right click on index and click on delete). It will delete both Primary key and clustered index and that table will be converted to Heap table since that table has no clustered index. We can access a Heap table.

  4. santosh Dantuluri
    May 10, 2018 at 7:06 am

    Thanks for sharing.Nice question.

  5. Nikhil
    December 19, 2019 at 10:41 am

    I have a feeling that this article will help me somewhere in future, thanks for the article

Anvesh Patel
Anvesh Patel

Database Engineer

ImageFebruary 18, 2017 SQL Server, SQL Server InterviewAnvesh Patel, Clustered Inded, database, database research and development, dbrnd, heap table, Nonclustered Index, Primary key, SQL Query, SQL Server, SQL Server Administrator, SQL Server Error, SQL Server Monitoring, SQL Server Performance Tuning, SQL Server Programming, SQL Server Tips and Tricks, TSQL
About Me!

I'm Anvesh Patel, a Database Engineer certified by Oracle and IBM. I'm working as a Database Architect, Database Optimizer, Database Administrator, Database Developer. Providing the best articles and solutions for different problems in the best manner through my blogs is my passion. I have more than six years of experience with various RDBMS products like MSSQL Server, PostgreSQL, MySQL, Greenplum and currently learning and doing research on BIGData and NoSQL technology. -- Hyderabad, India.

About DBRND !

dbrnd

This is a personal blog (www.dbrnd.com).

Any views or opinions represented in this blog are personal and belong solely to the blog owner and do not represent those of people, institutions or organizations that the owner may or may not be associated with in professional or personal capacity, unless explicitly stated.

Feel free to challenge me, disagree with me, or tell me I’m completely nuts in the comments section of each blog entry, but I reserve the right to delete any comment for any reason whatsoever (abusive, profane, rude, or anonymous comments) - so keep it polite.

The content of this website is protected by copyright. No portion of this website may be copied or replicated in any form without the written consent of the website owner.

Recent Comments !
  • Anvesh Patel { Sure will do... } – May 27, 12:43 PM
  • Anvesh Patel { Great... } – May 27, 12:41 PM
  • Anvesh Patel { Great... } – May 27, 12:39 PM
  • Anvesh Patel { Great... } – May 27, 12:36 PM
  • Anvesh Patel { Great... } – May 27, 12:28 PM
  • Anvesh Patel { Great... } – May 27, 12:27 PM
  • Anvesh Patel { Great... } – May 27, 12:16 PM
  • Older »
Follow Me !
  • facebook
  • linkedin
  • twitter
  • youtube
  • google
  • flickr
© 2015 – 2019 All rights reserved. Database Research & Development (dbrnd.com)
Posting....