TCL- view

VIEW view
CREATE create
replace replace


MySQL5.1 of new features, is a virtual table (there are row has columns). And use the same common table.
Its data from the table, when executed by the dynamically generated.

Save only the SQL logic, did not save the query results.

Do a complex query, the query package to a view, the formation of a fictitious set of results.
Next time do not need to write a query directly with a view

For example: some people picked from each class, consisting of dance classes. Leadership comes, dance class performances
led away, dance classes disbanded back to class.
Dance classes is the view class is an ordinary table.

Features:

1. Temporary
2. reuse
3. simplify complex SQL operations. Do not know the details of its inquiry
4. data protection, and security. View and basic appearances, which enhances the confidentiality of information

Scenario:
used more than once repeated queries

Case #: inquiry name and surname of the student professional name
 the SELECT stuname, majorName
 the FROM stuinfo S
 the INNER  JOIN Major m
 ON s.majorid = m.id
 the WHERE s.stuName the LIKE  ' Zhang% ' ; 

the CREATE  VIEW v1
 AS 
the SELECT stuname, majorName
 the FROM stuinfo S
 the INNER  the JOIN Major m
 the ON s.majorid = m.id; 

the SELECT  * 
the FROM V1
 the WHERE stuname
 the LIKE  ' Double% ' ;

1. How to create a view

The CREATE  VIEW view name
 AS 
query
# 1 . Employee name query name contains a character name and types of information department 
# 1 . Create a view
 the CREATE  VIEW myv1
 AS 
the SELECT last_name, department_name, JOB_TITLE
 the FROM the Employees E
 JOIN the Departments d
 ON e.department_id = d.department_id
 JOIN Jobs J
 ON j.job_id = e.job_id

 

2. Using Views

SELECT *
FROM myv1
WHERE last_name LIKE '%a%'
# 2 query various departments of the average wage level 
# 1 to create a view to see the average salary for each department.
 The CREATE  VIEW myv2
 AS 
the SELECT department_id, AVG (salary)
 the FROM the Employees
 the GROUP  BY department_id; 
# 2 . View using
 the SELECT myv2.` AVG ( salary) `, g.grade_level
 the FROM myv2
 JOIN job_grades G
 ON myv2.` AVG (salary)` the BETWEEN g.lowest_sal the AND g.highest_sal; 

# 3 . lowest average wage sector inquiry information
 the SELECT  *
The FROM myv2
 the ORDER  BY myv2.` AVG (salary) `# default ascending from small to large 
LIMIT 1 ; 

# 4 . Query lowest average wage and salary department name
 the CREATE  VIEW MyV3
 AS 
the SELECT  * 
the FROM myv2
 the ORDER  BY myv2.` AVG (salary)` # default ascending ascending 
the LIMIT . 1 ; 

the SELECT D. * , m.` AVG (the salary) `
 the FROM MyV3 m
 the JOIN Departments D
 the ON m.department_id = d.department_id;

 

3. Modify the view

Method 1:
 the CREATE  or  the replace  VIEW view name
 AS 
query; 
if there is on the amendment, it does not exist create 

Second way: 
the ALTER  VIEW view name
 AS  
query;
# Way: Review MyV3
 the CREATE  OR  the REPLACE  the VIEW MyV3
 the AS 
the SELECT  the AVG (the salary), the job_id
 the FROM the Employees
 the GROUP  BY the job_id; 

the SELECT  *  the FROM MyV3 

# way: Review MyV3 
the ALTER  the VIEW MyV3
 the AS 
the SELECT  *  the FROM the Employees;

 

4. Delete view

Syntax: drop  View view, view, ....; 
rights should be enough, after the new do not know enough 
now use the root super administrator.
DROP VIEW myv1,myv2,myv3;

 

5. View view

SHOW CREATE VIEW myv3;

 

 

*********************
view Name:
How to Create:
Character Set:

 

6. Update view: the original table will be updated

CREATE OR REPLACE VIEW myv1
AS
SELECT last_name,email,salary*12*(1+IFNULL(commission_pct,0)) "annual salary"
FROM employees;

CREATE OR REPLACE VIEW myv1
AS
SELECT last_name,email
FROM employees;

SELECT * FROM myv1;

#1.插入数据
INSERT INTO myv1
VALUES('张飞','[email protected]' )
 / * 
Not Insertable-INTO not update 
* / 

# 2 . Modify
 Update myv1 the SET last_name = ' zhangwuji '  
the WHERE last_name = ' Zhang ' ; 

# 3 . Remove
 the DELETE  the FROM myv1 
 the WHERE last_name = ' zhangwuji ' ; 

# relates to the table the only data required from

 

 View to a query, rather than updating, so have the following characteristics of view does not permit update

1. The following SQL statement keyword
  grouping function, DISTINCT deduplication, group by group, the query packet having, union joint inquiry, union joint All duplicate query display
2. Constants view (select 'john' NAME; query constant alias forming a constant view)
are comprising subqueries 3.select
4.join (SQL92 comma nor) join query (update can change, can not be inserted)
5.from a view can not be updated (created when a view, from a You can not update views)
6.where clause subquery, quoted from clause table. (Query information led)

Comparison of view and table

  Creating the key sub Whether physical space occupied use
view create view No open space for the data, save only SQL logic Only query. Generally can not be used: CRUD
table create table Save data CRUD

 

Guess you like

Origin www.cnblogs.com/rijiyuelei/p/12381275.html
TCL