MySQL常用修改表结构及索引的SQL

1、索引操作

show indexs from '表名';  //查看表的索引
show keys from '表名';    //查看表的索引

show status like '%Handler_read%';   //查看索引的使用情况
handler_read_key:这个值越高越好,越高表示使用索引查询到的次数
handler_read_rnd_next:这个值越高,说明查询低效

alter table 表名 add primary key pk_nm(列名);  //增加主键索引
alter table 表名 drop primary key(列名);       //删除主键索引

alter table 表名 add unique(列名);               //添加唯一索引
alter table 表名 add index 索引名(列1, 列2...);  //添加普通索引

create index 索引的名字 on 表名(列名);         //添加普通索引
create unique index 索引的名字 on 表名(列名);  //创建唯一索引
CREATE [UNIQUE] [CLUSTERED| NONCLUSTERED] INDEX <索引名>
ON <表名>(<列名1>[ASC|DESC] [, <列名2>[ASC|DESC]...])

2、自增

alter table 表名 modify 列名 int auto_increment;

3、列的操作

alter table 表名 add 列名 varchar(255) not null;    //增加字段
alter table 表名 drop 列名;      //删除字段
alter table 表名 change old  new varchar(255) not null;  //修改

4、默认值操作

ALTER TABLE table_name ALTER COLUMN column_name SET DEFAULT default_value;

  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值