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.小表或惟一值極少的列上不要建索引,要在大表以及不一樣內容多的列上建立索引。