当前位置:网站首页>MySQL view

MySQL view

2022-06-22 07:55:00 yolo2016

What is a view

  1. A view is a virtual table , Its content is defined by query .
  2. Like a real watch , The view contains a series of named column and row data .
  3. The row and column data comes from the tables referenced by the query that defines the view , And dynamically generate... When referencing views .
  4. Simply put, the view is made up of select Table of results .

Features of the view

  1. A view is a reference to several tables , A virtual table , Query the result of statement execution .
  2. No specific data is stored ( The basic table data has changed , The view changes as well );
  3. It can be the same as the basic table , Add, delete, modify and check ( There are conditional restrictions on adding, deleting, and modifying queries )

The function of view

  1. Security : Create a view , Define the data operated by the view . Then bind the user permissions to the view . In this way, a feature is used ,grant Statement can grant permission to a view .
  2. Improved query performance
  3. Improved data independence .

View operation

// Create view 
CREATE VIEW v1 AS (SELECT * from employees WHERE salary >10000)
select * from v1
// Delete view 
drop view v2

// Modify the view 
CREATE  or REPLACE VIEW v1 AS (SELECT * from employees WHERE salary >10000)

// View view structure 
select * from v1
desc v1
show CREATE view v1


// View Syntax  CREATE [ALGORITHM]={UNDEFINED| MERGE| TEMPTABLE }] VIEW  View name [( Attribute list )]
// AS SELECT  sentence  [WITH [CASCADED|LOCAL] CHECK OPTION];
// MERGE  Treatment mode replacement , You can update the data in the real table 
// TEMPTABLE  Specific formula , Because the data is stored in temporary tables , Therefore, data updating is not allowed 
// UNDEFINED  No definition ALGORITHM Parameters ,mysql Prefer to choose the replacement method , Because it's more efficient 
// WITH CHECK OPTION  When updating data, you cannot insert or update records that do not meet the restrictions you are trying to limit 
// CASCADED|LOCAL  Is an optional parameter , Determines the scope of inspection and testing , The default is CASCADED


CREATE ALGORITHM=UNDEFINED 
DEFINER=`root`@`%` 
SQL SECURITY DEFINER VIEW `v1` 
AS 
(select `employees`.`employee_id` AS `employee_id`,
`employees`.`first_name` AS `first_name`,
`employees`.`last_name` AS `last_name`,`employees`.`email` AS `email`,
`employees`.`phone_number` AS `phone_number`,`employees`.`job_id` AS `job_id`,
`employees`.`salary` AS `salary`,
`employees`.`commission_pct` AS `commission_pct`,
`employees`.`manager_id` AS `manager_id`,
`employees`.`department_id` AS `department_id`,
`employees`.`hiredate` AS `hiredate` 
from `employees` 
where (`employees`.`salary` > 10000))

Try a non updatable part

 Insert picture description here
As long as the data in the view is not from the base table , It cannot be directly modified .

原网站

版权声明
本文为[yolo2016]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202220531362492.html