2.1 view
Upcoming SlideShare
Loading in...5

Like this? Share it with your network


2.1 view






Total Views
Views on SlideShare
Embed Views



0 Embeds 0

No embeds



Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
Post Comment
Edit your comment

2.1 view Presentation Transcript

  • 1. Hệ quản trị cơ sở dữ liệu Khung nhìn (View) Dư Phương Hạnh Bộ môn Hệ thống thông tin Khoa CNTT, trường Đại học Công nghệ Đại học Quốc gia Hanoi hanhdp@vnu.edu.vn
  • 2. Outline     Introduction Create View Updatable View Manage View Tài liệu tham khảo: http://www.mysqltutorial.org/mysql-views-tutorial.a 2 Hệ quản trị CSDL @ BM HTTT
  • 3. Introduction  By definition, a database view is a virtual or logical table which is composed of result set of a SELECT query.  You can retrieve and update data on it in the same way with table.  View is dynamic because it is not related to the physical schema. It is stored as view definition as SELECT statements.  When the tables which are the source data of a view changes; the data in the view change also. 3 Hệ quản trị CSDL @ BM HTTT
  • 4. Introduction  Two kinds: 1. Virtual = not stored in the database; just a query for constructing the relation. 2. Materialized = actually constructed and stored. – Problem: each time a base table changes, the materialized view may change. • Cannot afford to recompute the view with each change. – 4 Solution: Periodic reconstruction of the materialized view, which is otherwise “out of date.” Hệ quản trị CSDL @ BM HTTT
  • 5. Giới thiệu  Advantages: – – – – – 5 Simplify complex query Limited access data to the specific users Provide extra security Computed column Backward compatibility Hệ quản trị CSDL @ BM HTTT
  • 6. MySQL View  MySQL supports views from version 5.x and almost views’ features conform to the SQL: 2003 standard. – MySQL creates a temporary table based on the query which defined the view and then execute the input query on this table. – MySQL first combines the input query and query which defined the view, then MySQL executes this query.  MySQL also allows you to create views of views.  If the cached is enabled, the query against view is stored in the cache  increases the performance of query by pulling data from the buffering instead of querying data from physical hard disk. 6 Hệ quản trị CSDL @ BM HTTT
  • 7. Create View CREATE VIEW [database name].[view name] AS [SELECT statement] In SELECT statement: – – – – – you can query any tables or views existed in the db. Subquery cannot be included Any variables cannot be used The prepared statement cannot be used in the view. Temporary tables or views cannot be used; any tables or views which referred by views must exists. – View cannot be associated with triggers. 7 Hệ quản trị CSDL @ BM HTTT
  • 8. Examples CREATE VIEW SalePerOrder AS SELECT orderNumber, SUM (quantityOrdered * priceEach) total FROM orderDetails GROUP BY orderNumber ORDER BY total DESC 8 Hệ quản trị CSDL @ BM HTTT
  • 9. Examples CREATE VIEW vwProducts AS SELECT productCode, productName, buyPrice FROM products WHERE buyPrice > ( SELECT AVG (buyPrice) FROM products) ORDER BY buyPrice DESC 9 Hệ quản trị CSDL @ BM HTTT
  • 10. Updatable View  The SELECT statement which defines View has to follow several following rules: – – – – SELECT statement must not reference to more than one table. SELECT statement must not use GROUP BY or HAVING clause. SELECT statement must not use DISTINCT in the selection list. SELECT statement must not reference to the view that is not updateable – SELECT statement must not contain any expression (aggregates, functions, computed columns…) 10 Hệ quản trị CSDL @ BM HTTT
  • 11. Updatable View examples CREATE VIEW officeInfo AS SELECT officeCode, phone, city FROM offices SELECT * FROM officeInfo UPDATE officeInfo SET phone = '+33 14 723 5555' WHERE officeCode = 4 11 Hệ quản trị CSDL @ BM HTTT
  • 12. Managing View  Displaying a View – SHOW CREATE VIEW <view_name>  Modifying a View – ALTER VIEW <view_name> AS <sql_statement>  Removing a View – DROP VIEW [IF EXISTS] <view_name> 12 Hệ quản trị CSDL @ BM HTTT