一. 索 引 的 概 念
mysql> create database xuxu;
mysql> create table member (id int(10),name varchar(10),cardid varchar(18),phone varchar(11),address varchar(50),remark text);
Query OK, 0 rows affected (0.00 sec)
mysql> desc member;
+---------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| id | int(10) | YES | | NULL | |
| name | varchar(10) | YES | | NULL | |
| cardid | varchar(18) | YES | | NULL | |
| phone | varchar(11) | YES | | NULL | |
| address | varchar(50) | YES | | NULL | |
| remark | text | YES | | NULL | |
+---------+-------------+------+-----+---------+-------+
6 rows in set (0.00 sec)
create index 索引名 on 表名 (列名[(length)]);
例:create index name_index on member (name);
alter table 表名 add index 索引名(列名);
create table 表名 ( 字段1 数据类型,字段2 数据类型[,...],index 索引名 (列名));
例:create table test(id int(4) not null,name varchar(10) not null,cardid varchar(18) not null,index id_index (id));
show create table test; #查看看是竖列
desc test #查看的横向
create unique index 索引名 on 表名(列名);
例:select * from member;
create unique index address_index on member (address);
create unique index name_index on member (name);
show create table member;
alter table 表名 add unique 索引名 (列名);
例:alter table member add unique cardid_index (cardid);
CREATE TABLE 表名 (字段1 数据类型,字段2 数据类型 [,...],UNIQUE 索引名 (列名));
例:mysql> create table test2(id int,name varchar(20),unique id_index(id));
Query OK, 0 rows affected (0.00 sec)
mysql> show create table test2;
+-------+--------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+-------+--------------------------------------------------------------------------------------------------------------------------------------------------------------+
| test2 | CREATE TABLE `test2` (
`id` int(11) DEFAULT NULL,
`name` varchar(20) DEFAULT NULL,
UNIQUE KEY `id_index` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
+-------+--------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
CREATE TABLE 表名 ([...],PRIMARY KEY (列名));
例:mysql> create table test1(id int primary key,name varchar(20));
Query OK, 0 rows affected (0.00 sec)
mysql> show create table test1;
| test1 | CREATE TABLE `test1` (
`id` int(11) NOT NULL,
`name` varchar(20) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
+-------+------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
或者create table test2 (id int,name varchar(20),primary key (id));
修改表方式创建
ALTER TABLE 表名 ADD PRIMARY KEY (列名);
举例:
mysql> alter table test2 add primary key(id);
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> show create table test2;
+-------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+-------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| test2 | CREATE TABLE `test2` (
`id` int(11) NOT NULL,
`name` varchar(20) DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `id_index` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
+-------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
CREATE TABLE 表名 (列名1 数据类型,列名2 数据类型,列名3 数据类型,INDEX 索引名
(列名1,列名2,列名3));
select * from 表名 where 列名1='...' AND 列名2='...' AND 列名3='...';
例:create table amd1 (id int not null,name varchar(20),cardid varchar(20),index index_amd (id,name));
show create table amd1;
insert into amd1 values(1,'zhangsan','123123');
select * from amd1 where name='zhangsan' and id=1;
CREATE FULLTEXT INDEX 索引名 ON 表名 (列名);
例:select * from member;
create fulltext index remark_index on member (remark);
ALTER TABLE 表名 ADD FULLTEXT 索引名 (列名);
CREATE TABLE 表名 (字段1 数据类型[,...],FULLTEXT 索引名 (列名));
SELECT * FROM 表名 WHERE MATCH(列名) AGAINST('查询内容');
例:update info set remark='this is vip'
select * from member where match(remark) against('this is vip');
or
select * from member where remark='this is vip';
show index from 表名;
show index from 表名\G; 竖向显示表索引信息
show keys from 表名;
show keys from 表名\G;
字段名 | 含义 |
---|---|
Table | 表的名称 |
Non_unique | 如果索引内容唯一,则为 0;如果可以不唯一,则为 1 |
Key_name | 索引的名称 |
Seq_in_index | 索引中的列序号,从 1 开始, limit 2,3 |
Column_name | 列名称 |
Collation | 列以什么方式存储在索引中。在 MySQL 中,有值‘A’(升序)或 NULL(无分类) |
Cardinality | 索引中唯一值数目的估计值 |
Sub_part | 如果列只是被部分地编入索引,则为被编入索引的字符的数目(zhangsan)。如果整列被编入索引,则为 NULL |
Packed | 指示关键字如何被压缩。如果没有被压缩,则为 NULL |
Null | 如果列含有 NULL,则含有 YES。如果没有,则该列含有 NO |
Index_type | 用过的索引方法(BTREE, FULLTEXT, HASH, RTREE) |
Comment | 备注 |
DROP INDEX 索引名 ON 表名;
例:drop index name_index on member;
$\textcolor{blue}{1 . 普 通 索 引 } $ALTER TABLE 表名 DROP INDEX 索引名;
例:alter table member drop index id_index;
show index from member;
ALTER TABLE 表名 DROP PRIMARY KEY;