赞
踩
视图(View
)是一种虚拟存在的表。视图中的数据并不在数据库中实际存在,行和列数据来自定义视图的查询中使用的表,并且是在使用视图时动态生成的。
通俗的讲,视图只保存了查询的SQL
逻辑,不保存查询结果。所以我们在创建视图的时候,主要的工作就落在创建这条SQL
查询语句上。
数据准备:
# 创建student表 create table student ( id int auto_increment primary key comment '主键ID', name varchar(10) comment '姓名', no varchar(10) comment '学号' ) comment '学生表'; # 往student表里面插入数据 insert into student values (null, '黛绮丝', '2000100101'), (null, '谢逊', '2000100102'), (null, '殷天正', '2000100103'), (null, '韦一笑', '2000100104'); # 创建course表 create table course ( id int auto_increment primary key comment '主键ID', name varchar(10) comment '课程名称' ) comment '课程表'; # 往课程表里面添加数据 insert into course values (null, 'Java'), (null, 'PHP'), (null, 'MySQL'), (null, 'Hadoop'); # 创建student_course中间表 create table student_course ( id int auto_increment comment '主键' primary key, studentid int not null comment '学生ID', courseid int not null comment '课程ID', constraint fk_courseid foreign key (courseid) references course (id), constraint fk_studentid foreign key (studentid) references student (id) ) comment '学生课程中间表'; # 往中间表里面插入数据 insert into student_course values (null, 1, 1), (null, 1, 2), (null, 1, 3), (null, 2, 2), (null, 2, 3), (null, 3, 4);
CREATE [OR REPLACE] VIEW 视图名称[(列名列表)] AS SELECT语句 [ WITH [ CASCADED | LOCAL ] CHECK OPTION ];
示例:
# 创建名为 stu_v_1 的视图:数据来源于 student 表 id <= 10 的 id, name信息
create or replace view stu_v_1 as
select id, name
from student
where id <= 10;
查看创建视图语句:SHOW CREATE VIEW 视图名称;
查看视图数据:SELECT * FROM 视图名称 ...... ;
示例1:查询stu_v_1视图的创建语句
# 查询stu_v_1视图的创建语句
show create view stu_v_1;
展开后:
CREATE ALGORITHM = UNDEFINED DEFINER =`root`@`%` SQL SECURITY DEFINER VIEW `stu_v_1` AS
select `student`.`id` AS `id`, `student`.`name` AS `name`
from `student`
where (`student`.`id` <= 10);
示例2:查询stu_v_1视图里面的数据
select *
from stu_v_1;
select *
from stu_v_1
where id < 3;
说明:
如果基本表结构发生变化甚至被删除,则查询视图也会报错。
方式一:CREATE [OR REPLACE] VIEW 视图名称[(列名列表)] AS SELECT语句 [ WITH [ CASCADED | LOCAL ] CHECK OPTION ]
方式二:ALTER VIEW 视图名称[(列名列表)] AS SELECT语句 [ WITH [ CASCADED | LOCAL ] CHECK OPTION ]
示例:
# 第一种方式
create or replace view stu_v_1 as
select id, name, no
from student
where id <= 10;
# 第二种方式
alter view stu_v_1 as select id, name, no
from student
where id <= 10;
DROP VIEW [IF EXISTS] 视图名称 [,视图名称] ...
示例:
# 删除新创建的stu_v_1视图
drop view if exists stu_v_1;
上述我们演示了,视图应该如何创建、查询、修改、删除,那么我们能不能通过视图来插入、更新数据呢? 接下来,做一个测试。
# 以student表id <= 10的id, name作为初始数据创建stu_v_1视图
create or replace view stu_v_1 as
select id, name
from student
where id <= 10;
# 查询视图中包含的数据
select *
from stu_v_1;
# 往视图新增2条数据
insert into stu_v_1
values (6, 'Tom'),
(17, 'Tom22');
# 再次查询视图中包含的数据
select *
from stu_v_1;
因为我们在创建视图的时候,指定的条件为 id<=10
,id
为17
的数据,是不符合条件的,所以没有查询出来,但是这条数据确实是已经成功的插入到了基表中。
# 查询基表的数据
select *
from student;
如果我们定义视图时,如果指定了条件,然后我们在插入、修改、删除数据时,是否可以做到必须满足条件才能操作,否则不能够操作呢? 答案是可以的,这就需要借助于视图的检查选项了。
当使用WITH CHECK OPTION
子句创建视图时,MySQL
会通过视图检查正在更改的每个行,例如 插入,更新,删除,以使其符合视图的定义。 MySQL
允许基于另一个视图创建视图,它还会检查依赖视图中的规则以保持一致性。为了确定检查的范围,mysql
提供了两个选项: CASCADED
和 LOCAL
,默认值为 CASCADED
。
比如,v2
视图是基于v1
视图的,如果在v2
视图创建的时候指定了检查选项为 cascaded
,但是v1
视图创建时未指定检查选项。 则在执行检查时,不仅会检查v2
,还会级联检查v2
的关联视图v1
。
比如,v2
视图是基于v1
视图的,如果在v2
视图创建的时候指定了检查选项为 local
,但是v1
视图创建时未指定检查选项。 则在执行检查时,只会检查v2
,不会检查v2
的关联视图v1
。
要使视图可更新,视图中的行与基础表中的行之间必须存在一对一的关系(1
行基础表数据对应视图里面的一行数据,不会出现类似2
行基础表数据对应视图里面的一行数据的情况)。如果视图包含以下任何一项,则该视图不可更新:
SUM()
、 MIN()
、 MAX()
、 COUNT()
等)DISTINCT
GROUP BY
HAVING
UNION
或者 UNION ALL
示例演示:
# 以基础表student的count()函数为基础创建视图
create view stu_v_count as
select count(*)
from student;
上述的视图中,就只有一个单行单列的数据,如果我们对这个视图进行更新或插入的,将会报错。
insert into stu_v_count
values (10);
数据准备:
create table tb_user ( id int primary key auto_increment comment '主键', name varchar(50) not null comment '用户名', phone varchar(11) not null comment '手机号', email varchar(100) comment '邮箱', profession varchar(11) comment '专业', age tinyint unsigned comment '年龄', gender char(1) comment '性别 , 1: 男, 2: 女', status char(1) comment '状态', createtime datetime comment '创建时间' ) comment '系统用户表'; INSERT INTO tb_user (name, phone, email, profession, age, gender, status, createtime) VALUES ('吕布', '17799990000', 'lvbu666@163.com', '软件工程', 23, '1', '6', '2001-02-02 00:00:00'); INSERT INTO tb_user (name, phone, email, profession, age, gender, status, createtime) VALUES ('曹操', '17799990001', 'caocao666@qq.com', '通讯工程', 33, '1', '0', '2001-03-05 00:00:00'); INSERT INTO tb_user (name, phone, email, profession, age, gender, status, createtime) VALUES ('赵云', '17799990002', '17799990@139.com', '英语', 34, '1', '2', '2002-03-02 00:00:00'); INSERT INTO tb_user (name, phone, email, profession, age, gender, status, createtime) VALUES ('孙悟空', '17799990003', '17799990@sina.com', '工程造价', 54, '1', '0', '2001-07-02 00:00:00'); INSERT INTO tb_user (name, phone, email, profession, age, gender, status, createtime) VALUES ('花木兰', '17799990004', '19980729@sina.com', '软件工程', 23, '2', '1', '2001-04-22 00:00:00'); INSERT INTO tb_user (name, phone, email, profession, age, gender, status, createtime) VALUES ('大乔', '17799990005', 'daqiao666@sina.com', '舞蹈', 22, '2', '0', '2001-02-07 00:00:00'); INSERT INTO tb_user (name, phone, email, profession, age, gender, status, createtime) VALUES ('露娜', '17799990006', 'luna_love@sina.com', '应用数学', 24, '2', '0', '2001-02-08 00:00:00'); INSERT INTO tb_user (name, phone, email, profession, age, gender, status, createtime) VALUES ('程咬金', '17799990007', 'chengyaojin@163.com', '化工', 38, '1', '5', '2001-05-23 00:00:00'); INSERT INTO tb_user (name, phone, email, profession, age, gender, status, createtime) VALUES ('项羽', '17799990008', 'xiaoyu666@qq.com', '金属材料', 43, '1', '0', '2001-09-18 00:00:00'); INSERT INTO tb_user (name, phone, email, profession, age, gender, status, createtime) VALUES ('白起', '17799990009', 'baiqi666@sina.com', '机械工程及其自动 化', 27, '1', '2', '2001-08-16 00:00:00'); INSERT INTO tb_user (name, phone, email, profession, age, gender, status, createtime) VALUES ('韩信', '17799990010', 'hanxin520@163.com', '无机非金属材料工 程', 27, '1', '0', '2001-06-12 00:00:00'); INSERT INTO tb_user (name, phone, email, profession, age, gender, status, createtime) VALUES ('荆轲', '17799990011', 'jingke123@163.com', '会计', 29, '1', '0', '2001-05-11 00:00:00'); INSERT INTO tb_user (name, phone, email, profession, age, gender, status, createtime) VALUES ('兰陵王', '17799990012', 'lanlinwang666@126.com', '工程造价', 44, '1', '1', '2001-04-09 00:00:00'); INSERT INTO tb_user (name, phone, email, profession, age, gender, status, createtime) VALUES ('狂铁', '17799990013', 'kuangtie@sina.com', '应用数学', 43, '1', '2', '2001-04-10 00:00:00'); INSERT INTO tb_user (name, phone, email, profession, age, gender, status, createtime) VALUES ('貂蝉', '17799990014', '84958948374@qq.com', '软件工程', 40, '2', '3', '2001-02-12 00:00:00'); INSERT INTO tb_user (name, phone, email, profession, age, gender, status, createtime) VALUES ('妲己', '17799990015', '2783238293@qq.com', '软件工程', 31, '2', '0', '2001-01-30 00:00:00'); INSERT INTO tb_user (name, phone, email, profession, age, gender, status, createtime) VALUES ('芈月', '17799990016', 'xiaomin2001@sina.com', '工业经济', 35, '2', '0', '2000-05-03 00:00:00'); INSERT INTO tb_user (name, phone, email, profession, age, gender, status, createtime) VALUES ('嬴政', '17799990017', '8839434342@qq.com', '化工', 38, '1', '1', '2001-08-08 00:00:00'); INSERT INTO tb_user (name, phone, email, profession, age, gender, status, createtime) VALUES ('狄仁杰', '17799990018', 'jujiamlm8166@163.com', '国际贸易', 30, '1', '0', '2007-03-12 00:00:00'); INSERT INTO tb_user (name, phone, email, profession, age, gender, status, createtime) VALUES ('安琪拉', '17799990019', 'jdodm1h@126.com', '城市规划', 51, '2', '0', '2001-08-15 00:00:00'); INSERT INTO tb_user (name, phone, email, profession, age, gender, status, createtime) VALUES ('典韦', '17799990020', 'ycaunanjian@163.com', '城市规划', 52, '1', '2', '2000-04-12 00:00:00'); INSERT INTO tb_user (name, phone, email, profession, age, gender, status, createtime) VALUES ('廉颇', '17799990021', 'lianpo321@126.com', '土木工程', 19, '1', '3', '2002-07-18 00:00:00'); INSERT INTO tb_user (name, phone, email, profession, age, gender, status, createtime) VALUES ('后羿', '17799990022', 'altycj2000@139.com', '城市园林', 20, '1', '0', '2002-03-10 00:00:00'); INSERT INTO tb_user (name, phone, email, profession, age, gender, status, createtime) VALUES ('姜子牙', '17799990023', '37483844@qq.com', '工程造价', 29, '1', '4', '2003-05-26 00:00:00');
为了保证数据库表的安全性,开发人员在操作tb_user
表时,只能看到的用户的基本字段,屏蔽手机号和邮箱两个字段。
# 创建视图 屏蔽手机号和邮箱两个字段
create view tb_user_view as
select id, name, profession, age, gender, status, createtime
from tb_user;
我们去查看视图里面的数据:
# 查询视图里面的数据
select *
from tb_user_view;
查询每个学生所选修的课程(三张表联查),这个功能在很多的业务中都有使用到,为了简化操作,定义一个视图。
# 创建视图
create view tb_stu_course_view as
select s.name student_name, s.no student_no, c.name course_name
from student s,
student_course sc,
course c
where s.id = sc.studentid
and sc.courseid = c.id;
我们去查看视图里面的数据:
# 查询视图里面的数据
select *
from tb_stu_course_view;
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。