MySQL-索引03

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个条件时,组合索引的性能好于多个单一索引合并。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值