MySQL Crash Course #14# Chapter 22. Using Views
索引
- 視圖是啥
- 為什麽需要視圖
- 使用視圖的規則
- 如何使用視圖
- 視圖應用實例
- 別用視圖更新數據!
視圖是啥
理解視圖的最佳方式就是看下面這個例子。
SELECT cust_name, cust_contact FROM customers, orders, orderitems WHERE customers.cust_id = orders.cust_id AND orderitems.order_num = orders.order_num AND prod_id = ‘TNT2‘;
上面的請求用於檢索購買了特定產品的顧客的信息,任何想要檢索到上面數據的人都必須理解表的結構
如果可以把上面的整個請求封裝成一個 productcustomers 表,那麽只需要下面的語句就足夠檢索出所需數據了:
SELECT cust_name, cust_contact FROM productcustomers WHERE prod_id = ‘TNT2‘;
productcustomers 就是一個視圖,視圖僅僅包含請求,它本身不具備任何字段和數據,當使用視圖的時候,視圖將動態地檢索出所需要的數據。
為什麽需要視圖
You‘ve already seen one use for views. Here are some other common uses:
-
To reuse SQL statements.
-
To simplify complex SQL operations. After the query is written, it can be reused easily, without having to know the details of the underlying query itself.
-
To expose parts of a table instead of complete tables.
-
To secure data. Users can be given access to specific subsets of tables instead of to entire tables.
-
To change data formatting and representation. Views can return data formatted and presented differently from their underlying tables.
For the most part, after views are created, they can be used in the same way as tables. You can perform SELECT operations, filter and sort data, join views to other views or tables, and possibly even add and update data. (There are some restrictions on this last item. More on that in a moment.)
The important thing to remember is views are just that, views into data stored elsewhere. Views contain no data themselves, so the data they return is retrieved from other tables. When data is added or changed in those tables, the views will return that changed data.
使用視圖的規則
Here are some of the most common rules and restrictions governing view creation and usage:
-
Like tables, views must be uniquely named. (They cannot be named with the name of any other table or view).
-
There is no limit to the number of views that can be created.
-
To create views, you must have security access. This is usually granted by the database administrator.
-
Views can be nested; that is, a view may be built using a query that retrieves data from another view.
-
ORDER BY may be used in a view, but it will be overridden if ORDER BY is also used in the SELECT that retrieves data from the view.
-
Views cannot be indexed, nor can they have triggers or default values associated with them.
-
Views can be used in conjunction with tables, for example, to create a SELECT statement which joins a table and a view.
如何使用視圖
So now that you know what views are (and the rules and restrictions that govern them), let‘s look at view creation:
-
Views are created using the CREATE VIEW statement.
-
To view the statement used to create a view, use SHOW CREATE VIEW viewname;.
-
To remove a view, the DROP statement is used. The syntax is simply DROP VIEW viewname;.
-
To update a view you may use the DROP statement and then the CREATE statement again, or just use CREATE OR REPLACE VIEW, which will create it if it does not exist and replace it if it does.
視圖應用實例
格式化檢索出來的數據:
CREATE VIEW vendorlocations AS SELECT Concat(RTrim(vend_name), ‘ (‘, RTrim(vend_country), ‘)‘) AS vend_title FROM vendors ORDER BY vend_name;
過濾不需要的數據:
CREATE VIEW customeremaillist AS SELECT cust_id, cust_name, cust_email FROM customers WHERE cust_email IS NOT NULL;
簡化計算字段:
CREATE VIEW orderitemsexpanded AS SELECT order_num, prod_id, quantity, item_price, quantity*item_price AS expanded_price FROM orderitems;
別用視圖更新數據
可以,但不推薦!
MySQL Crash Course #14# Chapter 22. Using Views