028-86922220

建站动态

根据您的个性需求进行定制 先人一步 抢占小程序红利时代

Mysql索引概述(用于个人学习与回顾)

MySQL索引概述

专注于为中小企业提供网站设计、成都做网站服务,电脑端+手机端+微信端的三站合一,更高效的管理,为中小企业玛多免费做网站提供优质的服务。我们立足成都,凝聚了一批互联网行业人才,有力地推动了上1000家企业的稳健成长,帮助中小企业通过网站建设实现规模扩充和转变。

索引介绍

索引优缺点



MySQL键值类型

键值类型

INDEX普通索引

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)
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)
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)
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主键

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
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)
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 外键

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)
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索引

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)
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)
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)











分享名称:Mysql索引概述(用于个人学习与回顾)
标题网址:http://www.tsicrk.com/article/jjjpop.html

其他资讯

让你的专属顾问为你服务

2.3211s