·021创建索引 MySQL (1) CREATE|NDEX语句创建索引 使用 CREATE INDEX语句可以在一个已有表上创建索引, 个表中可以创建多个索引。 命令 CREATE [UNIQUE FULLTEXT SPATIAL I NDEX索引名[索引类型]ON表名(索引列名…… 其中 索引列名:列名[长厦IASC/DESC MySQL数据库应用》http:/mysq!xg.hactcm.edu.cn 第11页
《MySQL数据库应用》 http://mysql.xg.hactcm.edu.cn/ 第 11 页 2.1 创建索引 (1)CREATE INDEX 语句创建索引 使用CREATE INDEX语句可以在一个已有表上创建索引,一 个表中可以创建多个索引。 • 命令: CREATE [UNIQUE | FULLTEXT | SPATIAL ] INDEX 索引名 [索引类型] ON 表名 (索引列名,……) – 其中: 索引列名 :列名 [(长度)] [ASC | DESC]
·021创建索引 MySQL 案例:在Xs表的学号列前5个字符建立升序索引inxs; create index in xs onXs(学号(5)asc) 也可以在一个索引中包含多列 案例:在ⅹskc表的学号列和课程列创建复合索引inⅹskc create index in xskc onⅹs_kc(学号,课程号); MySQL数据库应用》http:/mysq!xg.hactcm.edu.cn 第12页
《MySQL数据库应用》 http://mysql.xg.hactcm.edu.cn/ 第 12 页 2.1 创建索引 • 案例:在xs表的学号列前5个字符建立升序索引 in_xs; create index in_xs on xs(学号(5) asc) ; • 也可以在一个索引中包含多列; • 案例:在xs_kc表的学号列和课程列创建复合索引in_xskc; create index in_xskc on xs_kc(学号,课程号);
·021创建索引 MySQL (2)在建立数据表时创建索引 MySQL中,也可以在创建数据表时创建索引。 命令: CREATE TABLE[ F NOT EXISTS]表名 ([列定义], [ UNIQUE| FULLTEXT| SPATIAL][NDEX|KEY]][索引名 (索引列名[长度) ASC / DESC7 MySQL数据库应用》http:/mysq!xg.hactcm.edu.cn 第13页
《MySQL数据库应用》 http://mysql.xg.hactcm.edu.cn/ 第 13 页 2.1 创建索引 • (2)在建立数据表时创建索引 MySQL中,也可以在创建数据表时创建索引。 • 命令: • CREATE TABLE [IF NOT EXISTS] 表名 ([列定义] ,... [UNIQUE | FULLTEXT | SPATIAL] [INDEX |KEY] ] [索引名] (索引列名[长度])[ASC |DESC] … ) ;
·021创建索引 MySQL 案例:在创建表xskc的同时创建学号课程号的复合主 键,且在成绩上创建索引 create table xs kc 学号char(6) 课程号char(3) 成绩 tiny int, 学分 tiny int, pr imary key(学号,课程号), index in cj(成绩) MySQL数据库应用》http:/mysq!xg.hactcm.edu.cn 第14页
《MySQL数据库应用》 http://mysql.xg.hactcm.edu.cn/ 第 14 页 2.1 创建索引 • 案例:在创建表xs_kc的同时创建学号课程号的复合主 键,且在成绩上创建索引; create table xs_kc ( 学号 char(6) , 课程号 char(3) , 成绩 tinyint, 学分 tinyint, primary key(学号,课程号), index in_cj(成绩) );
·021创建索引 MySQL 案例:在创建表ⅹs的同时,在姓名列上创建唯一性索引 create table xs (学号char(6) not nul i pr i mary key, 姓名char(8) not nul I 专业名cha char(10) nul/ 性别 tiny int(1) not nu default1, 出生日期 date not nul I, 总学分 tiny int(1)null 照片b| ob nu l|, 备注 text nu l|, unique ined xm(姓名) MySQL数据库应用》http:/mysq!xg.hactcm.edu.cn 第15页
《MySQL数据库应用》 http://mysql.xg.hactcm.edu.cn/ 第 15 页 2.1 创建索引 • 案例:在创建表xs的同时,在姓名列上创建唯一性索引 create table xs (学号 char(6) not null primary key, 姓名 char(8) not null, 专业名 char(10) null, 性别 tinyint(1) not null default 1, 出生日期 date not null, 总学分 tinyint(1) null, 照片 blob null, 备注 text null, unique inedx xm(姓名) );