Linux——MySQL视图

一、视图

1.视图简介:

视图就是一张虚表,和真实的表一样。视图包含一系列带有名称的行和列数据。视图是从一个或多个表中导出来的,我们可以通过insert,update,delete来操作视图。当通过视图看到的数据被修改时,相应的原表的数据也会变化。同时原表发生变化,则这种变化也可以自动反映到视图中。

2.视图的优点:

  • 简单化:看到的就是需要的。视图不仅可以简化用户对数据的理解,也可以简化操作。经常被使用的查询可以制作成一个视图
  • 安全性:通过视图用户只能查询和修改所能见到的数据,数据库中其他的数据既看不见也取不到。数据库授权命令可以让每个用户对数据库的检索限制到特定的数据库对象上,但不能授权到数据库特定的行,列上。
  • 逻辑数据独立性:视图可帮助用户屏蔽真实表结构变化带来的影响

3.视图和表的区别:

  • 视图是已经编译好的SQL语句,是基于SQL语句的结果集的可视化的表。而表不是。
  • 视图没有实际的物理记录,而表有。
  • 表是内容,视图窗口 。
  • 表和视图虽然都占用物理空间,但是视图只是逻辑概念存在,而表可以及时对数据进行修改,但是视图只能用创建语句来修改 。
  • 视图是查看数据表的一种方法,可以查询数据表中某些字段构成的数据,只是一些SQL 语句的集合。从安全角度来说,视图可以防止用户接触数据表,因而不知道表结构。
  • 表属于全局模式中的表,是实表。而视图属于局部模式的表,是虚表 。
  • 视图的建立和删除只影响视图本身,而不影响对应表的基本表。

4.视图和表的联系:

视图是在基本表之上建立的表,它的结构和内容都来自于基本表,它依赖基本表存在而存在。一个视图可以对应一个基本表,也可以对应多个基本表。视图是基本的抽象和逻辑意义上建立的关系。

5.视图在什么情况下会无法执行更新

  • 视图中不包含基表中被定义为非空的列 。
  • 在定义视图的select语句后的字段列表中使用了数学表达式 。
  • 在定义视图的select 语句后字段列表中使用了聚合函数时不接受更新操作 。
  • select中,使用了union \top \group by 或having 无法接受。

6.语法:

[algroithm视图选择的算法={undefined|merge |temptable}]

  • Undefined:不常用。
  • merge :表示将使用的视图语句与视图定义合并起来,使视图定义的某一部分取代语句对应的部分
  • temptable :表示将视图的结果存入临时表,然后用临时表来执行语句

with[cascaded|local]

  • Cascaded :默认为cascaded,表示更新视图时,满足所有相关视图和表的条件
  • Local :表示更新视图时,满足该视图本身定义的条件即可

二、视图基本操作

2.1 创建单表视图

mysql> create table t1(quantity int,price int);
Query OK, 0 rows affected (0.02 sec)

mysql> insert into t1 values (3,50);
Query OK, 1 row affected (0.01 sec)

#创建单表视图
mysql> create view view_test(quantity,price,total) as select quantity,price,quantity*price from t1;
Query OK, 0 rows affected (0.00 sec)

#查看视图中反馈的内容
mysql> select * from view_test;
+----------+-------+-------+
| quantity | price | total |
+----------+-------+-------+
|        3 |    50 |   150 |
+----------+-------+-------+
1 row in set (0.00 sec)

2.2 创建多表视图

2.2.1 多表操作

mysql> create table student
    -> (
    -> s_id int(3) primary key,
    -> s_name varchar(30),
    -> s_age int (3),
    -> s_sex varchar(8)
    -> );
Query OK, 0 rows affected (0.10 sec)

mysql> create table stu_info
    -> (
    -> s_id int(3),
    -> class varchar(50),
    -> addr varchar(100)
    -> );
Query OK, 0 rows affected (0.12 sec)

#插入数据
mysql> insert into stu_info values
    -> (1,'erban','anhui'),
    -> (2,'sanban','chongqing'),
    -> (3,'yiban','shangdong');
Query OK, 3 rows affected (0.00 sec)
Records: 3  Duplicates: 0  Warnings: 0

