当前位置:网站首页>[mysql] database - View
[mysql] database - View
2022-06-10 03:44:00 【boy-s6】
What is a view
- The view displays data from one or more tables in a customized manner
- A view is a database object ( have access to create Keyword create view ), The user can query the view like a normal table
- No data is actually stored in the view , It's just a query to the table ( What is stored in the view is SQL sentence , Not the result . The reason is because , When the previous SQL When the statement is modified , If the view is stored, execute the results ( The result will not change ), Then the result will be after the change SQL Statements will conflict )
- The definition of the view is saved in the data dictionary , The table on which the view is created is called “ Base watch ”
Why you need a view
If you often need to perform join queries on two tables , You have to connect the tables every time , Write the same string of statements , At the same time, some information may be sensitive and do not want to be seen by ordinary users . In this case, we can solve the problem through the view .
The role of view is somewhat
effect :
- Control security
- Save query data
somewhat :
- Provides flexible and consistent levels of security
- Hide the complexity of the data
- Simplify the user's SQL Instructions
- By means of rebranding , Provide data from another perspective
Environmental preparation :
# Create department table
CREATE TABLE dept(
did INT PRIMARY KEY AUTO_INCREMENT,
dname VARCHAR(20)
);
# Create an employee table
CREATE TABLE emp (
id INT PRIMARY KEY AUTO_INCREMENT,
NAME VARCHAR(10),
gender CHAR(1), -- Gender
salary DOUBLE, -- Wages
join_date DATE, -- Date of entry
dep_id INT,
FOREIGN KEY (dep_id) REFERENCES dept(did) -- Foreign keys , Related department table ( The primary key of the Department table )
);
-- Add Department data
INSERT INTO dept (dNAME) VALUES (' R & D department '),(' The Marketing Department '),(' Finance Department '),(' The sales department ');
-- Add employee data
INSERT INTO emp(NAME,gender,salary,join_date,dep_id) VALUES
(' The Monkey King ',' male ',7200,'2013-02-24',1),
(' Pig eight quit ',' male ',3600,'2010-12-02',2),
(' Tang's monk ',' male ',9000,'2008-08-08',2),
(' Bones jing ',' Woman ',5000,'2015-10-07',3),
(' Spider essence ',' Woman ',4500,'2011-03-14',1),
(' Small white dragon ',' male ',2500,'2011-02-14',null);Create view
The essence of view is SQL Instructions (select sentence ), Basic grammar :
CREATE VIEW View name AS SQL sentence ;When we just want to show the employee's name and department to ordinary users , When other information is hidden, we can write this :
select
emp.name Employee's sex name , dept.d_name Department
from
emp,dept
where
emp.dep_id = dept.d_id; Execution results : 
Analyze the view above , I believe you have a certain understanding of the view . A view is a virtual table , When we want to turn the query results of the above two tables into one table , Then I can use the view , The statement is as follows
create view v_emp_dept
as
(select emp.name Employee's sex name , dept.d_name Department from emp,dept where emp.dep_id = dept.d_id);Use view
The view is a virtual table , You can directly think of the view as ” surface ” operation , But the view itself has no data , Is temporary select Statement to get the corresponding result , Views are mainly used for query operations . Basic grammar
select Field list from View name [ Clause ];Inquire about v_emp_dept View
SELECT * FROM v_emp_dept; -- Single table query The execution result is :
Rules for using views
- Views must have unique names
- stay MySQL There is no limit to the number of views in
- To create a view, you must obtain the necessary permissions from the administrator
- Views support nesting , That is to say, a new view can be created by using the data retrieved from other views
- It can be used in the view OREDR BY, However, if the sort clause is already used in the view , Then the view's ORDER BY Will overwrite the previous ORDER BY
- View cannot be indexed , You cannot associate triggers or default values ( A view is not essentially a table )
- Views can be used with tables
Modify the view
Use CREATE OR REPLACE VIEW Statement modification v_emp_dept View . Specify a column name for each column .
create
or replace
view v_emp_dept
as
(select emp.id, emp.name Employee's sex name , dept.d_name Department from emp,dept where emp.dep_id = dept.d_id);-- Query view
SELECT * FROM v_emp_dept; Execution results :
stay CREATE VIEW The fields in the statement must correspond to the fields in the subquery one by one , Otherwise, don't specify an alias , Or specify an alias to use in the subquery ALTER VIEW Statement modification EMP_V_10 View . Specify a column name for each column .
alter view v_emp_dept
( Number , Wages , full name , department )
as (select emp.id, emp.salary, emp.name , dept.d_name
from emp,dept
where
emp.dep_id = dept.d_id);-- Query view
SELECT * FROM v_emp_dept; Execution results :
Be careful : It is not recommended to add, delete or modify the view data . In case it has to be revised , What essentially changes is the structure of the table
Delete view
Deleting views does not result in data loss , Because a view is a query definition based on a database table .
DROP VIEW View name ;边栏推荐
猜你喜欢

C# 11 新特性:列表模式匹配

c语言刷题系列(三)

If else syntax

答辩前电脑坏了......

ACL 2022 | the latest hot research in NLP field, you must not miss it!

What are the assessment units of Shaanxi Xi'an insurance? Where can I find it?

【TFLite, ONNX, CoreML, TensorRT Export】

Microsoft build release - 7 major directions of technical updates concerned by developers
![[yolov3 loss function]](/img/79/87bcc408758403cf3993acc015381a.png)
[yolov3 loss function]

Refactoring technique --replace conditional with polymer
随机推荐
重构--消除重复代码
Easyexcel realizes dynamic import and export
【比特熊故事汇】X Microsoft Build 2022——微软专家+MVP,技术亮点全解析
Talk about 10 tips to ensure thread safety
新功能官宣丨PlayFab让跨平台游戏架构如此简单
01_知乎40个精辟的回复
Open source framework support for range mode
Twitter agreed to open the database for musk to check
纯js 实现图片压缩,并返回file图片信息
01_ Know about 40 brilliant replies
[actual combat] Application Based on chromedriver and crawler related
Switch case syntax
Machine learning & Content Security & overseas risk control companies
C language question brushing series (II)
Ptrtostructure error prompt: this structure must not be a value class. Solution
先序遍历二叉树
Redis core technology and practice - practice reading notes 20 ~ end
Vulnhub's harripot: Fawkes
[calculation method]
重构--Introduce Parameter Object