一、創建刪除主建索引
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.小表或唯一值極少的列上不要建索引,要在大表以及不同內容多的列上創建索引。