#创建多表视图
mysql> create view stu_class(id,name,class) as 
    -> select  student.s_id,student.s_name,stu_info.class from student,stu_info
    -> where student.s_id=stu_info.s_id;
Query OK, 0 rows affected (0.00 sec)

#查看视图的基本结构
mysql> desc stu_class;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id    | int(3)      | NO   |     | NULL    |       |
| name  | varchar(30) | YES  |     | NULL    |       |
| class | varchar(50) | YES  |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)

#查看视图的基本信息
mysql> show table status like 'stu_class'\G
*************************** 1. row ***************************
           Name: stu_class
         Engine: NULL
        Version: NULL
     Row_format: NULL
           Rows: NULL
 Avg_row_length: NULL
    Data_length: NULL
Max_data_length: NULL
   Index_length: NULL
      Data_free: NULL
 Auto_increment: NULL
    Create_time: NULL
    Update_time: NULL
     Check_time: NULL
      Collation: NULL
       Checksum: NULL
 Create_options: NULL
        Comment: VIEW
1 row in set (0.00 sec)

#查看视图的详细信息
mysql> show create view stu_class\G
*************************** 1. row ***************************
                View: stu_class
         Create View: CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `stu_class` AS select `student`.`s_id` AS `id`,`student`.`s_name` AS `name`,`stu_info`.`class` AS `class` from (`student` join `stu_info`) where (`student`.`s_id` = `stu_info`.`s_id`)
character_set_client: utf8
collation_connection: utf8_general_ci
1 row in set (0.00 sec)

2.2.2 查看数据库中的视图

查询数据库中所有的视图

#直接查询information_schema库中的views表,来查看所有的视图

mysql> select * from information_schema.views\G
*************************** 1. row ***************************
       TABLE_CATALOG: def
        TABLE_SCHEMA: test
          TABLE_NAME: view_t
     VIEW_DEFINITION: select `test`.`t`.`quantiy` AS `quantiy` from `test`.`t`
        CHECK_OPTION: NONE
        IS_UPDATABLE: YES
             DEFINER: root@localhost
       SECURITY_TYPE: DEFINER
CHARACTER_SET_CLIENT: utf8
COLLATION_CONNECTION: utf8_general_ci
*************************** 2. row ***************************
       TABLE_CATALOG: def
        TABLE_SCHEMA: test
          TABLE_NAME: view_t2
     VIEW_DEFINITION: select `test`.`t`.`quantiy` AS `qty`,`test`.`t`.`price` AS `price`,(`test`.`t`.`quantiy` * `test`.`t`.`price`) AS `total` from `test`.`t`
        CHECK_OPTION: NONE
        IS_UPDATABLE: YES
             DEFINER: root@localhost
       SECURITY_TYPE: DEFINER
CHARACTER_SET_CLIENT: utf8
COLLATION_CONNECTION: utf8_general_ci
104 rows in set (0.02 sec)

查询指定数据库中的视图

#where后面指定一个库名,也就是查看指定库中的所有视图

mysql>  select * from information_schema.views where table_schema = 'mytest'\G
*************************** 1. row ***************************
       TABLE_CATALOG: def
        TABLE_SCHEMA: mytest
          TABLE_NAME: stu_class
     VIEW_DEFINITION: select `mytest`.`student`.`s_id` AS `id`,`mytest`.`student`.`s_name` AS `name`,`mytest`.`stu_info`.`class` AS `class` from `mytest`.`student` join `mytest`.`stu_info` where (`mytest`.`student`.`s_id` = `mytest`.`stu_info`.`s_id`)
        CHECK_OPTION: NONE
        IS_UPDATABLE: YES
             DEFINER: root@localhost
       SECURITY_TYPE: DEFINER
CHARACTER_SET_CLIENT: utf8
COLLATION_CONNECTION: utf8_general_ci
*************************** 2. row ***************************
       TABLE_CATALOG: def
        TABLE_SCHEMA: mytest
          TABLE_NAME: view_test
     VIEW_DEFINITION: select `mytest`.`t1`.`quantity` AS `quantity`,`mytest`.`t1`.`price` AS `price`,(`mytest`.`t1`.`quantity` * `mytest`.`t1`.`price`) AS `total` from `mytest`.`t1`
        CHECK_OPTION: NONE
        IS_UPDATABLE: YES
             DEFINER: root@localhost
       SECURITY_TYPE: DEFINER
