創(chuàng)建索引??
普通索引 index|key? ? ?唯一索引? unique? ? ? 主鍵? ?primary key
1. alter table 庫.表? add { index| unique } 索引名(某一個(gè)列名) //? ps:有名字的 索引? ? 給某一列添加有名索引
例如:? alter table lx18.lyon add index idx_name (userName); // 向lx18 的 表lyon 中 的 userName 添加索引
例如:? alter table lx18.lyon add unique idx_name (userName); // 向lx18 的 表lyon 中 的 userName 添加w唯一索引??
2 alter table 庫.表? add index (某一個(gè)列名)? //? ps:給某一列添加索引? 字段名? 即是 索引名
例如: alter table lx18.lyon add index (userName);
3. 創(chuàng)建表的時(shí)候直接加索引
create table lyon01(?
id int not null auot_increment,
userName char(20) not null,
phone int not null,
primary key(id),
unique(phone),
index(userName)
)
如下圖:

alter table 庫.表 下的語句 刪除 索引?
1)選用DROP PRIMARY KEY子句用于刪除表中的主鍵,由于一個(gè)表中只有
一個(gè)主鍵,其也是一個(gè)索引;
2)選用DROP INDEX子句用于刪除各種類型的索引(普通索引 和? 唯一索引);
3)選用DROP FOREIGN KEY子句用于刪除外鍵。
例如:alter table lyon drop index userName;
例如:alter table lyon drop index age;? //?刪除 index 和 unique 都是這樣寫
4)drop index 語句刪除索引
drop index? 索引名 on 庫.表
例如: drop index idx_name on lx18.lyon;
創(chuàng)建組合索引
create index idx_name on? 庫.表(字段1,字段2) //? 根據(jù)字段1 字段2 創(chuàng)建組合? idx_name (ps:索引名)
例如: create index idx_name on lx18.lyon01(userName,phone);
在已有數(shù)據(jù)庫mysql_test上新建一個(gè)包含產(chǎn)品賣家id號(hào)、姓名、地址、
聯(lián)系方式、售賣產(chǎn)品類型、當(dāng)月銷量等內(nèi)容的產(chǎn)品賣家信息表seller,
要求在創(chuàng)建表的同時(shí),為該表添加由賣家id號(hào)和售賣產(chǎn)品類型組成的
聯(lián)合主鍵,并在當(dāng)月銷量上創(chuàng)建索引。\
mysql>USE mysql_test;
? ? Database changed
? ? mysql>CREATE TABLE seller
? ? ? ? ? ? ->(
? ? ? ? ? ? ->? seller_id int NOT NULL,
? ? ? ? ? ? ->? seller_name char(50)NOT NULL,
? ? ? ? ? ? ->? seller_address char(50)NULL,
? ? ? ? ? ? ->? seller_contact char(50)NULL,
? ? ? ? ? ? ->? product_type int(5)NULL,
? ? ? ? ? ? ->? sales int NULL,
? ? ? ? ? ? ->? PRIMARY KEY(seller_id,product_type),
? ? ? ? ? ? ->? INDEX index_seller(sales)
? ? ? ? ? ? ->);
? ? Query OK,0 rows affected(0.14 sec)
