当前位置:网站首页>SQL Server 2016 learning records - View
SQL Server 2016 learning records - View
2022-07-28 10:23:00 【Hard work Zhang Zhang】
Learning goals :
1、 Concept of view
2、 Define views
3、 Query view
4、 Update the view
5、 The function of view
Learning content :
1、 Concept of view
Views are virtual tables , From one or more basic tables ( Or view ) Exported table
Only store the definition of the view , Do not store the data corresponding to the view
The data in the base table changes , The data queried from the view also changes
2、 Define views
Create view
Sentence format
CREATE VIEW
< View name > [(< Name > [,< Name >]…)]
AS < Subquery >
[WITH CHECK OPTION];
Subqueries can be arbitrary SELECT sentence ;
The attribute column names that make up the view : Omit all or specify all
-- Set up a view of information students .
CREATE VIEW IS_Student
AS
SELECT Sno,Sname,Sage
FROM Student
WHERE Sdept= 'IS';
-- Set up a view of information students , It is also required to ensure that the view is only for information department students when modifying and inserting operations .
CREATE VIEW IS_Student
AS
SELECT Sno,Sname,Sage
FROM Student
WHERE Sdept= 'IS'
WITH CHECK OPTION;
If a view is exported from a single base table , And just remove some rows and columns of the basic table , But the main code is retained , We call this kind of view row column subset view .
- View based on multiple base tables
-- Set up the Department of information 1 Student's view of Lesson No ( Including student ID 、 full name 、 achievement ).
CREATE VIEW IS_S1(Sno,Sname,Grade)
AS
SELECT Student.Sno,Sname,Grade
FROM Student,SC
WHERE Sdept= 'IS' AND
Student.Sno=SC.Sno AND
SC.Cno= '1';
- View based views
-- Set up the Department of information 1 And the grade is in 90 Views of students with a score above .
CREATE VIEW IS_S2
AS
SELECT Sno,Sname,Grade
FROM IS_S1
WHERE Grade>=90;
- Views with expressions
-- Define a view that reflects the year the student was born .
CREATE VIEW BT_S(Sno,Sname,Sbirth)
AS
SELECT Sno,Sname,2014-Sage
FROM Student;
- Group view
-- Define the student number and grade average as a view
CREATE VIEW S_G(Sno,Gavg)
AS
SELECT Sno,AVG(Grade)
FROM SC
GROUP BY Sno;
Delete view
Format of statement :
DROP VIEW < View name >[CASCADE];
This statement deletes the specified view definition from the data dictionary
If there are other views exported on this view , Use CASCADE Cascade delete statements , Delete this view along with all views exported from it
Error deleting base table , All view definitions exported from this base table must explicitly use DROP VIEW Statement delete
-- Delete view BT_S and IS_S1
DROP VIEW BT_S; /* Successful execution */
DROP VIEW IS_S1; /* Refuse to enforce */
/* To delete IS_S1, Cascade deletion is required :DROP VIEW IS_S1 CASCADE; */
3、 Query view
In fact, the query view is similar to the query table
The method of realizing view query in relational database management system : View resolution method (View Resolution)
- Check the effectiveness
- Convert to an equivalent query to the basic table
- Execute the modified query
-- In the view of information students, find out that they are younger than 20 Year old student .
SELECT Sno,Sage
FROM IS_Student
WHERE Sage<20;
4、 Update the view
-- View information department students IS_Student Middle school number ”201215122” The student's name was changed to ” Liu Chen ”.
UPDATE IS_Student
SET Sname= ' Liu Chen '
WHERE Sno= ' 201215122 ';
-- To the information department students view IS_S Insert a new student record in , The student number is ”201215129”, The name is ” Zhao Xin ”, Age is 20 year
INSERT
INTO IS_Student
VALUES('201215129',' Zhao Xin ',20);
Add data to the view , Is to add data to the master and apprentice's base table , Adding data also uses INSERT command , Be careful :
- When trying to have multiple base tables , You cannot modify multiple base tables in a view at the same time in one statement , That is, you can only add, delete or modify data in one base table of the view at a time
- Because the view may only contain some columns of the base table , therefore , When inserting data into the base table through the view , It is required that other non view columns in the base table should be allowed to be empty or contain default values
- The inserted data must meet the constraints of the base table
5、 The function of view
- View can simplify the operation of users
- Views enable users to view the same data from multiple perspectives
- Views provide a certain degree of logical independence for refactoring databases
- View can provide security for confidential data
- The proper use of views can express queries more clearly
边栏推荐
- 中芯国际科创板IPO顺利过会,市值有望突破2000亿!
- Record a parent-child project in idea, modify the name of project and module, and test it personally!
- SQL Server 2016 学习记录 --- 集合查询
- Aqua Data Studio 18.5.0 export insert statement
- 剑指offer
- Aqua Data Studio 18.5.0导出insert语句
- SQL Server 2016 学习记录 --- 数据更新
- 上下文变量值(context values)陷阱及在 Go 中如何避免或缓和这些陷阱
- 谈谈基于JS实现阻止别人调试通过控制台调试网站的问题
- 11. Linked list inversion
猜你喜欢

Typora tutorial

IDEA创建我的第一个项目

Aqua Data Studio 18.5.0导出insert语句

语音聊天app——如何规范开发流程?

11. Linked list inversion

6、双指针——递增数组两数之和与目标数相等

14. Double pointer - the container that holds the most water

Digital transformation scheme of real estate: all-round digital intelligence system operation, helping real estate enterprises improve the effectiveness of management and control

定了!就在7月30日!

5. Dynamic programming -- Fibonacci series
随机推荐
按位与、或、异或等运算方法
What are the highlights of B2B2C system? How to help jewelry enterprises build an omni channel multi merchant mall management system
B2B e-commerce website scheme for building materials industry: enable the transformation and upgrading of building materials enterprises to achieve cost reduction and efficiency improvement
7. Dichotomy -- find a set of repeated or ordered but rotating arrays
简介
定了!就在7月30日!
Install MySQL under centos7, and the online articles are not accurate
初识SuperMap iDesktop
16、字符串反转
Sleeping barber problem
ogg里用多个filter语法应该怎么写?
Choosing a supplier service system is the first step for large health industry enterprises to move towards digital transformation
It's settled! On July 30!
漏洞分析丨HEVD-0x8.IntegerOverflow[win7x86]
9、删除链表中节点
中兴通讯总裁徐子阳:5nm芯片将在2021年推出
15、判断二维数组中是否存在目标值
Summary of key points of bank entry examination
Typora使用教程
【微信小程序】项目实战—抽签应用