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

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值