Is a clustered index created on primary key on default as soon as we create a table? can we create a clustered index on some other attribute except this primary key ? if yes, what happens in that case does the default ordering of table on primary key gets destroyed and gets physically ordered on tha
-
I am taggeging few people aas they are too expensive for ask to answer:
-
Answer:
In innodb (mysql), there is always exactly one clustered index per table: the primary key. If you don't specify a primary key, the engine creates a hidden auto-increment one for you. If you change the primary key, the table will rebuild itself s.t. it's physically ordered on disk by the primary key. I'm not sure about SQL Server. But in general, there can only be one clustered index on a table at a time (after all, you can only write a table to disk in one order). If you were to change the columns in a clustered index, the table would have to rebuild. Hopefully all this nonsense about physical disk layout will be archaic in the future, when we have cheaper SSDs :)
Ted Suzman at Quora Visit the source
Other answers
in SQL server, a primary key is not created unless explicitly specified. Same goes for a clustered index. A clustered index doesn't have to be a primary key and vice versa. There is no default ordering of a table. In fact data in a table is by definition not ordered. Regardless of any primary keys or indexes. Database engine will try to keep similar values of a clustered index next to each other, but there is no guarantee as to what order they will be retrieved in. if you want to change the columns in the clustered index you would first have to drop it. This will put the table back to a heap state. you can then create a new clustered index which will re-organize data into a different B-tree.
Mordechai Danielov
With SQL Server, by default, if you do nothing else, when you declare a primary key, that is automatically built as a clustered index. But it does not have to be. You can declare it to be nonclustered. Then you can add a clustered index to some other column(s) on the table. One point worth noting, since the clustered index acts as the definition of table storage, each row must be uniquely identified. If the keys you've defined for your clustered index are not unique, then SQL Server will add an additional value, called a uniquifier, to make sure that each row can be uniquely identified. When this clustered index gets created, it does reorder the data in the order of the key values that define the index. In general, this should be considered a good thing. This is because the clustered index, which holds the data, usually should be placed on the most common access path to the data. Usually that access path is the primary key, but it doesn't have to be.
Grant Fritchey
By default SQL Server creates a Clustered Index (CI) on a Primary key. But yes, you can also create a PK with a Non-Clustered Index (NCI) by specifying it, but this index will be a non-covering index. And thus you can create a CI on non-Key column. The physical ordering of the Table/Index is with Clustered Index key only, not on the PK, but that's also not guaranteed. Check my blog posts on: - http://sqlwithmanoj.com/2011/03/02/clustered-vs-nonclustered-indexes-and-data-sorting/ - http://sqlwithmanoj.com/2013/06/02/clustered-index-do-not-guarantee-physically-ordering-or-sorting-of-rows/ - http://sqlwithmanoj.com/2013/06/10/clustered-index-will-not-always-guarantee-sorted-rows/ ~http://SQLwithManoj.com
Manoj Pandey
Related Q & A:
- Can I create a second filestream container on an existing SQL Server 2008 database without going offline?Best solution by Database Administrators
- How can i create a mobile application server?Best solution by Stack Overflow
- What is Hash Table, how to create a hash table?Best solution by Stack Overflow
- How to create a table in PHP with MySQL?Best solution by Stack Overflow
- How to create a table in gmail?Best solution by Web Applications
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.