Take the 2-minute tour ×
Database Administrators Stack Exchange is a question and answer site for database professionals who wish to improve their database skills and learn from others in the community. It's 100% free, no registration required.

In MySQL, when using InnoDB, what happens to the way data is laid out on disk when the primary key is dropped? Also, what happens when a new primary key is added?

share|improve this question
add comment

migrated from stackoverflow.com Nov 20 '13 at 13:08

This question came from our site for professional and enthusiast programmers.

1 Answer

If there is a not null unique key, that will be used as an internal primary key. If there is no candidate for a primary key, a 6-byte internal identified will be used as a hidden primary key that, unlike Oracle's ROWID, it cannot be accessed by the user.

As that implies that potentially an arbitrary value will be used to cluster the data, it is always recommended to have an explicit primary key.

When a new primary key is added, essentially the table (B+tree) is fully rebuilt around the new primary key value, including the secondary keys. That is why any ALTER TABLE operation involving the primary key will be very costly (and in MySQL 5.6 cannot be done online).

share|improve this answer
    
one off the reasons why you indeed should create an explicit primary key with the InnoDB engine -> dba.stackexchange.com/questions/48072/… –  Raymond Nijland Dec 9 '13 at 22:24
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.