一、视图 View

视图是一个虚拟表,是sql语句的查询结果,其内容由查询定义。同真实的表一样,视图包含一系列带有名称的列和行数据,在使用视图时动态生成。视图的数据变化会影响到基表,基表的数据变化也会影响到视图[insert update delete ] ; 创建视图需要create view 权限,并且对于查询涉及的列有select权限;使用create or replace 或者 alter修改视图,那么还需要改视图的drop权限。

视图的核心功能是用来简化查询,查询可以当作一个表来查询。

select * from stu;
-- 建立视图
create view v_li as
select id,name,score from stu where name like '李%' --
show tables;
select * from v_li limit 5; -- 建立账号,并授权
create user vu identified by 'vu';
grant select(id,name) on wxdb.v_li to vu;
grant select on wxdb.v_li to vu; create view v_member as
select m.nickname,m.sex,m.mobile from hbcf.cf_member m; grant select on v_member to vu; -- 删除视图
drop view v_member; -- 查看系统所有账号信息
select user,host,u.authentication_string,u.plugin from mysql.user u;
-- 查询视图
select * from stu01; --
create view vstu
as
select level 等级,count(*) 人数 from (select id,name,score,
case
when score>=90 then '优秀'
when score>=80 then '良好'
when score>=60 then '及格'
else '补考' end level
from stu) as t group by t.level; select * from stu; select * from vstu; -- 查看数据库 d3 所有 view 视图
select `TABLE_NAME`
from `INFORMATION_SCHEMA`.`TABLES`
where `TABLE_SCHEMA` = 'd3' and `TABLE_TYPE` = 'VIEW'; -- 查看数据库 d3的所有 base table 表
select `TABLE_NAME`
from `INFORMATION_SCHEMA`.`TABLES`
where `TABLE_SCHEMA` = 'd3' and `TABLE_TYPE` = 'BASE TABLE'; -- 删除视图
drop view if exists stu01,vstu;

二、索引

MySQL索引的建立对于MySQL的高效运行是很重要的,索引可以大大提高MySQL的检索速度。

打个比方,如果合理的设计且使用索引的MySQL是一辆兰博基尼的话,那么没有设计和使用索引的MySQL就是一个人力三轮车。

拿汉语字典的目录页(索引)打比方,我们可以按拼音、笔画、偏旁部首等排序的目录(索引)快速查找到需要的字。

索引分单列索引和组合索引。单列索引,即一个索引只包含单个列,一个表可以有多个单列索引,但这不是组合索引。组合索引,即一个索引包含多个列。

创建索引时,你需要确保该索引是应用在 SQL 查询语句的条件(一般作为 WHERE 子句的条件)。

实际上,索引也是一张表,该表保存了主键与索引字段,并指向实体表的记录。

上面都在说使用索引的好处,但过多的使用索引将会造成滥用。因此索引也会有它的缺点:虽然索引大大提高了查询速度,同时却会降低更新表的速度,如对表进行INSERT、UPDATE和DELETE。因为更新表时,MySQL不仅要保存数据,还要保存一下索引文件。

建立索引会占用磁盘空间的索引文件。

-- 建立表时指定索引,
create table t(t varchar(30),key(t desc))
show index from t;
-- 删除索引
drop index t on t; alter table t add index index1 (t desc);
alter table t drop index index1; -- 使用索引
select * from stu where name = '李丽'; -- 建立唯一索引
create unique index name_index on stu(name asc); create index namegi on stu(name asc,gender desc); -- 删除索引
drop index name_index on stu;
drop index namegi on stu; -- 查看表中所有索引
show index from stu; -- 索引缺点 1 50mb 100mb 2 降低数据的插入,修改速度 -- index 索引
-- 建立索引
create table t1(
id int unsigned auto_increment,
name varchar(30),
score tinyint unsigned,
primary key(id),
key(name desc)
); create table t2(
id int unsigned,
name varchar(30),
score tinyint unsigned,
index iname (name(30))
); create table t3(
id int unsigned,
name varchar(30),
score tinyint unsigned
);
-- 建立索引
create index inn on t3(name)
-- 建立唯一索引
create unique index inn2 on t3(name); alter table t3 add index inn4(name asc,score desc); -- 使用索引 索引字段是不是出现在where条件中,如果在索引就起作用
select * from stu where name = '张三'; select * from t3;
insert into t3 value(1,'李四1',80);
insert into t3 value(2,'李四2',10);
insert into t3 value(3,'李四3',50);
insert into t3 value(4,'李四4',40);
insert into t3 value(5,'李四5',30);
insert into t3 value(6,'李四6',20); explain select * from t3 where id = 6; explain select * from t3 where name = '李四3'; -- 删除索引
drop index inn2 on t3; alter table t3 drop index inn4; -- 查看表中所有索引
show index from t3; -- 索引缺点 1 50mb 100mb 2 降低数据的插入,修改速度

最新文章

  1. python用二进制读取文件
  2. HDU4609 & FFT
  3. JetS3t使用说明
  4. paper 92:Lena与图像处理
  5. [lintcode the-smallest-difference]最小差(python)
  6. 采用rest接口对接而非webservice
  7. DataURL与File,Blob,canvas对象之间的互相转换的Javascript
  8. 最简单的 springboot 发送邮件,使用thymeleaf模板
  9. Hadoop HDFS常用命令
  10. centos7离线安装rpm包自动解决依赖
  11. Spring boot http, https
  12. (转)IBM AppScan 安全漏洞问题修复(.net)
  13. python标准库介绍——34 commands 模块详解
  14. pycharm 里面引用pymysql
  15. mysql-innodb的事务日志
  16. tnsping 命令解析
  17. new[] 到底做了什么?
  18. Android WebView学习
  19. pip3 的安装 同时安装lxml和pygame
  20. CrtmpServr 接收Http流程

热门文章

  1. HDU 1863 畅通工程 (并查集)
  2. Solon 开发,七、自定义注解开发汇总
  3. Three.js之绘制物体的边框及修改lineWidth
  4. Javascript面向对象——创建对象、构造函数的原型
  5. ROS Kinetic 使用PocketSphinx进行语音识别报错
  6. Cesium应用篇--添加雨雪天气
  7. C++11多线程之future(一)
  8. golang中浮点型底层存储原理和decimal使用方法
  9. 返回值String表示视图
  10. Nginx搭建游戏