当前位置:网站首页>【MySQL和数据库】MySQL & database 第七章:视图的学习
【MySQL和数据库】MySQL & database 第七章:视图的学习
2022-07-21 14:58:00 【NoBlackstone】
MySQL和数据库系列文章目录
【MySQL和数据库】MySQL & database 第一章:数据库的安装与卸载
【MySQL和数据库】MySQL & database 第二章:数据库与MySQL的基本概述
【MySQL和数据库】MySQL & database 第三章:DQL学习(主要是记录的查询)
【MySQL和数据库】MySQL & database 第四章:DML学习(主要是对记录的增删改)
【MySQL和数据库】MySQL & database 第五章:DDL学习(主要是对库、表、字段的增删改)
【MySQL和数据库】MySQL & database 第六章:TCL学习(主要是针对事务的控制)
【MySQL和数据库】MySQL & database 第七章:视图的学习
【MySQL和数据库】MySQL & database 第八章:MySQL中的变量
【MySQL和数据库】MySQL & database 第九章:存储过程的学习
【MySQL和数据库】MySQL & database 第十章:函数的学习
【MySQL和数据库】MySQL & database 第十一章:流程控制结构
视图的概念
含义:
MySQL 从 5.0.1版本开始提供视图功能;
一种虚拟存在的表,行和列的数据来自定义视图的查询中使用的表,并且是在使用视图时动态生成的,只保存sql逻辑,不保存查询结果;应用场景:
- 多个地方用到同样的查询结果;
- 该查询结果使用的sql语句比较复杂;
特点:
- 重用sql语句;
- 简化复杂的sql操作,不必知道它的查询细节;
- 保护数据,提高安全性;
视图与表的区别
创建语法的关键词 是否实际占用物理空间 使用
视图 create view 只是保存了sql逻辑 增删改查,一般只用查
表 create table 保存了数据 增删改查
视图的创建
语法:
#视图的创建 create view 视图名 as 多条sql语句; #视图的使用 select * from 视图名;
视图的修改
方式一:
create or replace 视图名 as 多条sql语句;
方式二:
alter view 视图名 as 多条sql语句;
视图的删除
drop view 视图名1,视图名2,...;
视图的查看
方法一:
desc 视图名;
方法二:
show create view 视图名; #如果在终端上,可以规范格式; show create view 视图名\G;
视图的更新
具备以下特点的视图不允许更新
包含以下包含以下关键词的 sql 语句:
- 分组函数;
- distinct;
- group by;
- having;
- union、union all;
常量视图
create view myv as select 'john' as `name`; update myv set `name` = 'lucy'; #更新失败;
select 后面包含 子查询
create view myv as select (select max(salary) from employees) as max_salary; update myv set max_salary = 10000; #更新失败;
使用了 join 关键词
create or replace myv as select last_name, department_name from employees em inner join departments de on em.department_id = de.department_id; update myv set last_name '张飞' where last_name = 'Whalen'; #更新成功,特例; insert into myv values ('陈真', 1000); #更新失败;
from 后面接入一个不能更新的视图
create view myv1 as select max(salary) max_sal, department_id from employees group by department_id; #出现 group by 不能更新视图; create view myv2 as select * from myv1; update myv2 set max_sal = 1 where department_id = 10; #更新失败;
where子句 存在子查询,且该子查询引用了 外部from子句中的同一个表
create view myv as select last_name, email, salary from employees where emplyee_id = ( select manager_id from employees where manager_id is not null ); update myv set salary = 10 where last_name = 'Hunold'; #更新失败;
边栏推荐
- Node+Express实现文件的上传下载与删除
- Detailed explanation of oracle:pl/sql basic syntax (selection structure, loop structure and cursor)
- rider 切换主题
- office 2013 自定义样式,并且将样式设置为目录
- 直播回顾| Apache Pulsar Meetup 精彩回放(含 PPT 下载)
- Mitsubishi PLC FX3U pulse axis return function block (mc_home_p)
- 一键免费下载外文文献的方式
- Package wechat Alipay desensitization
- RF cable automatic test system can improve your test efficiency by 60 times
- 携手HMS Core分析服务,以数据助力游戏高效增长
猜你喜欢
One click free download of foreign documents
想要制作沙盒游戏?那么这一款插件你一定不能错过(Unity3D)
idea 改变字体大小
U++ learning note component and collision
leetcode 225. Implement stack with queue (simple)
Gisley keithley Software 2600 Series 2635b | 2636b | 2651a | 2657a NS sourcemeter source table Software
ECCV 2022 oral "unicorn" unifies the network structure and learning paradigm of four target tracking tasks for the first time, and SOTA on eight challenging data sets
Detailed steps for installing and uninstalling MySQL
Deep understanding of perfect hash
unity 3D 修改默认编辑器
随机推荐
剑指 Offer 17. 打印从1到最大的n位数(大数问题)
Abstract classes and interfaces
什么是扩展方法?virtual方法或abstract方法有什么区别?Interface与 Abstract Class有什么区别?
Idea change font size
7.15 week learning summary
ECCV 2022 Oral《Unicorn》首次统一了四项目标跟踪任务的网络结构与学习范式,在8个富有挑战性的数据集上SOTA...
深入理解完美哈希
趣店预制菜爆火背后,是一场慢节奏的长期主义
win10 最新版更改语言之间切换快捷键
【FAQ】应用内支付服务无法拉起支付页面常见原因分析和解决方法
One click free download of foreign documents
LeetCode 1217 玩筹码[贪心] HERODING的LeetCode之路
To create a private Ca, I use OpenSSL
UE4 learning notes checkbox instead of button + image
unity 3中获取指定gameObject的方式
What is the difference between single domain name SSL certificate and multi domain name SSL certificate?
吉时利Keithley软件2600系列2635B|2636B|2651A|2657A NS-SourceMeter源表软件
软件测试简历部分应该注意什么?
网络中的延迟和抖动问题
Detailed explanation of oracle:pl/sql basic syntax (selection structure, loop structure and cursor)