1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
| # 1、在创建表时指定索引
MariaDB [test]> CREATE TABLE `customerstest` (
`cust_id` int(11) NOT NULL AUTO_INCREMENT,
`cust_name` char(50) NOT NULL,
`cust_address` char(50) DEFAULT NULL,
`cust_city` char(50) DEFAULT NULL,
`cust_state` char(5) DEFAULT NULL,
`cust_zip` char(10) DEFAULT NULL,
`cust_country` char(50) DEFAULT NULL,
`cust_contact` char(50) DEFAULT NULL,
`cust_email` char(255) DEFAULT NULL,
PRIMARY KEY (`cust_id`),
INDEX customerIndex (cust_name)
) ENGINE=InnoDB AUTO_INCREMENT=10006 DEFAULT CHARSET=latin1;
MariaDB [test]> show index from customerstest;
+---------------+------------+---------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+---------------+------------+---------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| customerstest | 0 | PRIMARY | 1 | cust_id | A | 0 | NULL | NULL | | BTREE | | |
| customerstest | 1 | customerIndex | 1 | cust_name | A | 0 | NULL | NULL | | BTREE | | |
+---------------+------------+---------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
2 rows in set (0.00 sec)
# 2、使用 ALTER TABLE
mysql> alter table customerstest add index customerAlterIndex (cust_address);
Query OK, 0 rows affected (0.02 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> show index from customerstest;
+---------------+------------+--------------------+--------------+--------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+---------------+------------+--------------------+--------------+--------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| customerstest | 0 | PRIMARY | 1 | cust_id | A | 0 | NULL | NULL | | BTREE | | |
| customerstest | 1 | customerIndex | 1 | cust_name | A | 0 | NULL | NULL | | BTREE | | |
| customerstest | 1 | customerAlterIndex | 1 | cust_address | A | 0 | NULL | NULL | YES | BTREE | | |
+---------------+------------+--------------------+--------------+--------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
3 rows in set (0.01 sec)
# 3、使用 create index
mysql> create index customersCreatIndex on customerstest (cust_city);
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> show index from customerstest;
+---------------+------------+---------------------+--------------+--------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+---------------+------------+---------------------+--------------+--------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| customerstest | 0 | PRIMARY | 1 | cust_id | A | 0 | NULL | NULL | | BTREE | | |
| customerstest | 1 | customerIndex | 1 | cust_name | A | 0 | NULL | NULL | | BTREE | | |
| customerstest | 1 | customerAlterIndex | 1 | cust_address | A | 0 | NULL | NULL | YES | BTREE | | |
| customerstest | 1 | customersCreatIndex | 1 | cust_city | A | 0 | NULL | NULL | YES | BTREE | | |
+---------------+------------+---------------------+--------------+--------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
4 rows in set (0.00 sec)
# 4、唯一索引 unique
mysql> create unique index customersUniqueIndex on customerstest (cust_state);
create table test.customerstest
(
cust_id int auto_increment
primary key,
cust_name char(50) not null,
cust_address char(50) null,
cust_city char(50) null,
cust_state char(5) null,
cust_zip char(10) null,
cust_country char(50) null,
cust_contact char(50) null,
cust_email char(255) null,
constraint customersUniqueIndex unique (cust_state) # 在数据库表的DDL中,新增呢唯一索引标识
);
# 5、组合索引
mysql> create index customersTogtherIndex on customerstest (cust_state, cust_zip, cust_country);
Query OK, 0 rows affected (0.02 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> show index from customerstest;
+---------------+------------+-----------------------+--------------+--------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+---------------+------------+-----------------------+--------------+--------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| customerstest | 0 | PRIMARY | 1 | cust_id | A | 0 | NULL | NULL | | BTREE | | |
| customerstest | 0 | customersUniqueIndex | 1 | cust_state | A | 0 | NULL | NULL | YES | BTREE | | |
| customerstest | 1 | customerIndex | 1 | cust_name | A | 0 | NULL | NULL | | BTREE | | |
| customerstest | 1 | customerAlterIndex | 1 | cust_address | A | 0 | NULL | NULL | YES | BTREE | | |
| customerstest | 1 | customersCreatIndex | 1 | cust_city | A | 0 | NULL | NULL | YES | BTREE | | |
| customerstest | 1 | customersTogtherIndex | 1 | cust_state | A | 0 | NULL | NULL | YES | BTREE | | |
| customerstest | 1 | customersTogtherIndex | 2 | cust_zip | A | 0 | NULL | NULL | YES | BTREE | | |
| customerstest | 1 | customersTogtherIndex | 3 | cust_country | A | 0 | NULL | NULL | YES | BTREE | | |
+---------------+------------+-----------------------+--------------+--------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
8 rows in set (0.00 sec)
|