当前位置:网站首页>I learned database at station B (10): View
I learned database at station B (10): View
2022-06-12 20:00:00 【4:00 a.m. code】
Last one : I am here b Station learning database ( Nine ): function
One 、 Introduce
1、 View (view) It's a virtual table , It's not real , Its essence is based on SQL Statement to get a dynamic data set , And name it , When using, users only need to use the view name to obtain the result set , And you can use it as a table .
2、 In the database Only the definition of the view is stored , There is no data stored in view . The data is stored in the original table .
3、 When using views to query data , The database system will take the corresponding data from the original table . therefore , The data in the view depends on the data in the original table . Once the data in the table changes , The data displayed in the view will also change .
Two 、 effect
Security reasons , If there are a lot of data in a table , A lot of information doesn't want everyone to see , You can use the view view , Such as : Social security fund table , You can use the view to display only names , Address , Instead of showing the social security number and the number of wages , It can be applied to different users , Set different views .
3、 ... and 、 Create view
1、 grammar :
create [or replace] [algorithm = {undefined | merge | temptable}]
view view_name [(column_list)]
as select_statement
[with [cascaded | local] check option]
Parameter description :
(1)algorithm: optional , Algorithm for view selection .
(2)view_name : Represents the name of the view to be created .
(3)column_list: optional , Specifies the noun for each attribute in the view , By default, it is the same as SELECT The properties of the query in the statement are the same .
(4)select_statement
: Represents a complete query statement , Import the query record into the view .
(5)[with [cascaded | local] check option]: optional , It means that when updating a view, it must be within the permission range of the view .
2、 Data preparation
Create three tables in one database
dept surface ( department )
emp surface ( staff )
salgrade surface ( Pay scales )
3、 example
create or replace view view1_emp
as
select ename,job from emp;
-- View tables and views
show full tables;
Four 、 Modify the view
1、MySQL Pass through CREATE OR REPLACE VIEW Statement and ALTER VIEW Statement to modify the view .
2、 Format
alter view View name as select sentence
3、 example
alter view view1_emp
as
select a.deptno,a.dname,a.loc,b.ename,b.sal from dept a, emp b where a.deptno = b.deptno;
5、 ... and 、 Update the view
1、 If the view contains any of the following structures , So it's not updatable :
Aggregate functions (SUM(), MIN(), MAX(), COUNT() etc. )
DISTINCT
GROUP BY
HAVING
UNION or UNION ALL
Subqueries in the selection list
JOIN
FROM Non updatable view in Clause
WHERE Subquery in Clause , quote FROM Table in clause .
Reference text values only ( In this case , There are no basic tables to update )
Be careful : Although the data can be updated in the view , But there are a lot of restrictions . In general , It's best to use the view as a virtual table for querying data , Instead of updating data through views . because , When using views to update data , If you don't fully consider the limitations of updating data in the view , Data update may fail .
2、 example
-- --------- Update the view -------
create or replace view view1_emp
as
select ename,job from emp;
update view1_emp set ename = ' Zhou Yu ' where ename = ' Lu Su '; -- You can modify
insert into view1_emp values(' king of Wu in the Three Kingdoms Era ',' Clerk '); -- You can't insert
# Be careful : Update and insert data , It's all true emp Table operation
-- ---------- The view contains aggregate functions that are not updatable --------------
create or replace view view2_emp
as
select count(*) cnt from emp;
insert into view2_emp values(100); # Report errors
update view2_emp set cnt = 100; # Report errors
-- ---------- The view contains distinct Not updatable ---------
create or replace view view3_emp
as
select distinct job from emp;
insert into view3_emp values(' financial ');
-- ---------- The view contains goup by 、having Not updatable ------------------
create or replace view view4_emp
as
select deptno ,count(*) cnt from emp group by deptno having cnt > 2;
insert into view4_emp values(30,100);
-- ---------------- The view contains union perhaps union all Not updatable ----------------
create or replace view view5_emp
as
select empno,ename from emp where empno <= 1005
union
select empno,ename from emp where empno > 1005;
insert into view5_emp values(1015,' Trinket ');
-- ------------------- The view contains subqueries that are not updatable --------------------
create or replace view view6_emp
as
select empno,ename,sal from emp where sal = (select max(sal) from emp);
insert into view6_emp values(1015,' Trinket ',30000);
-- ---------------------- The view contains join Not updatable -----------------
create or replace view view7_emp
as
select dname,ename,sal from emp a join dept b on a.deptno = b.deptno;
insert into view7_emp(dname,ename,sal) values(' The administration department ',' Trinket ',30000);
-- -------------------- The view contains constant text values that are not updatable -----------------
create or replace view view8_emp
as
select ' The administration department ' dname,' Yang2 guo4 ' ename;
insert into view8_emp values(' The administration department ',' Trinket ');
6、 ... and 、 Rename view
1、 Format
rename table View name to New view name ;
2、 example
rename table view1_emp to my_view1
7、 ... and 、 Delete view
1、 Format
drop view View name [, View name …];
2、 example
drop view if exists view_student;
Be careful : When deleting a view , You can only delete the definition of a view , Data will not be deleted .
边栏推荐
- System log
- Since using low code development, the development efficiency has been increased by 10 times
- PostgreSQL database replication - background first-class citizen process walreceiver PG_ stat_ wal_ Receiver view
- 基于微信电子书阅读小程序毕业设计毕设作品(3)后台功能
- Demand and business model innovation - demand 3- demand engineering process
- 基于微信电子书阅读小程序毕业设计毕设作品(4)开题报告
- exec函数、shell的实现
- system()
- MySQL - the execution order of an SQL statement
- Is it really hopeless to choose electronic engineering and be discouraged?
猜你喜欢

用户权限和组权限

Equipment management - borrowing and returning module 1

负数取余问题

Low code enables rural construction to open "smart mode"

How to close icloud when Apple ID of Apple mobile phone forgets password and frequently jumps out to log in

Demand and business model innovation-4-strategy

Demand and business model innovation - demand 2- demand basis

The component style set by uniapp takes effect in H5 and app, but does not take effect in wechat applet. The problem is solved

Wechat e-book reading applet graduation design completion works (3) background function

What is a federated index?
随机推荐
Macro definitions and functions
PostgreSQL数据库复制——后台一等公民进程WalReceiver pg_stat_wal_receiver视图
Detailed explanation of IO flow basic knowledge -- file and IO flow principle
Unsupported class file major version 60
Kyma application connectivity feature introduction
94. analyze the content in the web page
Ctfshow-web265 (deserialization)
系统 日志
The Milvus graphical management tool Attu is coming!
How to make a computer installation and startup USB flash disk
The difference between MySQL full table scanning and indexing
Module 8 fonctionnement
Golang type assertion understanding [go language Bible]
Demand and business model analysis-1-business model canvas
synchronized下的 i+=2 和 i++ i++执行结果居然不一样
Parameter meaning of random forest randomforestclassifier in sklearn
WordPress station group tutorial automatic collection of pseudo original release tutorial
Wechat e-book reading applet graduation design work (6) opening defense ppt
【splishsplash】自定义导出器
sklearn中随机森林RandomForestClassifier的参数含义