Mysql数据表索引操作

Mysql数据表索引操作:

        索引作用:提高检索速度
          1、主键索引(包含了唯一索引)
          2、唯一索引 

          3、普通索引

       数据库中对索引和不加索引的对比:
          1、desc select * from user where id=4\G;
          2、desc select * from user where name="user4"\G;

     一、 普通索引
          1、添加索引:
            alter table user add index in_name(name);
          2、删除索引:
            alter table user drop index in_name(name);
          3、查询数据:
            desc select * from user where name="user4"\G;
    二、唯一索引:
          1、添加索引
            alter table user add unique uni_name(name);
          2、删除索引
            不能删除唯一索引,只能当做普通索引来删除
            alter table user drop index uni_name;
     三、主键索引:
          1、添加索引
            primary key(id);
          2、删除索引
            01)、首先删除自增
                alter table user modify id int unsigned not null;
            02)、删除主键

                alter table user drop primary key;

     四、修改数据表字段
          1、新增
            alter table user add sex tinyint not null after name;
            alter table user add sex tinyint not null first;
          2、删除
            alter table user drop sex;
          3、修改
            01)、修改字段属性
                 alter table user modify name varchar(50) not null;
            02)、修改字段名称
                 alter table user change name username varchar(50) not null;

猜你喜欢

转载自blog.csdn.net/shaoyanlun/article/details/80497870