Note -7: mysql view

1. Overview view

 

2. Create a view

The CREATE  [ OR REPLACE ]  the VIEW view_name [ (the column_list) ] 
the AS select_statement
 [ the WITH CASCADED {|} the LOCAL the CHECK the OPTION ] 

# OR  the REPLACE : Optional, OR REPLACE clause specifies the statement to replace the same name already exists in the database view, but you need to have DROP permission on the view. 
# View_name: Specifies the view name. View name must be unique in the database. 
# Column_list: specify explicit names for each column. 
# SELECT_statement: Specifies the SELECT statement to create a view. 
# The WITH  CHECK  OPTION : optional, used to specify the modifications carried out on updatable views need to comply with the limits specified in the select_statement.
# Created in the database view v_student, requires that the customer information table tb_student view contains information about all the boys, and to ensure the future of the modified view of the data must comply with the conditions of the students were male gender.
Create  or  Replace  the VIEW v_student AS  SELECT  *  from tb_student WHERE Sex = ' M '  the WITH  the CHECK  the OPTION ;
# Create a view v_score_avgs, requires a view that contains the table tb_score all students learn numbers and grade point average, according to student number studentNo sort.
Create  or  Replace  the VIEW v_score_avgs AS  SELECT studentNo, AVG (Score) from tb_score Group  by studentNo;
# Using the WITH CHECK OPTION clause to create a view v_score, the view contains all the required score table tb_score in < student number 90, course number, grade information.
Create  the VIEW v_score AS  SELECT  *  from tb_score WHERE Score < 90  the WITH  the CHECK  the OPTION ;     

3. Delete View

# Grammatical structure:
 drop  VIEW  [ IF EXISTS ] view_name;
# 删除视图v_score、v_score_local、v_score_cascaded
drop VIEW if exists v_score,v_score_local,v_score_cascaded;

4. Modify the view definition

alter VIEW view_name [(column_list)] as SELECT_statement [WITH [CASCADED | LOCAL] CHECK OPTION];
# Use the ALTER VIEW statement to modify the definition of v_student view, the view contains the requirements for future modifications tb_student gender 'male', for the nation 'Han' student number, name, belongs to the class, and the view of the data required to ensure that all students must comply with gender as 'male', for the nation 'Han' this condition.
ALTER  the VIEW v_student (studentNo, studentName, classno) AS  SELECT studentNo, studentName, classno from tb_student WHERE Sex = ' M '  and   Nation = ' Chinese '  the WITH  the CHECK  the OPTION ;

5. Review the view definition

# Grammatical structures: 
SHOW the CREATE  VIEW view_name;
# View the definition of a database v_student 
Show the Create  VIEW v_student;

6. Update the view data

6.1 insert statements are inserted into the data base table through a view

# Insert a record in a view v_student :( ' 2014310109 ' , ' Zhou ' , ' M ' , ' 1997-08-16 ' , ' Hubei ' , ' Chinese ' , ' IS1401 ' )
 INSERT  INTO v_student values ( ' 2014310109 ' , ' Zhou ' , ' M ' , ' 1997-08-16 ' , 'Hubei ' ,'','IS1401');

select * from v_student;

6.2 by update statements modify the base view table data

# V_student the view of native students in all columns is updated to "Hubei"
 Update v_student the SET native = ' Hubei ' ;

6.3 by using the delete statement to delete the base table of the view data

# Delete the names to view v_student ' Zhou ' student information
 the Delete  from v_student the WHERE studentName = ' Zhou Ming ' ; 

# For more dependent on the underlying table view, you can use the DELETE statement

When the view contains any of the following SQL statement structure, the view is not updated.

  • Aggregate function
  • DISTINCT keyword
  • CROUP BY clause
  • ORDER BY clause
  • HAVING clause
  • UNION operator
  • Located in the sub-query select list
  • The FROM clause including a plurality of numbers
  • SELECT statement quoted nonrenewable view
  • WHERE clause of the subquery, references the FROM clause table

7. Use the view need to pay attention

  • Create a view must have sufficient access rights
  • There is no limit to the number of views can be created
  • View can be nested
  • View can not be indexed, not associated triggers, default values
  • order by clause in the view may be used. However, if the select clause retrieve data from this view also have an order by clause, it will be overwritten

 

Guess you like

Origin www.cnblogs.com/Cyzhouke/p/11470428.html