mysql sql删除主键索引_sql操作之添加删除索引

一、创建删除主建索引

1.在创建表时就创建好索引

CREATE TABLE `student` (

`id` int(4) NOT NULL AUTO_INCREMENT,

`name` char(20) NOT NULL,

`age` tinyint(2) NOT NULL DEFAULT '0',

`dept` varchar(16) DEFAULT NULL,

PRIMARY KEY (`id`),

KEY `idx_name` (`name`)

) ENGINE=MyISAM DEFAULT CHARSET=utf8

对应该的删除主键要有两步来完成:1).Alter table student modify id int(4) not null;//删除自增长 2).alter table student drop primary key;

2.建表时忘记创建主键索引时, 在之后手动创建

alter table student modify id int(4) primary key auto_increment;

或者alter table student add primary key (id); alter table student change id id int(4) not null auto_increment;

二、创建删除唯一索引和普通索引

create [UNIQUE] index idx_name on student (name);

alter table student add index idx_union (age,dept);

----------------------------------------------------------------

alter table student drop index idx_name;

drop INDEX index_name ON tbl_name

查看索引 show index from student\G

基本创建索引的原则:

1.索引会加快查询速度,但是会影响更新的速度,因为更新后要维护索引。

2.索引不是越多越好,要是频繁查询的where条件列上创建索引。

3.小表或唯一值极少的列上不要建索引,要在大表以及不同内容多的列上创建索引。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值