MySQL之視圖

什麼是視圖?mysql

一張虛表,和真實的表同樣。視圖包含一系列帶有名稱的行和列數據。視圖是從一個或多個表中導出來的,咱們能夠經過insert,update,delete來操做視圖。當經過視圖看到的數據被修改時,相應的原表的數據也會變化。同時原表發生變化,則這種變化也能夠自動反映到視圖中。sql

視圖具備如下優勢:數據庫

  1. 簡單化:看到的就是須要的。視圖不只能夠簡化用戶對數據的理解,也能夠簡化操做。常常被使用的查詢能夠製做成一個視圖;
  2. 安全性:經過視圖用戶只能查詢和修改所能見到的數據,數據庫中其餘的數據既看不見也取不到。數據庫受權命令可讓每一個用戶對數據庫的檢索限制到特定的數據庫對象上,但不能受權到數據庫特定的行,列上;
  3. 邏輯數據獨立性:視圖可幫助用戶屏蔽真實表結構變化帶來的影響。

視圖和表的區別以及聯繫是什麼?安全

二者的區別:ide

  • 視圖是已經編譯好的SQL語句,是基於SQL語句的結果集的可視化的表,而表不是;
  • 視圖沒有實際的物理記錄,而表有;
  • 表是內容,視圖窗口;
  • 表和視圖雖然都佔用物理空間,可是視圖只是邏輯概念存在,而表能夠及時對數據進行修改,可是視圖只能用建立語句來修改 ;
  • 視圖是查看數據表的一種方法,能夠查詢數據表中某些字段構成的數據,只是一些SQL 語句的集合。從安全角度來講,視圖能夠防止用戶接觸數據表,於是不知道表結構 ;
  • 表屬於全局模式中的表,是實表。而視圖屬於局部模式的表,是虛表;
  • 視圖的創建和刪除隻影響視圖自己,而不影響對應表的基本表。

二者的聯繫:ui

視圖是在基本表之上創建的表,它的結構和內容都來自於基本表,它依賴基本表存在而存在。一個視圖能夠對應一個基本表,也能夠對應多個基本表。視圖是基本的抽象和邏輯意義上創建的關係。code

1、建立視圖

一、建立單表視圖orm

#建立表
mysql> create table t(
    -> quantity int,
    -> price int
    -> );
#插入數據
mysql> insert into t values(3,50);
#建立視圖
mysql> create view view_t as select quantity,price,quantity*price as total from t;
Query OK, 0 rows affected (0.00 sec)

mysql> select * from view_t;               查看視圖中的數據
+----------+-------+-------+
| quantity | price | total |
+----------+-------+-------+
|        3 |    50 |   150 |
+----------+-------+-------+
1 row in set (0.01 sec)

二、建立多表視圖對象

#建立基本表
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.02 sec)

mysql> create table stu_info(
    -> s_id int(3),
    -> class varchar(50),
    -> addr varchar(100)
    -> );
Query OK, 0 rows affected (0.01 sec)
#插入數據
mysql> insert into stu_info values
    -> (1,'erban','anhui'),
    -> (2,'sanban','chongqing'),
    -> (3,'yiban','shandong');
Query OK, 3 rows affected (0.01 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;

三、查看視圖的相關信息ci

#查看視圖的表結構
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)
#也能夠直接查詢information_schema庫中的views表,來查看全部的視圖
mysql> select * from information_schema.views where table_schema='test1'\G
#where後面指定的是一個庫名,也就是查看test02這個庫中的全部視圖

四、修改視圖
方法一:

mysql> create or replace view view_t as select * from t;
Query OK, 0 rows affected (0.00 sec)

mysql> select * from view_t;           查看修改後的視圖
+----------+-------+
| quantity | price |
+----------+-------+
|        3 |    50 |
+----------+-------+
1 row in set (0.00 sec)

方法二:

#修改指定視圖的列名
mysql> alter view view_t(abc) as select quantity from t;
Query OK, 0 rows affected (0.00 sec)
#查看修改後的表結構
mysql> desc view_t;
+-------+---------+------+-----+---------+-------+
| Field | Type    | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| abc   | int(11) | YES  |     | NULL    |       |
+-------+---------+------+-----+---------+-------+
1 row in set (0.00 sec)

五、更新視圖
1)update指令更新

#查看錶以及視圖的數據,其中quantity對應視圖的abc字段
mysql> select * from t;
+----------+-------+
| quantity | price |
+----------+-------+
|        3 |    50 |
+----------+-------+
1 row in set (0.00 sec)

mysql> select * from view_t;
+------+
| abc  |
+------+
|    3 |
+------+
1 row in set (0.01 sec)

mysql> update view_t set abc=5;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0
#查看更新後的視圖
mysql> select * from view_t;
+------+
| abc  |
+------+
|    5 |
+------+
1 row in set (0.00 sec)
#查看更新後的表
mysql> select * from t;
+----------+-------+
| quantity | price |
+----------+-------+
|        5 |    50 |
+----------+-------+
1 row in set (0.00 sec)

2)insert指令更新

#查看錶的數據
mysql> select * from t;
+----------+-------+
| quantity | price |
+----------+-------+
|        5 |    50 |
+----------+-------+
1 row in set (0.00 sec)
#查看視圖的數據
mysql> select * from view_t;
+------+
| abc  |
+------+
|    5 |
+------+
1 row in set (0.00 sec)
#向表中插入數據
mysql> insert into t values(3,5);
Query OK, 1 row affected (0.00 sec)
#查看視圖的數據
mysql> select * from view_t;
+------+
| abc  |
+------+
|    5 |
|    3 |
+------+
2 rows in set (0.00 sec)
#查看錶的數據
mysql> select * from t;
+----------+-------+
| quantity | price |
+----------+-------+
|        5 |    50 |
|        3 |     5 |
+----------+-------+

3) delete指令刪除表數據

#建立新的視圖
mysql> create view view_t2(qty,price,total) as select quantity,price,quantity*price from t;
mysql> select * from view_t2;
+------+-------+-------+
| qty  | price | total |
+------+-------+-------+
|    5 |    50 |   250 |
|    3 |     5 |    15 |
+------+-------+-------+
2 rows in set (0.00 sec)
#刪除視圖中的數據
mysql> delete from view_t2 where price=5;
Query OK, 1 row affected (0.00 sec)
#再次查看視圖的數據
mysql> select * from view_t2;
+------+-------+-------+
| qty  | price | total |
+------+-------+-------+
|    5 |    50 |   250 |
+------+-------+-------+
1 row in set (0.00 sec)
查看原表的數據
mysql> select * from t;
+----------+-------+
| quantity | price |
+----------+-------+
|        5 |    50 |
+----------+-------+
1 row in set (0.00 sec)

六、刪除視圖

mysql> drop view view_t;
相關文章
相關標籤/搜索