我的mysql学习心得(十一)我的mysql学习心得(一)
我的mysql学习心得(二)
我的mysql学习心得(三)
我的mysql学习心得(四)
我的mysql学习心得(五)
我的mysql学习心得(六)
我的mysql学习心得(七)
我的mysql学习心得(八)
我的mysql学习心得(九)
我的mysql学习心得(十)
这一篇《我的mysql学习心得(十一)》将会讲解mysql的视图
使用视图的理由是什么?
1、安全性:一般是这样做的:创建一个视图,定义好该视图所操作的数据。
之后将用户权限与视图绑定,这样的方式是使用到了一个特性:grant语句可以针对视图进行授予权限。
2、查询性能提高
3、有灵活性的功能需求后,需要改动表的结构而导致工作量比较大,那么可以使用虚拟表的形式达到少修改的效果。
这是在实际开发中比较有用的
4、复杂的查询需求,可以进行问题分解,然后将创建多个视图获取数据。将视图联合起来就能得到需要的结果了。
创建视图
创建视图的语法
create [or replace] [algorithm = {undefined | merge | temptable}] view view_name [(column_list)] as select_statement [with [cascaded | local] check option]
其中,create:表示新建视图;
replace:表示替换已有视图
algorithm :表示视图选择算法
view_name :视图名
column_list:属性列
select_statement:表示select语句
[with [cascaded | local] check option]参数表示视图在更新时保证在视图的权限范围之内
可选的algorithm子句是对标准sql的mysql扩展。
algorithm可取三个值:merge、temptable或undefined。
如果没有algorithm子句,默认算法是undefined(未定义的)。算法会影响mysql处理视图的方式。
对于merge,会将引用视图的语句的文本与视图定义合并起来,使得视图定义的某一部分取代语句的对应部分。
对于temptable,视图的结果将被置于临时表中,然后使用它执行语句。
对于undefined,mysql自己选择所要使用的算法。如果可能,它倾向于merge而不是temptable,
这是因为merge通常更有效,而且如果使用了临时表,视图是不可更新的。
local和cascaded为可选参数,决定了检查测试的范围,默认值为cascaded。
脚本 视图的数据来自于两个表
create table student (stuno int ,stuname nvarchar(60))create table stuinfo (stuno int ,class nvarchar(60),city nvarchar(60))insert into student values(1,'wanglin'),(2,'gaoli'),(3,'zhanghai')insert into stuinfo values(1,'wuban','henan'),(2,'liuban','hebei'),(3,'qiban','shandong')-- 创建视图create view stu_class(id,name,glass) as select student.`stuno`,student.`stuname`,stuinfo.`class`from student ,stuinfo where student.`stuno`=stuinfo.`stuno`select * from stu_class
查看视图
查看视图必须要有show view权限
查看视图的方法包括:describe、show table status、show create view
describe查看视图基本信息
describe 视图名
describe stu_class
结果显示了视图的字段定义、字段的数据类型、是否为空、是否为主/外键、默认值和额外信息
describe一般都简写成desc
show table status语句查看查看视图基本信息
查看视图的信息可以通过show table status的方法
show table status like 'stu_class'
name engine version row_format rows avg_row_length data_length max_data_length index_length data_free auto_increment create_time update_time check_time collation checksum create_options comment--------- ------ ------- ---------- ------ -------------- ----------- --------------- ------------ --------- -------------- ----------- ----------- ---------- --------- -------- -------------- -------stu_class (null) (null) (null) (null) (null) (null) (null) (null) (null) (null) (null) (null) (null) (null) (null) (null) view
comment的值为view说明该表为视图,其他的信息为null说明这是一个虚表,如果是基表那么会基表的信息,这是基表和视图的区别
show create view语句查看视图详细信息
show create view stu_class
view create view character_set_client collation_connection--------- ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ -------------------- --------------------stu_class create algorithm=undefined definer=`root`@`localhost` sql security definer view `stu_class` as select `student`.`stuno` as `id`,`student`.`stuname` as `name`,`stuinfo`.`class` as `class` from (`student` join `stuinfo`) where (`student`.`stuno` = `stuinfo`.`stuno`) utf8 utf8_general_ci
执行结果显示视图的名称、创建视图的语句等信息
在views表中查看视图的详细信息
在mysql中,information_schema views表存储了关于数据库中的视图的信息
通过对views表的查询可以查看数据库中所有视图的详细信息
select * from `information_schema`.`views`
table_catalog table_schema table_name view_definition check_option is_updatable definer security_type character_set_client collation_connection------------- ------------ ---------- -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ------------ ------------ -------------- ------------- -------------------- --------------------def school stu_class select `school`.`student`.`stuno` as `id`,`school`.`student`.`stuname` as `name`,`school`.`stuinfo`.`class` as `class` from `school`.`student` join `school`.`stuinfo` where (`school`.`student`.`stuno` = `school`.`stuinfo`.`stuno`) none yes root@localhost definer utf8 utf8_general_ci
当前实例下只有一个视图stu_class
修改视图
修改视图是指修改数据库中存在的视图,当基本表的某些字段发生变化时,可以通过修改视图来保持与基本表的一致性。
mysql中通过create or replace view 语句和alter语句来修改视图
语法如下:
alter or replace [algorithm = {undefined | merge | temptable}]view view_name [(column_list)]as select_statement[with [cascaded | local] check option]
该语句用于更改已有视图的定义。其语法与create view类似。当视图不存在时创建,存在时进行修改
修改视图
delimiter $$create or replace view `stu_class` as select `student`.`stuno` as `id`from (`student` join `stuinfo`)where (`student`.`stuno` = `stuinfo`.`stuno`)$$delimiter ;
通过desc来查看更改之后的视图定义
desc stu_class
可以看到只查询一个字段
alter语句修改视图
alter [algorithm = {undefined | merge | temptable}]view view_name [(column_list)]as select_statement[with [cascaded | local] check option]
这里关键字跟前面的一样,这里不做介绍
使用alter语句修改视图 stu_class
alter view stu_class as select stuno from student;
使用desc查看
desc stu_class
更新视图
更新视图是指通过视图来插入、更新、删除表数据,因为视图是虚表,其中没有数据。
通过视图更新的时候都是转到基表进行更新,如果对视图增加或者删除记录,实际上是对基表增加或删除记录
我们先修改一下视图定义
alter view stu_class as select stuno,stuname from student;
查询视图数据
update
update stu_class set stuname='xiaofang' where stuno=2
查询视图数据
更新成功
insert
insert into stu_class values(6,'haojie')
插入成功
delete
delete from stu_class where stuno=1
删除成功
当视图中包含如下内容的时候,视图的更新操作将不能被执行
(1)视图中包含基本中被定义为非空的列
(2)定义视图的select语句后的字段列表中使用了数学表达式
(3)定义视图的select语句后的字段列表中使用聚合函数
(4)定义视图的select语句中使用了distinct、union、top、group by 、having子句
删除视图
删除视图使用drop view语法
drop view [if exists]view_name [, view_name] ...[restrict | cascade]
drop view能够删除1个或多个视图。必须在每个视图上拥有drop权限
可以使用关键字if exists来防止因不存在的视图而出错
删除stu_class视图
drop view if exists stu_class
如果名称为 stu_class 的视图存在则删除
使用show create view语句查看结果
show create view stu_class
query: -- update stu_class set stuname='xiaofang' where stuno=2; -- delete from stu_class where stuno=1 -- select * from stu_class; -- ...error code: 1146table 'school.stu_class' doesn't existexecution time : 0 sectransfer time : 0 sectotal time : 0.004 sec---------------------------------------------------
该视图不存在,删除成功
总结
sqlserver里实际上跟mysql一样,也是有信息架构视图的
信息架构视图 (transact-sql)
信息架构视图是 sql server 提供的几种获取元数据的方法之一。
信息架构视图提供独立于系统表的内部 sql server 元数据视图。
尽管已经对基础系统表进行了重要的修改,信息架构视图仍然可使应用程序正常工作。
sql server 中包含的信息架构视图符合 iso 标准中的信息架构定义。
信息架构视图的数据是存放在系统数据库resource 数据库里面
mssqlsystemresource.mdf
利用information_schema视图来拼接 sql 语句
如有不对的地方,欢迎大家拍砖o(∩_∩)o