Mysql Index Overview (for personal study and review)

Mysql Index Overview


Index Introduction

  • Indexing method for recording a plurality of sets of fields to sort.

  • Similar to the book's table of contents

  • Index type comprising: Btree, B + tree, hash


Index pros and cons

  •  Index advantage

    - by creating a unique index, you can guarantee the uniqueness of each row of data in the database

    - can accelerate the speed of data retrieval

  • Index disadvantages

    - When the data in the table to add, delete and modify, the index should be dynamic maintenance, reduce maintenance speed data

    - The index needs to occupy physical space



MySQL key type


Key type

  • INDEX: general index

  • UNIQUE: The only index

  • FULLTEXT: full-text index

  • PRIMARY KEY: primary key

  • POREIGN KEY: foreign keys


General index INDEX

  • Instructions for use

    - a table can have multiple fields INDEX

    - allows repeated field value, and can impart a NULL value

    - often do the query criteria field to field INDEX

    key flag MUL -INDEX field

  • Construction of the table specified index fields

    -index (field 1), index (field 2) ...

mysql> create table test1(
    -> id char(6) not null,
    -> name varchar(4) not null,
    -> age int(3) not null,
    -> gender enum('male','female'),
    -> index(id),index(name)
    -> );
Query OK, 0 rows affected (0.06 sec)
mysql> desc test1;
+--------+-----------------------+------+-----+---------+-------+
| Field  | Type                  | Null | Key | Default | Extra |
+--------+-----------------------+------+-----+---------+-------+
| id     | char(6)               | NO   | MUL | NULL    |       |
| name   | varchar(4)            | NO   | MUL | NULL    |       |
| age    | int(3)                | NO   |     | NULL    |       |
| gender | enum('male','female') | YES  |     | NULL    |       |
+--------+-----------------------+------+-----+---------+-------+
4 rows in set (0.00 sec)
  • Set index field in the existing table

    -create index index name on table name (field name);

mysql> create index age on test1(age);
Query OK, 0 rows affected (0.02 sec)
Records: 0  Duplicates: 0  Warnings: 0
mysql> desc test1;
+--------+-----------------------+------+-----+---------+-------+
| Field  | Type                  | Null | Key | Default | Extra |
+--------+-----------------------+------+-----+---------+-------+
| id     | char(6)               | NO   | MUL | NULL    |       |
| name   | varchar(4)            | NO   |     | NULL    |       |
| age    | int(3)                | NO   | MUL | NULL    |       |
| gender | enum('male','female') | YES  |     | NULL    |       |
+--------+-----------------------+------+-----+---------+-------+
4 rows in set (0.00 sec)
  • Delete the specified table index fields

    -drop index index name on table;

mysql> drop index name on test1;
Query OK, 0 rows affected (0.34 sec)
Records: 0  Duplicates: 0  Warnings: 0
mysql> desc test1;
+--------+-----------------------+------+-----+---------+-------+
| Field  | Type                  | Null | Key | Default | Extra |
+--------+-----------------------+------+-----+---------+-------+
| id     | char(6)               | NO   | MUL | NULL    |       |
| name   | varchar(4)            | NO   |     | NULL    |       |
| age    | int(3)                | NO   |     | NULL    |       |
| gender | enum('male','female') | YES  |     | NULL    |       |
+--------+-----------------------+------+-----+---------+-------+
4 rows in set (0.00 sec)
  • View index information table

    -show index from 表名;

mysql> show index from test1\G;
*************************** 1. row ***************************
        Table: test1
   Non_unique: 1
     Key_name: id
 Seq_in_index: 1
  Column_name: id
    Collation: A
  Cardinality: 0
     Sub_part: NULL
       Packed: NULL
         Null: 
   Index_type: BTREE
      Comment: 
Index_comment: 
*************************** 2. row ***************************
        Table: test1
   Non_unique: 1
     Key_name: age
 Seq_in_index: 1
  Column_name: age
    Collation: A
  Cardinality: 0
     Sub_part: NULL
       Packed: NULL
         Null: 
   Index_type: BTREE
      Comment: 