CHARACTER_SET_CLIENT: utf8
COLLATION_CONNECTION: utf8_general_ci
2 rows in set (0.00 sec)

2.3 修改视图

2.3.1 create

  • or replace:替换
#创建视图view_a
mysql> create or replace view  view_a as select * from t1;
Query OK, 0 rows affected (0.00 sec)

mysql> desc view_a;
+----------+---------+------+-----+---------+-------+
| Field    | Type    | Null | Key | Default | Extra |
+----------+---------+------+-----+---------+-------+
| quantity | int(11) | YES  |     | NULL    |       |
| price    | int(11) | YES  |     | NULL    |       |
+----------+---------+------+-----+---------+-------+
2 rows in set (0.00 sec)

mysql> select * from view_a;
+----------+-------+
| quantity | price |
+----------+-------+
|        3 |    50 |
+----------+-------+
1 row in set (0.00 sec)

2.3.2 alter

#修改视图view_a
mysql> alter view view_a as select  quantity from t1;
Query OK, 0 rows affected (0.01 sec)

mysql> desc view_a;
+----------+---------+------+-----+---------+-------+
| Field    | Type    | Null | Key | Default | Extra |
+----------+---------+------+-----+---------+-------+
| quantity | int(11) | YES  |     | NULL    |       |
+----------+---------+------+-----+---------+-------+
1 row in set (0.00 sec)

mysql> select * from view_a;
+----------+
| quantity |
+----------+
|        3 |
+----------+
1 row in set (0.00 sec)

2.4 更新视图

2.4.1 Update更新

#查看表以及视图的数据,其中quantity对应视图的abc字段
mysql> select * from t1;
+----------+-------+
| quantity | price |
+----------+-------+
|        3 |    50 |
+----------+-------+
1 row in set (0.00 sec)

mysql> select * from view_a;
+----------+
| quantity |
+----------+
|        3 |
+----------+
1 row in set (0.00 sec)

#更新视图
mysql> update view_a set quantity=5;
Query OK, 1 row affected (0.01 sec)
Rows matched: 1  Changed: 1  Warnings: 0

#查看更新后的视图
mysql> select * from view_a;
+----------+
| quantity |
+----------+
|        5 |
+----------+
1 row in set (0.00 sec)

#查看更新后的表中对应视图中的数据
mysql> select * from t1;
+----------+-------+
| quantity | price |
+----------+-------+
|        5 |    50 |
+----------+-------+
1 row in set (0.00 sec)

2.4.2 Insert更新

#对表插入数据
mysql> insert into t1 values(3,5);
Query OK, 1 row affected (0.00 sec)

#查询更新后的表
mysql> select * from t1;
+----------+-------+
| quantity | price |
+----------+-------+
|        5 |    50 |
|        3 |     5 |
+----------+-------+
2 rows in set (0.00 sec)

#查询更新后的视图
mysql> select * from view_a;
+----------+
| quantity |
+----------+
|        5 |
|        3 |
+----------+
2 rows in set (0.00 sec)

2.4.3 delete删除表数据

mysql> select * from view_a;
+----------+
| quantity |
+----------+
|        5 |
|        3 |
+----------+
2 rows in set (0.00 sec)

#删除指定表和视图表中的数据
mysql> delete from view_a where quantity=3;
Query OK, 1 row affected (0.00 sec)

mysql> select * from view_a;
+----------+
| quantity |
+----------+
|        5 |
+----------+
1 row in set (0.00 sec)

2.5 删除视图

#删除指定视图
mysql> drop view if exists stu_class;
Query OK, 0 rows affected (0.00 sec)

mysql> drop view if exists stu_class;
Query OK, 0 rows affected, 1 warning (0.00 sec)

#查看上一条命令反馈的警告!!!
mysql> show warnings;
+-------+------+----------------------------------+
| Level | Code | Message                          |
+-------+------+----------------------------------+
| Note  | 1051 | Unknown table 'mytest.stu_class' |
+-------+------+----------------------------------+
1 row in set (0.00 sec)

#查询删除的视图是否已删除
mysql> SHOW CREATE VIEW stu_class;
ERROR 1146 (42S02): Table 'mytest.stu_class' doesn't exist

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值