mysql-- view - concept

Second, the view 


view is a virtual table, is lead out from the database table in one or more tables. 

It may also be defined from the view based on the view already exists. 

The definition stored in the database view only, and not stored in the data view, the data stored in the original table. 

Query using the view data corresponding to the database will be extracted from the original data table, therefore, view data is dependent on the original table. 

Once the data in the original table is changed, the data is displayed in the view may also change. 



Effect of view: A view is a virtual table based on the original table or view on redefined, so that the user can select the useful information from the original table. 


1 , create a view 

views can be built on a table, you can also create multiple tables in the 

syntax: 
           the Create  [ algorithm = {undefined | Merge | the TempTable} ] 

                  View view name [ (property list) ] 

                  AS  the SELECT statement 

                  [ with Cascaded {|} loocal the Check the Option ] ; 


algorithm is an optional parameter that indicates the algorithm selected view 



view name parameter represents the name of the view needs to be created;

The list of attributes is an optional parameter, which specifies the view in terms of each attribute; By default, the same properties as the select statement in the query; 

select statement argument is a complete query, representing a query from a table that meet recording condition, and introduced into the view of these records; 

with  Check Option is an optional parameter, it indicates when the view is updated to ensure that within the purview of this view; 



Cascaded is an optional parameter, indicates when the view is updated to meet all relevant conditions of view and table, the default parameters; 

local view showing updates, view itself as long as the conditions can be defined; 


( 1 ) create a view on a single table in 

example 1: Create  view department_view1 aS  SELECT  *  from Department 

query table structure department_view1 view: desc department_view1; 

Note: properties and department table view department_view1 table is exactly the same as in the case do not specify a custom view of the list of attributes, 

      the same property name property name and select statement query view. 


Example 2: Create  View department_view2 (name, fuction, LOCATION)AS  SELECT d_name, fuction, address from Department; 

view department_view2 name attribute columns are: name, fuction, location. Because when you create a view, you specify a list of attributes. 

View property name and property name the same attribute list. 



( 2 ) create a view on a multi-table 


example: Create algorithm = Merge View worker_view1 (name, Department, Sex, Age, address) 

      AS  SELECT name, department.d_name, Sex, 2009 - Birthday, address 

      from worker, Department WHERE worker. D_ID = departmnet.d_id 

      with local Check  Option ; 




2 , see view 

(1 ) DESCRIBE statement to see the view basic information about 

syntax: describe the view name; can be abbreviated as: desc view name; 

This statement can be simple to understand simple information view various fields 


( 2 ) Show the Table Status statement to see the view basic information about 

syntax : Show Table Status like  ' view name ' ; 

like a string which matches the rear; 

view name refers to the name of the view parameters need to see, it is necessary to cause the single quotation marks; 



( . 3 ) Show Create View View View Details statement 

syntax : Show the Create  View View name; 


( 4 ) in the table view views views details 

in MYSQL, all defined views are stored in views under the table information_schema database. 

Query views, you can check the detailed information in the database of all views. 

Syntax: the SELECT  *  from INFORMATION_SCHEMA.VIEWS; 







. 3 , modify the view 

edit view is a defined modifying already in the database table. 

When some of the fields of the base table changes, consistency can be maintained between the base table and the view by modifying the view; 

( . 1 ) Create  or  Replace View View modified statement 

syntax: 
           Create  or  Replace  [ algorithm undefined = {| Merge | } TempTable ] 

                      view view name [ (attribute list) ] 

                      AS  SELECT statement 

                     [ with Cascaded {|} Check loocal Option ] ; 



( 2 ) modify the view ALTER statement 


syntax: 
           ALTER  [ algorithm undefined = {| Merge |} TempTable] 

                  View view name [ (property list) ] 

                  AS  the SELECT statement 

                  [ with Cascaded {|} loocal the Check the Option ] ; 


4 , update the view 

********************** ***************************** 


5 , delete view 


remove the view refers to the view definition that already exists in the database to delete; delete view to remove such a definition of the view, does not delete data 

syntax: drop  view  [ IF eXISTS ] view name in the list; 

IF eXISTS parameter refers to determine whether there is a view, if there is executed or not executed, 

view the list parameter indicates the name to be deleted name of the list view, between views name separated by a comma;

Guess you like

Origin www.cnblogs.com/xiaobaibailongma/p/12093548.html