1、实例问题
SELECT * FROM student WHERE id=100000;
2、出现问题
3、解决问题方案
4、理解
5、索引的作用
6、索引的缺点
1、普通索引
2、唯一性索引
3、全文索引
4、单列索引
5、多列索引
6、空间索引
CREATE TABLE 表名(字段名1 数据类型[完整性约束条件],
字段名2 数据类型[完整性约束条件],
...........
[UNIQUE|FULLTEXT|SPATIAL] INDEX|KEY
[别名](索引字段名 [(长度)])[ASC|DESC])
UNIQUE:该参数为可选项,标识唯一索引
FULLTEXT:该参数为可选项,表示全文索引
SPATIAL:可选参数该参数,表示空间索引
INDEX|KEY:该参数表示字段的索弓|,二者选一即可
别名:该参数为可选项,表示创建的索引的名称
索引字段名:指定索引对应的字段的名称
长度:该参数为可选项,表示索引的长度
ASC|DESC:该参数为可选项,ASC表示升序排列,DESC表示降序排列。
-- 创建表并建立普通索引
DROP TABLE IF EXISTS student;
CREATE TABLE student (
sid INT,
sname VARCHAR(50),
age INT,
gender VARCHAR(50) DEFAULT 'male',
INDEX(sid)
);
-- 查看表结构
SHOW CREATE TABLE student;
-- 插入数据
INSERT INTO student (sid,sname,age,gender) VALUES (1001, 'lili', 17, 'male');
INSERT INTO student (sid,sname,age,gender) VALUES (1002, 'wang', 18, 'female');
INSERT INTO student (sid,sname,age,gender) VALUES (1003, 'dodo', 19, 'male');
INSERT INTO student (sid,sname,age,gender) VALUES (1004, 'xixi', 20, 'female');
-- 查询数据
SELECT * FROM student WHERE sid=1004;
-- 创建表并建立唯一性索引
DROP TABLE IF EXISTS student;
CREATE TABLE student (
sid INT,
sname VARCHAR(50),
age INT,
gender VARCHAR(50) DEFAULT 'male',
UNIQUE INDEX uniqueIndex (sid ASC)
);
-- 查看表结构
SHOW CREATE TABLE student;
-- 插入数据
INSERT INTO student (sid,sname,age,gender) VALUES (1001, 'lili', 17, 'male');
INSERT INTO student (sid,sname,age,gender) VALUES (1002, 'wang', 18, 'female');
INSERT INTO student (sid,sname,age,gender) VALUES (1003, 'dodo', 19, 'male');
INSERT INTO student (sid,sname,age,gender) VALUES (1004, 'xixi', 20, 'female');
-- 查询数据
SELECT * FROM student WHERE sid=1004;
-- 创建表并建立全文索引
DROP TABLE IF EXISTS student;
CREATE TABLE student (
sid INT,
sname VARCHAR(50),
age INT,
gender VARCHAR(50) DEFAULT 'male',
FULLTEXT INDEX fullTextIndex (sname)
)ENGINE=MyISAM;
-- 查看表结构
SHOW CREATE TABLE student;
-- 插入数据
INSERT INTO student (sid,sname,age,gender) VALUES (1001, 'lili', 17, 'male');
INSERT INTO student (sid,sname,age,gender) VALUES (1002, 'wang', 18, 'female');
INSERT INTO student (sid,sname,age,gender) VALUES (1003, 'dodo', 19, 'male');
INSERT INTO student (sid,sname,age,gender) VALUES (1004, 'xixi', 20, 'female');
-- 查询数据
SELECT * FROM student WHERE sname='xixi';
-- 创建表并建立单列索引
DROP TABLE IF EXISTS student;
CREATE TABLE student (
sid INT,
sname VARCHAR(50),
age INT,
gender VARCHAR(50) DEFAULT 'male',
UNIQUE singleIndex (sname (20))
);
-- 查看表结构
SHOW CREATE TABLE student;
-- 插入数据
INSERT INTO student (sid,sname,age,gender) VALUES (1001, 'lili', 17, 'male');
INSERT INTO student (sid,sname,age,gender) VALUES (1002, 'wang', 18, 'female');
INSERT INTO student (sid,sname,age,gender) VALUES (1003, 'dodo', 19, 'male');
INSERT INTO student (sid,sname,age,gender) VALUES (1004, 'xixi', 20, 'female');
-- 查询数据
SELECT * FROM student WHERE sname='dodo';
-- 创建表并建立多列索引
DROP TABLE IF EXISTS student;
CREATE TABLE student (
sid INT,
sname VARCHAR(50),
age INT,
gender VARCHAR(50) DEFAULT 'male',
INDEX mulIndex (sid,sname (20))
);
-- 查看表结构
SHOW CREATE TABLE student;
-- 插入数据
INSERT INTO student (sid,sname,age,gender) VALUES (1001, 'lili', 17, 'male');
INSERT INTO student (sid,sname,age,gender) VALUES (1002, 'wang', 18, 'female');
INSERT INTO student (sid,sname,age,gender) VALUES (1003, 'dodo', 19, 'male');
INSERT INTO student (sid,sname,age,gender) VALUES (1004, 'xixi', 20, 'female');
-- 查询数据
SELECT * FROM student WHERE sid=1003;
CREATE [UNIQUE|FULLTEXT|SPATIAL] INDEX 索引名
ON 表名 (字段名 [(长度)] [ASC|DESC]);
-- 创建表
DROP TABLE IF EXISTS book;
CREATE TABLE book (
bookid INT NOT NULL,
bookname VARCHAR(255) NOT NULL,
authors VARCHAR(255) NOT NULL,
info VARCHAR(255) NULL,
comment VARCHAR(255) NULL,
publicyear YEAR NOT NULL
)ENGINE=MyISAM;
-- 创建普通索引
CREATE INDEX bookIndex ON book(bookid);
-- 创建唯一性索引
CREATE UNIQUE INDEX uniqueIndex ON book (bookid);
-- 创建单列索引
CREATE INDEX singleIndex ON book(comment);
-- 创建多列索引
CREATE INDEX mulIndex ON book (authors(20),info(20));
-- 创建全文索引
CREATE FULLTEXT INDEX fullTextIndex ON book(info);
-- 查询表结构
SHOW CREATE TABLE book;
ALTER TABLE 表名 ADD [UNIQUE|FULLTEXT|SPATIAL] INDEX
ON 表名 (字段名 [(长度)] [ASC|DESC]);
- 创建表
DROP TABLE IF EXISTS book;
CREATE TABLE book (
bookid INT NOT NULL,
bookname VARCHAR(255) NOT NULL,
authors VARCHAR(255) NOT NULL,
info VARCHAR(255) NULL,
comment VARCHAR(255) NULL,
publicyear YEAR NOT NULL
)ENGINE=MyISAM;
-- 创建普通索引
ALTER TABLE book ADD INDEX bookIndex(bookid);
-- 创建唯一性索引
ALTER TABLE book ADD UNIQUE uniqueIndex(bookid);
-- 创建单列索引
ALTER TABLE book ADD INDEX singleIndex(comment);
-- 创建多列索引
ALTER TABLE book ADD INDEX mulIndex(authors(20),info(20));
-- 创建全文索引
ALTER TABLE book ADD FULLTEXT INDEX fullTextIndex(info);
-- 查询表结构
SHOW CREATE TABLE book;
ALTER TABLE 表名 DROP INDEX 索引名;
-- 创建表
DROP TABLE IF EXISTS book;
CREATE TABLE book (
bookid INT NOT NULL,
bookname VARCHAR(255) NOT NULL,
authors VARCHAR(255) NOT NULL,
info VARCHAR(255) NULL,
comment VARCHAR(255) NULL,
publicyear YEAR NOT NULL
)ENGINE=MyISAM;
-- 创建普通索引
ALTER TABLE book ADD INDEX bookIndex(bookid);
-- 查询表结构
SHOW CREATE TABLE book;
-- 删除索引
ALTER TABLE book DROP INDEX bookIndex;
-- 查询表结构
SHOW CREATE TABLE book;
DROP INDEX 索引名 ON 表名;
-- 创建表
DROP TABLE IF EXISTS book;
CREATE TABLE book (
bookid INT NOT NULL,
bookname VARCHAR(255) NOT NULL,
authors VARCHAR(255) NOT NULL,
info VARCHAR(255) NULL,
comment VARCHAR(255) NULL,
publicyear YEAR NOT NULL
)ENGINE=MyISAM;
-- 创建普通索引
ALTER TABLE book ADD INDEX bookIndex(bookid);
-- 查询表结构
SHOW CREATE TABLE book;
-- 删除索引
DROP INDEX bookIndex ON book;
-- 查询表结构
SHOW CREATE TABLE book;