MySQL Why do digital auto-increment primary key?

Why do 1.MySQL digital auto-increment primary key?

First of all why we use is of type int, not type varchar

  • int is always four bytes is fixed, and a char is between 1 and 255 bytes


  1. Small footprint, saving CPU overhead
  2. In use, typically index on the primary key, use plastic more indexes can be loaded into memory, improve performance
  3. Use plastic only be used AUTO_INCREAMENT


  • Generating lock contention problems
    at some point, user A, user B, the user may simultaneously or sequentially propionate (front job is not yet completed) operations on the data in Table A query or update. When a thread involves updating operation, we need to get access to exclusive. During the update, all other threads that want to access the table must wait until its update is complete. At this time, the lock will lead to competition problems.

2. Why can not live without a primary key?

  • Primary key acts as a unique identifier to ensure that we can secure a line change or delete specified in the table

Into two conditions need to have primary keys

  1. No two rows can have the same primary key value
  2. Each row must have a primary key (primary key column does not allow NULL value)

3. Select the order of the primary key?

  • (Only select this one) regardless of the choice of a service content from increasing as the primary key field
    • Use increment primary key, then the new data are sequentially inserted at the end, when a filled, the operating system opens another page for writing; if non-use self-energizing a primary key, the master key value is random, then inserted when you need to insert into a certain location of the existing index page. Then the data will need to move the target page, and then read from disk, adds a lot of overhead, and prone to memory fragmentation.
  • reference
Published 257 original articles · won praise 223 · views 320 000 +

Guess you like