Index_comment: 
2 rows in set (0.00 sec)


primary key primary key

  • basic concept

    - A table can have only one primary key field

    - the corresponding duplicate field values ​​are not allowed, and assigned NULL values ​​are not allowed

    - If there are multiple fields as the primary key, called the composite primary key must be created together

    - KEY mark the primary key field is PRI

    - usually used in conjunction with the AUTO_INCREMENT

    - often Table record field can uniquely identify the primary key of the record number field [field]

  • Construction of the table when the specified primary key field

    -primary key (field names)

mysql> create table test2(
    -> id int(3) auto_increment,
    -> name varchar(4) not null,
    -> age int(2) not null,
    -> primary key(id)
    -> );
Query OK, 0 rows affected (0.05 sec)
mysql> desc test2;
+-------+------------+------+-----+---------+----------------+
| Field | Type       | Null | Key | Default | Extra          |
+-------+------------+------+-----+---------+----------------+
| id    | int(3)     | NO   | PRI | NULL    | auto_increment |
| name  | varchar(4) | NO   |     | NULL    |                |
| age   | int(2)     | NO   |     | NULL    |                |
+-------+------------+------+-----+---------+----------------+
3 rows in set (0.00 sec
  • Set primary key field in the existing table

    -alter table table add primary key (field name);

mysql> desc test2;
+-------+------------+------+-----+---------+-------+
| Field | Type       | Null | Key | Default | Extra |
+-------+------------+------+-----+---------+-------+
| id    | int(3)     | NO   |     | NULL    |       |
| name  | varchar(4) | NO   |     | NULL    |       |
| age   | int(2)     | NO   |     | NULL    |       |
+-------+------------+------+-----+---------+-------+
3 rows in set (0.01 sec)
mysql> alter table test2 add primary key(name);
Query OK, 0 rows affected (0.05 sec)
Records: 0  Duplicates: 0  Warnings: 0
mysql> desc test2;
+-------+------------+------+-----+---------+-------+
| Field | Type       | Null | Key | Default | Extra |
+-------+------------+------+-----+---------+-------+
| id    | int(3)     | NO   |     | NULL    |       |
| name  | varchar(4) | NO   | PRI | NULL    |       |
| age   | int(2)     | NO   |     | NULL    |       |
+-------+------------+------+-----+---------+-------+
3 rows in set (0.00 sec)
  • Remove table PRIMARY KEY field

    -alter table table name drop primary key; (auto_increment if you need to remove and then remove the primary key, remove auto_incrememt is reassigned to the field type)

ysql> desc test2;
+-------+------------+------+-----+---------+-------+
| Field | Type       | Null | Key | Default | Extra |
+-------+------------+------+-----+---------+-------+
| id    | int(3)     | NO   | PRI | NULL    |       |
| name  | varchar(4) | NO   |     | NULL    |       |
| age   | int(2)     | NO   |     | NULL    |       |
+-------+------------+------+-----+---------+-------+
3 rows in set (0.00 sec)
mysql> alter table test2 drop primary key;
Query OK, 0 rows affected (0.31 sec)
Records: 0  Duplicates: 0  Warnings: 0
mysql> desc test2;
+-------+------------+------+-----+---------+-------+
| Field | Type       | Null | Key | Default | Extra |
+-------+------------+------+-----+---------+-------+
| id    | int(3)     | NO   |     | NULL    |       |
| name  | varchar(4) | NO   |     | NULL    |       |
| age   | int(2)     | NO   |     | NULL    |       |
+-------+------------+------+-----+---------+-------+
3 rows in set (0.01 sec)


foreign key foreign key

  • basic concept

    - let the value of the current table field in another field in a table worthy choice within range.

  • Using the foreign key conditions

    - Table storage engine must be innodb

    - field type to be consistent

    - if the index field must be referred to a type (primary key)

  • Basic Usage

mysql> create table yuangong(
    -> yg_id int(4) auto_increment,
    -> name char(16) not null,
    -> primary key(yg_id)
    -> );
Query OK, 0 rows affected (0.31 sec)
mysql> create table gongzi(
    -> gz_id int(4) not null,
    -> name char(15) not null,
    -> gz float(6,2) not null default 0,
    -> index(name),
    -> foreign key(gz_id) references yuangong(yg_id)
    -> on update cascade on delete cascade
    -> );
Query OK, 0 rows affected (0.32 sec)
  • Remove the foreign key field

    -alter table table drop foreign key constraint name;

mysql> show create table gongzi\G;
*************************** 1. row ***************************
       Table: gongzi
Create Table: CREATE TABLE `gongzi` (
  `gz_id` int(4) NOT NULL,
  `name` char(15) NOT NULL,
  `gz` float(6,2) NOT NULL DEFAULT '0.00',
  KEY `name` (`name`),
  KEY `gz_id` (`gz_id`),
  CONSTRAINT `gongzi_ibfk_1` FOREIGN KEY (`gz_id`) REFERENCES `yuangong` (`yg_id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=latin1
1 row in set (0.01 sec)

mysql> alter table gongzi drop foreign key gongzi_ibfk_1;
Query OK, 0 rows affected (0.01 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> show create table gongzi\G;
*************************** 1. row ***************************
       Table: gongzi
Create Table: CREATE TABLE `gongzi` (
  `gz_id` int(4) NOT NULL,
  `name` char(15) NOT NULL,
  `gz` float(6,2) NOT NULL DEFAULT '0.00',
  KEY `name` (`name`),
  KEY `gz_id` (`gz_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
1 row in set (0.00 sec)


UNIQUE index

  • basic concept

    - UNIQUE represents uniqueness, the same field may have a plurality of fields having a unique

  • UNIQUE index fields specified when creating a table

    See New Field structure test3 table can be found KEY flag UNIQUE field is the UNI; Further, since the field name must meet the "NOT NULL" non-empty constraint, so after it is set to UNIQUE automatically becomes PRIMARY KEY primary key field :

mysql> create table test3(
    -> id char(6),
    -> name varchar(4) not null,
    -> age int(3) not null,
    -> unique(id),unique(name),index(age)
    -> );
Query OK, 0 rows affected (0.36 sec)
mysql> desc  test3;
+-------+------------+------+-----+---------+-------+
| Field | Type       | Null | Key | Default | Extra |
+-------+------------+------+-----+---------+-------+
| id    | char(6)    | YES  | UNI | NULL    |       |
| name  | varchar(4) | NO   | PRI | NULL    |       |
| age   | int(3)     | NO   | MUL | NULL    |       |
+-------+------------+------+-----+---------+-------+
3 rows in set (0.00 sec)
  • 删除UNIQUE索引,在已有的表中设置UNIQUE索引字段

mysql> drop index name on test3;
Query OK, 0 rows affected (0.07 sec)
Records: 0  Duplicates: 0  Warnings: 0
mysql> desc test3;
+-------+------------+------+-----+---------+-------+
| Field | Type       | Null | Key | Default | Extra |
+-------+------------+------+-----+---------+-------+
| id    | char(6)    | YES  | UNI | NULL    |       |
| name  | varchar(4) | NO   |     | NULL    |       |
| age   | int(3)     | NO   | MUL | NULL    |       |
+-------+------------+------+-----+---------+-------+
3 rows in set (0.00 sec)
  • 在已有表中建立UNIQUE索引

mysql> create unique index name on test3(name);
Query OK, 0 rows affected (0.06 sec)
Records: 0  Duplicates: 0  Warnings: 0
mysql> desc test3;
+-------+------------+------+-----+---------+-------+
| Field | Type       | Null | Key | Default | Extra |
+-------+------------+------+-----+---------+-------+
| id    | char(6)    | YES  | UNI | NULL    |       |
| name  | varchar(4) | NO   | PRI | NULL    |       |
| age   | int(3)     | NO   | MUL | NULL    |       |
+-------+------------+------+-----+---------+-------+
3 rows in set (0.00 sec)












Guess you like

Origin blog.51cto.com/11483827/2413721