索引index
创建表时,创建索引
| 创建普通索引 | create table 表名( ……. index(字段) #普通索引 ); |
|---|---|
| 创建唯一索引 | create table 表名( ……. unique index unique_字段(字段 asc) #唯一索引 ); |
| 创建全文索引 | create table 表名( ……. fulltext index full_字段(字段) #全文索引 )ENGINE=MyISAM; |
| 创建多列索引 | create table 表名( ……. index multi(字段1,字段2) #多列索引 ); |
| 创建空间索引 | create table 表名( ……. space GEOMETRY not null, spatial index s_space(space) #空间索引 )ENGINE=MyISAM; |
使用create index语句在已经存在的表中创建索引
| create 【unique/fulltext/spatial】index 索引名 on 表名(字段); |
|---|
使用alter table语句在已经存在的表中创建索引
| alter table 表名 add index 索引名 (字段); |
|---|
删除索引
| drop index 索引名 on 表名; |
|---|
添加主键和添加普通索引有什么区别?1.普通索引是最基本的索引类型,没有任何限制,值可以为空,仅加速查询。普通索引是可以重复的,一个表中可以有多个普通索引。
2.主键索引是一种特殊的唯一索引,一个表只能有一个主键,不允许有空值;索引列的所有值都只能出现一次,即必须唯一。简单来说:主键索引是加速查询+列值唯一(不可以有null)+表中只有一个
视图
创建视图
| create [or replace] view 视图名[别名1,别名2] as 查询语句 [with check option] #[]内,可有可无 ); |
|---|
查看视图
| #结构 | desc 视图名; |
|---|---|
| #信息 | show table status like ‘视图名’; |
| #创建语句 | show create view 视图名; |
修改视图
| 使用create or replace view |
|---|
| alter view 视图名(别名1,别名2..) as 查询语句; |
更新视图
| 使用update语句 | update 视图名 set 列名=值; |
|---|---|
| 通过insert或delete在基本表中 |
删除视图
| drop view [if exists]视图名; |
|---|
with check option 的作用添加或者修改视图数据时,配合where语句使用,必须满足where条件,否则加不进去视图中,但表中会添加上此数据
