1、建立删除主建索引索引
1.在建立表时就建立好索引rem
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=utf8io
对应该的删除主键要有两步来完成:1).Alter table student modify id int(4) not null;//删除自增加 2).alter table student drop primary key;table
2.建表时忘记建立主键索引时, 在以后手动建立nio
alter table student modify id int(4) primary key auto_increment;im
或者alter table student add primary key (id); alter table student change id id int(4) not null auto_increment;查询
2、建立删除惟一索引和普通索引tab
create [UNIQUE] index idx_name on student (name);di
alter table student add index idx_union (age,dept);index
----------------------------------------------------------------
alter table student drop index idx_name;
drop INDEX index_name ON tbl_name
查看索引 show index from student\G
基本建立索引的原则:
1.索引会加快查询速度,可是会影响更新的速度,由于更新后要维护索引。
2.索引不是越多越好,要是频繁查询的where条件列上建立索引。
3.小表或惟一值极少的列上不要建索引,要在大表以及不一样内容多的列上建立索引。