您的位置:首页 > 数据库 > MySQL

mysql innodb 引擎 key - 和 Primary key的区别 以及 索引的用法总结

2016-04-16 11:47 806 查看
mysql中关键字 key的几个用法:

①、用在列定义中,表示主键,这里 key = primary key

②、用在create table 的所有列定义后,这里key=index,表示索引

③、用在create key中,key = index

④、用在alter table中,用法和create table中相同

mysql中,广义的索引包括 ①、primary key ; ②、unique ; ③、create index <这种事普通意义上的索引>

只研究针对innodb 存储引擎的索引:

1、innodb存储引擎的表默认的索引都是btree索引,支持的是前缀索引,即对索引字段的前 N 个字符创建索引,索引的前缀长度最长是 767个字节。 - innodb不支持全文索引。 - - 对字符串列进行索引的时候,应该指定一个前缀长度。

2、使用create index 创建索引的语法:

CREATE [UNIQUE|FULLTEXT|SPATIAL] INDEX index_name

[USING index_type]

ON tbl_name (index_col_name,...)

[WITH PARSER parser_name]

index_col_name:

col_name [(length)] [ASC | DESC]

3、mysql里创建索引的方式汇总:

①、create table中 所有列定义后 INDEX [index_name] [index_type] (index_col_name,...)

②、create table中 所有列定义后 key [index_name] [index_type] (index_col_name,...)

③、alter table c add key(name);

④、alter table c add index(address);

⑤、create index idx_c_info on c(info);

示例代码:

drop table if exists c;

create table if not exists c -- Multiple primary key defined

(

id int key,

age int unique,

name varchar(50),

address varchar(100),

info varchar(1000),

test1 varchar(50),

test2 varchar(50),

key(test1(10)),

index(test2)

);

alter table c add key(name);

alter table c add index(address);

create index idx_c_info on c(info);

show create table c;

CREATE TABLE `c` (

`id` int(11) NOT NULL,

`age` int(11) DEFAULT NULL,

`name` varchar(50) DEFAULT NULL,

`address` varchar(100) DEFAULT NULL,

`info` varchar(1000) DEFAULT NULL,

`test1` varchar(50) DEFAULT NULL,

`test2` varchar(50) DEFAULT NULL,

PRIMARY KEY (`id`),

UNIQUE KEY `age` (`age`),

KEY `test1` (`test1`(10)),

KEY `test2` (`test2`),

KEY `name` (`name`),

KEY `address` (`address`),

KEY `idx_c_info` (`info`)

) ENGINE=InnoDB DEFAULT CHARSET=utf8
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: