MySQL索引的分类

1、单值索引

1、即一个索引只包含单个列,一个表可以有多个单列索引

2、索引使用语法

(1)随表一起建索引:

CREATE TABLE customer (id INT(10) UNSIGNED  AUTO_INCREMENT ,customer_no VARCHAR(200),customer_name VARCHAR(200),
  PRIMARY KEY(id),
  KEY (customer_name)
);

(2)单独建单值索引:

CREATE  INDEX idx_customer_name ON customer(customer_name); 

(3)删除索引:

DROP INDEX idx_customer_name  on customer;

2、唯一索引

1、索引列的值必须唯一,但允许有空值

2、索引使用语法
(1)随表一起建索引:

CREATE TABLE customer (id INT(10) UNSIGNED  AUTO_INCREMENT ,customer_no VARCHAR(200),customer_name VARCHAR(200),
  PRIMARY KEY(id),
  KEY (customer_name),
  UNIQUE (customer_no)
);

(2)单独建唯一索引:

CREATE UNIQUE INDEX idx_customer_no ON customer(customer_no); 

(3)删除索引:

DROP INDEX idx_customer_no on customer ;

3、主键索引

1、设定为主键后数据库会自动建立索引,innodb为聚簇索引
2、索引使用语法
(1)随表一起建索引:

CREATE TABLE customer (id INT(10) UNSIGNED  AUTO_INCREMENT ,customer_no VARCHAR(200),customer_name VARCHAR(200),
  PRIMARY KEY(id) 
);
CREATE TABLE customer2 (id INT(10) UNSIGNED   ,customer_no VARCHAR(200),customer_name VARCHAR(200),
  PRIMARY KEY(id) 
);

(2)单独建主键索引:

ALTER TABLE customer 
add PRIMARY KEY customer(customer_no); 

(3)删除建主键索引:

ALTER TABLE customer 
 drop PRIMARY KEY ;  

(4)修改建主键索引:
必须先删除掉(drop)原索引,再新建(add)索引

4、复合索引

1、即一个索引包含多个列

2、索引使用语法
(1)随表一起建索引:

CREATE TABLE customer (id INT(10) UNSIGNED  AUTO_INCREMENT ,customer_no VARCHAR(200),customer_name VARCHAR(200),
  PRIMARY KEY(id),
  KEY (customer_name),
  UNIQUE (customer_name),
  KEY (customer_no,customer_name)
);

(2)单独建索引:

CREATE  INDEX idx_no_name ON customer(customer_no,customer_name);

(3)删除索引:

DROP INDEX idx_no_name  on customer ;

你可能感兴趣的:(Java,mysql,数据库,开发语言,后端,中间件)