MySQL-索引03

来源:互联网 发布:java替换字符串 编辑:程序博客网 时间:2024/06/06 18:37

MySQL-索引03

  • 主键索引
  • 普通索引
  • 唯一索引
  • 联合(组合)索引
    • 联合主键索引
    • 联合普通索引
    • 联合唯一索引

-------------------

  • 主键索引
    create tabel tb1(        id int auto_increment priamry key,        name char(32) not null,        age int not null    )engine=innodb default utf8;    create tabel tb1(        id int auto_increment,        name char(32) not null,        age int not nullprimary key (id)    )engine=innodb default utf8;    添加主键:           alter table 表名 add primary key(列名)    删除主键:           alter table 表名 drop primary key;           alter table 表名 modify 列名 int, drop primary key;
  • 普通索引
    create table tb1(        id int auto_increment primary key,        name char(32) not null,        age int(10) not null,        index ix_name (name)    )engine=innodb default charset utf8;    create index ix_name on tb1(name);    drop index ix_name on tb1;    show index from tb1;    create index ix_name on tb1(name(2));    如果建立索引的对象是二进制blob()和text类型时,必须像上面这一在括号内指定长度。     
  • 唯一索引
    • 唯一约束(不能重复)和加速索引的功能
    • 唯一索引和主键索引的区别:unique 可以为空null,而primary key 不能为空
    create table tb1(        id int auto_increment primary key,        name char(32) not null,        age int(10) not null,        email char(32) not null,        unique ui_email (email)    )    create unique index ui_email on tb1(email);    drop unique index ui_email on tb1;
  • 组合索引
    create unique index  ui_index on tb1(name,email);    drop unique index ui_index on tb1;    如上创建组合索引之后,查询:    name and email  -- 使用索引    name            -- 使用索引    email           -- 不使用索引    注意:对于同时搜索n个条件时,组合索引的性能好于多个单一索引合并。
原创粉丝点击