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.小表或唯一值极少的列上不要建索引,要在大表以及不同内容多的列上创建索引。


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM