数据库索引就象书的目录一样,如果在字段上建立了索引,那么以索引列为查询条件时可以加快查询数据的速度。查询数据库,按主键查询是最快的,每个表只能有一个主键列,但是可以有多个普通索引列,主键列要求列的所有内容必须唯一,而普通索引列不要求内容必须唯一。主键就类似我们在学校学习时的学好一样,班级里是唯一的,整个表的每一条记录的主键值在表内都是唯一的,用来唯一标识一条记录
创建表的时候同时为表创建主键索引和普通索引:例子如下
mysql> 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 index_name(name) );
Query OK, 0 rows affected (0.00 sec)
注意:
primary key(id) <-主键
KEY index_name(name) <-name字段普通索引
只有int类型且为primary key才可以使用auto_increment
查看student表的表结构:
mysql> show create table studentG;
*************************** 1. row ***************************
Table: student
Create Table: 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 `index_name` (`name`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8
1 row in set (0.00 sec)
ERROR:
No query specified
删除表的主键:
alter table student1 drop primary key;
注意:如果一个表中的primary key设置了AUTO_INCREMENT(自动增加)的话,就删不掉。
修改student2表的id列为自增主键列:例子
修改前:
mysql> desc student2;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id | int(4) | NO | | NULL | |
| name | char(20) | NO | MUL | NULL | |
| age | tinyint(2) | NO | | 0 | |
| dept | varchar(16) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
4 rows in set (0.00 sec)
修改后:
mysql> alter table student2 change id id int primary key auto_increment;
Query OK, 0 rows affected (0.08 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc student2;
+-------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | char(20) | NO | MUL | NULL | |
| age | tinyint(2) | NO | | 0 | |
| dept | varchar(16) | YES | | NULL | |
+-------+-------------+------+-----+---------+----------------+
4 rows in set (0.00 sec)
为student2表的删除name列普通索引:例子
删除前:
mysql> desc student2;
+-------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | char(20) | NO | MUL | NULL | |
| age | tinyint(2) | NO | | 0 | |
| dept | varchar(16) | YES | | NULL | |
+-------+-------------+------+-----+---------+----------------+
4 rows in set (0.00 sec)
删除后:
mysql> alter table student2 drop index index_name;
Query OK, 0 rows affected (2.08 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc student2;
+-------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | char(20) | NO | | NULL | |
| age | tinyint(2) | NO | | 0 | |
| dept | varchar(16) | YES | | NULL | |
+-------+-------------+------+-----+---------+----------------+
4 rows in set (0.00 sec)
为student2表的name列增加普通索引,索引的名字叫作index_name:例子
mysql> alter table student2 add index index_name(name);
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> show create table student2G;
*************************** 1. row ***************************
Table: student2
Create Table: CREATE TABLE `student2` (
`id` int(11) 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 `index_name` (`name`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8
1 row in set (0.00 sec)
ERROR:
No query specified
注意: KEY `index_name` (`name`) 提示:访问数据量很大的时候,不适合建立普通索引,会影响用户访问,尽量选择业务低估时建立索引。