MySQL (InnoDB analysis): ---table (index organized table, attached _rowid)

One, the concept of index-organized tables

  • In InnoDB, tables are stored according to the order of the primary key composition , this storage method is called an index-organized table

Second, the confirmation rules of the table primary key

  • In InnoDB, each table will have a primary key
  • The rules for confirming the primary key are as follows:
  • ① The primary key specified when the table was created
  • ② If the primary key is not explicitly defined when the table is created , select or create the primary key as follows:
  1. First determine whether there is a non-empty unique index (unique, not null) in the table . If there is, the column is the primary key
  2. When there are multiple non-empty unique indexes in the table, InnoDB will choose the first non-empty unique index defined when the table is built as the primary key . It should be noted here that the choice of the primary key is based on the order in which the index is defined, not the order of the columns when the table is built (there is a demonstration case below)
  3. If the above conditions are not met, InnoDB will automatically create a 6-byte pointer

3. Demonstration case (_rowid)

 

  • The following demonstrates that the primary key is not set when the table is created, and the process of verifying that the system automatically assigns the primary key

_rowid introduction:

  • Role: can display the primary key of the table
  • Remarks : _rowid can only be used when a single column is used as the primary key, and it cannot be used for a primary key composed of multiple columns (see demonstration case ② below)

Demonstration case①

  • Step 1 : Create a table. Where c and d are non-empty and unique, so MySQL will choose one of these two fields as the primary key
create table z(
    a int not null,
    b int null,
    c int not null,
    d int not null,
    unique key(b),
    unique key(d),
    unique key(c)
);

  • Step 2 : Insert three rows of data into the table at this time
insert into z select 1,2,3,4;
 
insert into z select 5,6,7,8;
 
insert into z select 9,10,11,12;

  • Step 3 : Now use _rowid to display the primary key of the table, you can see that _rowid points to the d column, so d is the primary key recognized by InnoDB by default
select a,b,c,d,_rowid from z;

 

Demonstration case ②

  • Because _rowid cannot be used for the primary key composed of multiple columns, the following simple verification
  • Step 1: Create a table with the composition of a and b as the primary key
create table a(
    a int,
    b int,
    primary key(a,b)
)ENGINE=InnoDB;

  • Step 2 : Insert a row of data
insert into a select 1,1;

  • Step 3 : Use _rowid again for query, error
select a,_rowid from a;

Guess you like

Origin blog.csdn.net/m0_46405589/article/details/114483849