mysql 索引操作

6.4 mysql表的索引操作
6.4.1 创建索引
    方式一(create):
    创建普通索引:create index indexName on tableName(列名)
    创建唯一索引:create unique index indexName on tableName(列名)
    方式二(alter):
    创建普通索引:alter table tableName add index indexName(列名)
    创建唯一索引:alter table tableName add unique(列名)
    创建主键索引: alter table tableName add primary key (列名)
# 实验一:使用(create)将student表的id设置为索引
    mysql> show index from student\G;
    Empty set (0.00 sec)

    mysql> create index stu_index_id on student(id);
    Query OK, 4 rows affected (0.01 sec)
    Records: 4  Duplicates: 0  Warnings: 0

    mysql> show index from student\G;
    *************************** 1. row ***************************
           Table: student
      Non_unique: 1
        Key_name: stu_index_id
    Seq_in_index: 1
     Column_name: id
       Collation: A
     Cardinality: NULL
        Sub_part: NULL
          Packed: NULL
            Null: YES
      Index_type: BTREE
         Comment: 
    1 row in set (0.00 sec)
# 实验二:使用(alter)将student3表的id设置为索引
    mysql> show index from student3\G;
    Empty set (0.00 sec)

    mysql> mysql> alter table student3 add index stu3_index_id (id);
    Query OK, 0 rows affected (0.03 sec)
    Records: 0  Duplicates: 0  Warnings: 0


    mysql> show index from student3\G;
    *************************** 1. row ***************************
           Table: student3
      Non_unique: 1
        Key_name: stu3_index_id
    Seq_in_index: 1
     Column_name: id
       Collation: A
     Cardinality: NULL
        Sub_part: NULL
          Packed: NULL
            Null: YES
      Index_type: BTREE
         Comment: 
    1 row in set (0.00 sec)

注意:创建唯一索引的时候,该列中不能出现重复值否则索引创建不成功

6.4.2 删除索引
    方式一(drop):
    删除普通索引和唯一索引:drop index indexName on tableName;
    方式二(alter):
    删除普通索引和唯一索引:alter table tableName drop index indexName;
    删除主键索引:alter table tableName drop primary key
# 实验一 使用方式一删除student索引:
    mysql> drop index stu_index_id on student;
    Query OK, 4 rows affected (0.00 sec)
    Records: 4  Duplicates: 0  Warnings: 0

    mysql> show index from student;
    Empty set (0.00 sec)
# 实验三 使用方式二删除student3索引:
    mysql> alter table student3 drop index stu3_index_id;
    Query OK, 0 rows affected (0.00 sec)
    Records: 0  Duplicates: 0  Warnings: 0

    mysql> show index from student3;
    Empty set (0.00 sec)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值