创建索引 | 一、创建表的同时创建索引 create table name [colName dataType] [unique|fulltext|spatial] [index|key] [indexName](colName[length]) [asc|desc] create table teacher (id int(11),name varchar(25),salary int(11),index(id));普通索引 create table t1(id int(11),name varchar(25),unique index myindex(id));唯一索引 create table t2(id int(11) not null,name varchar(30) not null, index singleIndex(name(20)));单列索引,只能在string类型上创建,并且索引长度不得大于字段长度; create table t3(id int(11) not null,name varchar(30)not null,index(id,name(30))); 多列索引;索引长度不能超过string数据长度; 全文索引,只支持MyISAM引擎,创建表时,必须显示指明引擎类型,否则报错; create table t4(id int not null,name char(30) not null,fulltext index(name));× ERROR 1214 (HY000): The used table type doesn't support FULLTEXT indexes create table t4(id int not null,name char(30) not null,fulltext index(name))engine=myisam;√ create table t5(g geometry not null ,spatial index myindex (g))engine=myisam ; |
二、在已经存在的表上创建索引 ■■使用alter table创建 alter table name add [unique|fulltext|spatial] [index|key] [indexName](colName[length],...)[asc|desc] alter table student add index myIndex(name(10)); 索引的查看方式: l show index from tableName;查看表中索引的详细信息;(这里不能用show key) l show create table tableName;查看创建表信息,包含创建索引信息; l explain select *from student where name='cat';可以查看该筛选所用的索引; alter table student add unique key(age); 创建唯一索引,要保证该列的数据唯一性,否则报错; ERROR 1062 (23000): Duplicate entry '22' for key 'age' 关键字key和index是等价的; alter table student add key multiKey(name,age); alter table t1 engine=myisam;先修改引擎为MyISAM,然后再添加fulltext索引; alter table t1 add fulltext key(name); ■■用create index创建 create [unique|fulltext|spatial] index indexName on tableName(colName[lenght],...) [asc|desc] create index idIndex on t2(id);普通索引 create unique index nameIndex on t2(name);唯一索引 create index multiIndex on t2(id,name);多列索引 alter table t3 engine=myisam;修改引擎后,再创建全文索引; create index fullIndex on t3(name); | |
删除索引 | ■用alter table删除索引 alter table tableName drop index indexName;编辑表 删除索引; show create table tableName;首先查看包含的索引名称,然后再针对性删除; alter table t3 drop index fullIndex; ■用drop index删除索引 drop index indexName on tableName;删除某个表上的某个索引; drop index name on t3; 在删除表中的列时,该列也会从索引中删除; 如果索引中的列全部被删除,那么该索引也会被自动删除; |
查询 | 查询是数据库最重要的功能;可以查询数据、筛选数据、指定显示格式; 几何函数查询、连接查询、子查询、合并查询、取别名、正则表达式; [table] |
select {*|columnList} from tableList [where condition] [group by field1] [order by field2] [limit [offset,] rowCount] | 选择查询字段(1-n个) 指定查询数据源(1-n个) 筛选条件 按照某个字段分组 按照某个字段排序 显示的行数 |
where | having |
分组之前进行过滤 | 分组之后进行过滤 |
欢迎光临 ToB企服应用市场:ToB评测及商务社交产业平台 (https://dis.qidao123.com/) | Powered by Discuz! X3.4 |