当前位置:网站首页>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
边栏推荐
- 什么样的知识付费系统功能,更有利于平台与讲师发展?
- It's settled! On July 30!
- Small knowledge in Oracle
- 记录一次idea中的父子项目修改project与module名称,亲测!
- Prometheus operation and maintenance tool promtool (IV) TSDB function
- 中兴通讯总裁徐子阳:5nm芯片将在2021年推出
- 海量数据TopN问题
- Install MySQL under centos7, and the online articles are not accurate
- Redis设计规范
- 5. Dynamic programming -- Fibonacci series
猜你喜欢

gcc: error trying to exec 'as': execvp: No such file or directory

Guangzhou metro line 14 xinshixu station is under construction, and residents in Baiyun District are about to start a double line transfer mode!

Performance test of API gateway APIs IX in Google cloud T2a and T2D

什么样的知识付费系统功能,更有利于平台与讲师发展?

SQL Server 2016学习记录 --- 连接查询

Multithreading and high concurrency (III) -- source code analysis AQS principle

Skillfully use NGX_ Lua makes traffic grouping

数据库安全 --- 创建登录名 用户+配置权限【笔记】

SQL Server 2016 学习记录 --- 集合查询

SuperMap iServer发布管理以及调用地图服务
随机推荐
巧用ngx_lua做流量分组
centos7下安装mysql,网上文章都不太准
华为入股石墨烯材料厂商富烯科技,持股10%
多线程与高并发(三)—— 源码解析 AQS 原理
Consul
[wechat applet] project practice - lottery application
Hurun released the 2020 top 10 Chinese chip design private enterprises: Huawei Hisilicon did not appear on the list!
ogg里用多个filter语法应该怎么写?
a different object with the same identifier value was already associated with the session
leetcode——旋转数组的最小数字
IE兼容性问题处理
用K-means聚类分类不同行业的关税模型
Get to know SuperMap idesktop for the first time
6. Double pointer -- the sum of the two numbers of the incremental array is equal to the target number
Digital transformation scheme of real estate: all-round digital intelligence system operation, helping real estate enterprises improve the effectiveness of management and control
传全球半导体设备巨头或将于上海建合资工厂!
Problem summary file
Huawei takes a 10% stake in fullerene technology, a graphene material manufacturer
leetcode076——数组中的第 k 大的数字
11. Linked list inversion