什么是视图?
视图是从一个或多个表中导出来的表,是一种虚拟存在的表。
视图就像一个窗口,通过这个窗口可以看到系统专门提供的数据。
这样,用户可以不用看到整个数据库中的数据,而只关心对自己有用的数据。
数据库中只存放了视图的定义,而没有存放视图中的数据,这些数据存放在原来的表中。
使用视图查询数据时,数据库系统会从原来的表中取出对应的数据。
视图中的数据依赖于原来表中的数据,一旦表中数据发生改变,显示在视图中的数据也会发生改变。
视图的作用
1.使操作简单化,可以对经常使用的查询定义一个视图,使用户不必为同样的查询操作指定条件
2.增加数据的安全性,通过视图,用户只能查询和修改指定的数据。
3.提高表的逻辑独立性,视图可以屏蔽原有表结构变化带来的影响。
总而言之,使用视图的大部分情况是为了保障数据安全性,提高查询效率
一、创建视图
首先建立一个表 t_product
> create database test_view;
> use test_view;
> create table t_product(
> id int primary key,
> name varchar(20),
> price float(6,2));
插入数据
> insert into t_product
> values(1, 'apple',6.5),
> (2, 'banana',4.5),
>(3, ‘orange’,1.5),
>(4,’pear’,2.5);
在表t_product的基础上创建含有属性id,name的视图
> create view view_selectproduct
> as select id,name from t_product;
查询视图
> select * from view_selectproduct;
二、查看视图
使用show查看; 显示表和视图的名字
> show tables;
使用describe查看/或者简写为desc;显示视图的字段定义,数据类型,外键等
> describe view_selectproduct;
使用show table status查看视图的详细信息
> show table status from test_view\G;
使用show table status查看视图view_selectproduct的详细信息
> show table status from test_view
> like 'view_selectproduct' \G;
使用show create view查看视图的详细信息
> show create view view_selectproduct;
在views表中查看视图详细信息,information_schema>views
> show create view view_selectproduct;
> select * from views;
> select * from views
> where table_name='view_selectproduct'\G;
三、删除视图
使用drop删除视图
首先查看下修改前的视图
>desc view_selectproduct;
使用drop删除视图
>drop view view_selectproduct;
查看视图是否存在
>desc view_selectproduct;
或者
>select * from view_selectproduct
四、修改视图
(1)利用create创建查看下视图,这要求之前没有该视图
> create view view_selectproduct
> as select name from t_product;
再查看视图
>select * from view_selectproduct
(2)使用create or replace view语句修改视图
先删除已有的视图
> drop view view_selectproduct;
在创建一个视图
> create view view_selectproduct
> as select * from t_product;
再查看视图
>select * from view_selectproduct
使用create or replace view语句修改视图
> create or replace view view_selectproduct
> as select name from t_product;
再查看视图
(3)使用alter语句修改视图
修改已经建立好的视图
> alter view view_selectproduct
> as select name from t_product;
五、利用视图操作基本表数据(适应于表的操作)
(1)添加数据
首先查看当前的表
> select * from view_selectproduct;
插入一行数据
> insert into view_selectproduct values(5,'grape',6.0);
(2)删除数据
首先查看当前的表
> select * from view_selectproduct;
删除一行数据
> delete from view_selectproduct
> where name='grape';
(3)更新数据
首先查看当前的表
> select * from view_selectproduct;
更新一行数据
> update view_selectproduct
> set price=7
> where name='apple';
六、创建各种不同的视图
首先创建两个表t_student, t_group
创建表t_student
> create table t_student(
> id int primary key,
> name varchar(10),
> sex varchar(1),
> group_id int);
向表t_student插入数据
> insert into t_student
> values(1,'tom1','m',1),
> (2,'tom2','w',1),
> (3,'tom3','m',2)
> (4,'tom4','m',2);
> (5,'tom5','w',2);
创建表t_group
> create table t_groud(
> id int primary key,
> name varchar(10));
向表 t_group插入数据
> insert into t_group
> values(1,'group1'),
> (2,'group2'),
> (3,'group3');
> (4,'group4');
(1)封装实现查询常量语句的视图
> create view view_test1
> as select 5;
(2)封装使用聚合函数(sum,min,max,count)的视图
> create view view_test2
> as select sum(id) from t_student;
(3)封装了实现排序功能查询语句的视图
> create view view_test3
> as select name
> from t_student
> order by id desc;
(4)封装了实现表内连接查询语句的视图
> create view view_test4
> as select s.name
> from t_student as s, t_group as g
> where s.group_id=g.id and g.id=2;
(5)封装了实现表外连接语句的视图
> create view view_test5
> as select s.name
> from t_student s left join t_group g
> on s.group_id=g.id
> where g.id=2;
(6)封装实现子查询语句的视图
> create view view_test6
> as select s.name
> from t_student s
> where s.group_id in(select id from t_group);
(7)封装了实现记录联合查询语句的视图
> create view view_test7
> as
> select id, name from t_student
> union
> select id, name from t_group;
PS:结合的数目要一致
Comments