关于Mysql唯一索引的操作方法(添加删除)

    首先我们查看一下News数据表的索引信息

       使用命令 show index from ‘数据表名称’;

    目前数据表中仅有一个主键索引

   

 

 

    继续,我们给news表添加两个唯一索引(两种方法)

    方法一: alter table '数据表名' add  constraint '索引名'  unique(‘要添加的字段名’);

         alter  table news add constraint title_sy unique(title);

    

 

    方法二:create unique index 索引名 on 数据表名(字段名);

        create unique index http_sy on news(http);

    

 

    添加完索引之后,我们查看一下刚才添加的两个索引是否存在--->

    

 

 

    下面我们给News表删除索引  (两种方法)

    方法一:  alter table 数据表名 drop index 删除的索引名;

        alter table news drop index title_sy;

    

 

    方法二:   drop index 索引名 on 数据表名;

         drop index http_sy on news;

         

 

    最后,检查一下是否删除了

    show index from news;

    

 

posted @ 2018-11-23 15:57  ShareJia  阅读(9247)  评论(0)    收藏  举报