创建视图SQL:在SQL Server中修改视图

In my previous article, we looked at how to use the CREATE VIEW SQL statement to create views. In this one, we are moving on and focusing on how to modify views. We will continue using examples on a sample database and data created in the first write-up so in order to follow along, head over and read the Creating views in SQL Server part before starting with this one.

在上一篇文章中,我们研究了如何使用CREATE VIEW SQL语句创建视图。 在这一篇中,我们将继续关注于如何修改视图。 我们将继续在示例数据库中使用示例,并在第一次撰写中创建数据,因此,在继续学习之前,请继续阅读并在SQL Server中创建视图

介绍 (Introduction)

The primary goal will be to get familiar with the ALTER VIEW command used to modify views and change the output. A view is based on the result set from a query, and this command allows us to change the structure and definition of a query.

主要目标是熟悉用来修改视图和更改输出的ALTER VIEW命令。 视图基于查询的结果集,该命令允许我们更改查询的结构和定义。

Ironically, before modifying a view, we will create another view with a bit more complex T-SQL using aggregates in it rather than having a simple SELECT statement that is pulling everything from a table. We will not go over the syntax again since T-SQL is exactly the same as in CREATE VIEW SQL statement except the fact that instead of the CREATE reserved keyword ALTER is used.

具有讽刺意味的是,在修改视图之前,我们将使用其中的聚合来创建另一个视图,该视图使用更复杂的T-SQL,而不是使用简单的SELECT语句从表中提取所有内容。 由于T-SQL与CREATE VIEW SQL语句完全相同,因此我们不再赘述语法,只是使用了代替CREATE保留关键字ALTER的事实。

创建视图 (Creating view)

As I mentioned earlier, let’s use the code from below to create a bit more complex view:

正如我之前提到的,让我们使用下面的代码创建一个更复杂的视图:

CREATE VIEW vTop3SalesByQuantity
AS
     SELECT TOP 3 --will only return first 3 records from query
     Sales.ProductID, 
     Name AS ProductName, 
     SUM(Sales.Quantity) AS TotalQuantity
     FROM Sales
          JOIN Products ON Sales.ProductID = Products.ProductID
     GROUP BY Sales.ProductID, 
              Name
     ORDER BY SUM(Sales.Quantity) DESC;

But before we run the script, we can again just highlight the SELECT statement and see what it returns as shown below:

但是在运行脚本之前,我们可以再次突出显示SELECT语句并查看其返回结果,如下所示:

An executed CREATE VIEW SQL script showing data returned only for the SELECT statement in SSMS

Basically, what we are doing here is for each product in the Product table, we are fetching all the quantities and add them together per product. As you can see, we have our Long-Sleeve Logo Jersey product in different sizes and sold quantities. We only have four products in our table, so that’s why we’re selecting only the top three records.

基本上,我们在这里所做的是针对“产品”表中的每个产品,我们获取所有数量并将其添加到每个产品中。 如您所见,我们有不同尺寸和已售数量的长袖徽标球衣产品。 我们的表中只有四个产品,所以这就是为什么我们只选择前三个记录。

Everything looks good, so we can execute the whole CREATE VIEW SQL statement to create the view with the SELECT statement that has SUM in it which is an aggregate:

一切看起来都很不错,因此我们可以执行整个CREATE VIEW SQL语句,以使用其中包含SUM的SELECT语句创建一个视图,该视图是一个聚合:

A message in result-set saying that CREATE VIEW SQL command completed successfully and showing the newly created view in Object Explorer

The SUM is considered an aggregate because, in general, it adds the numbers together. Therefore, we also have the GROUP BY clause, followed by ORDER BY or otherwise, we’d run into some errors. In this particular case, this is what we’d get:

之所以将SUM视为汇总,是因为通常将数字加在一起。 因此,我们还有GROUP BY子句,后面跟着ORDER BY,否则我们会遇到一些错误。 在这种情况下,这是我们得到的:

Msg 8120, Level 16, State 1, Line 4
Column ‘Sales.ProductID’ is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.

消息8120,第16级,状态1,第4行
选择列表中的“ Sales.ProductID”列无效,因为该列未包含在聚合函数或GROUP BY子句中。

An executed SELECT part within the CREATE VIEW SQL statement returning an error about missing the GROUP BY CLAUSE in an aggregate function

Once created, we can query this view by saying select everything from the name of the view and we should get the same result as before:

创建视图后,我们可以通过说从视图名称中选择所有内容来查询该视图,我们应该得到与以前相同的结果:

SELECT * FROM vTop3SalesByQuantity;

An executed SELECT statement using a view in the FROM clause showing a list of products and total purchasing number

Hopefully, all this rings the bell from the first article. The point being, the complexity of a view can be as much as the complexity of the SELECT statement can get.

希望所有这些都能引起第一篇文章的钟声。 关键是,视图的复杂度可能与SELECT语句的复杂度一样多。

修改视图 (Modifying view )

Let’s move on and take a look at how we can alter views. We will take the script of the first view as an example because it has a simple SELECT statement. If you remember the CREATE VIEW SQL syntax, a view can be modified by simply using the ALTER VIEW keyword instead, and then changing the structure of the SELECT statement.

让我们继续前进,看看如何改变视图。 我们将以第一个视图的脚本为例,因为它具有一个简单的SELECT语句。 如果您还记得CREATE VIEW SQL语法,则可以通过简单地使用ALTER VIEW关键字来修改视图,然后更改SELECT语句的结构。

To get started, in SQL Server Management Studio (SSMS) we can simply right-click the view from Object Explorer, and from the context menu navigate to Script View as | ALTER To | New Query Editor Window as shown below:

首先,在SQL Server Management Studio (SSMS)中,我们只需在Object Explorer中右键单击视图,然后从上下文菜单中导航为Script View as |。 更改为 | 新建查询编辑器窗口 ,如下所示:

"Script view as" option from right-click context menu in Object Explorer in SQL Server Management Studio

SSMS will take the existing structure of the view and generate the following code in a new query editor:

SSMS将采用视图的现有结构,并在新的查询编辑器中生成以下代码:

USE [SQLShackDB]
GO
    
/****** Object:  View [dbo].[vEmployeesWithSales]    Script Date: 2/25/2020 10:49:32 PM ******/
SET ANSI_NULLS ON
GO
    
SET QUOTED_IDENTIFIER ON
GO
    
ALTER VIEW [dbo].[vEmployeesWithSales]
AS
     SELECT DISTINCT 
            Employees.*
     FROM Employees
          JOIN Sales ON Employees.EmployeeID = Sales.EmployeeID;
GO

What we are interested in is the SELECT part of the code. Selecting everything is generally a bad thing. Why? For example, let’s say that we have an application using this view and that it’s relying on a specific output AKA the signature. There could be a problem if we change the underlying table e.g. add extra columns or remove some, etc. In other words, anything we do to the underlying table when saying select everything from (Employees.*) will shine through this view:

我们感兴趣的是代码的SELECT部分​​。 选择所有内容通常是一件坏事。 为什么? 例如,假设我们有一个使用此视图的应用程序,并且它依赖于特定输出(也就是签名)。 如果我们更改基础表(例如,添加额外的列或删除某些列等),可能会出现问题。换句话说,当说从(Employees。*)中选择所有内容时,我们对基础表所做的一切都会通过此视图显示:

An automatically generated script for altering a view from Object Explorer in the query editor

Therefore, let’s change the previously created view with the CREATE VIEW SQL statement by using the ALTER VIEW statement. Note that changing the view using this command does not affect dependent stored procedures or triggers and does not change permissions.

因此,让我们通过使用ALTER VIEW语句,使用CREATE VIEW SQL语句更改以前创建的视图。 请注意,使用此命令更改视图不会影响相关的存储过程或触发器,也不会更改权限。

Previously, we generated a T-SQL script to modify our view within SSMS. This is the easiest and fastest way. But hey, if you like typing and doing it old school, just make sure that you’re connected to the appropriate database, type ALTER VIEW, followed by the name of the view, and then followed by an AS. After this, it goes the view definition:

以前,我们生成了一个T-SQL脚本来修改SSMS中的视图。 这是最简单,最快的方法。 但是,嘿,如果您喜欢打字并使用旧字体,只需确保已连接到适当的数据库,请输入ALTER VIEW,然后输入视图名称,然后输入AS。 之后,它进入视图定义:

ALTER VIEW vEmployeesWithSales
AS
    SELECT --statement that defines the view

As you can see, this is essentially the same thing as with the CREATE VIEW SQL syntax, we only change the definition of an existing view. So, let’s see what we can do with this SELECT statement and change the definition of the view to eliminate some potential problems.

如您所见,这与CREATE VIEW SQL语法基本相同,我们只更改现有视图的定义。 因此,让我们来看看如何使用此SELECT语句并更改视图的定义以消除一些潜在的问题。

For the purpose of this example, we can consider the code from below as one solution:

就本示例而言,我们可以将下面的代码视为一种解决方案:

ALTER VIEW vEmployeesWithSales
AS
     SELECT DISTINCT 
            Employees.EmployeeID, 
            FirstName, 
            LastName
     FROM Employees
          INNER JOIN Sales ON Employees.EmployeeID = Sales.EmployeeID;
GO

Before we run the script, let’s go through the SELECT part to see what we changed. Instead of fetching all columns from the Employees table, we are returning just three columns:

在运行脚本之前,让我们遍历SELECT部分​​以查看所做的更改。 与其从Employees表中获取所有列,我们仅返回三列:

  • Employees.EmployeeID

    员工编号
  • FirstName

    名字
  • LastName

Notice that the EmployeeID column is fully qualified because EmployeeID exists in both tables that we are referencing. On the other hand, FirstName and LastName only exist in the Employees table, so we don’t need to fully qualify those.

请注意, EmployeeID列是完全合格的,因为在我们引用的两个表中都存在EmployeeID 。 另一方面, FirstNameLastName仅存在于Employees表中,因此我们不需要完全限定它们。

This is a very common mistake, and that’s why it’s always a good idea to run and check only the SELECT part in a batch to see what it returns. Simply remove the alias from the EmployeeID column and execute the SELECT part as shown below:

这是一个非常常见的错误,因此为什么总是运行并只检查批处理中的SELECT部件以查看其返回值始终是一个好主意。 只需从EmployeeID列中删除别名并执行SELECT部分​​,如下所示:

Msg 209, Level 16, State 1, Line 4
Ambiguous column name ‘EmployeeID’.

消息209,第16级,州1,第4行
列名称“ EmployeeID”不明确。

An executed ALTER VIEW SQL script showing error message about ambiguous column name returned only for the SELECT statement in SSMS
  • Note: Now, that I’ve mentioned batches, bear in mind that CREATE VIEW SQL must be the only statement in a batch or the first statement in a query batch or you might get an error from SQL Server注意:现在,我已经提到了批处理,请记住,CREATE VIEW SQL必须是批处理中的唯一语句或查询批处理中的第一条语句,否则您可能会从SQL Server中得到错误消息

In our case, you’ll see the error message “Ambiguous column name ‘EmployeeID’”. This is the SQL Server way of saying that we have referenced more than one column with the same name in the FROM clause.

在我们的情况下,您将看到错误消息“模棱两可的列名'EmployeeID'”。 这是SQL Server的一种说法,即我们在FROM子句中引用了多个具有相同名称的列。

If you remember the view’s definition, views are pretty much just virtual tables. So, if we head over to Object Explorer, expand the Views folder, then vEmployeesWithSales, and then the Columns folder, we should see the following:

如果您还记得视图的定义,则视图几乎只是虚拟表。 因此,如果我们转到Object Explorer ,依次展开Views文件夹, vEmployeesWithSalesColumns文件夹,我们应该看到以下内容:

Object Explorer showing the difference between the list of columns in a table and a view

When we initially created this view using the CREATE VIEW SQL statement, we specified that all columns from the Employees table should be retrieved. However, now rather than eight columns we only fetch three.

最初使用CREATE VIEW SQL语句创建此视图时,我们指定应检索Employees表中的所有列。 但是,现在我们只需要获取三列,而不是八列。

结论 (Conclusion)

In this part of learning the CREATE VIEW SQL statement, we learned how to use the ALTER VIEW command to modify an existing view and change the output. I promised more in the first part, but rather than making this a long and boring article, we’ll continue our journey in the next one. We haven’t even touched how to use the DLM language for inserting data through a view, so that’s is what we’ll cover in the next part of the series.

在学习CREATE VIEW SQL语句的这一部分中,我们学习了如何使用ALTER VIEW命令来修改现有视图并更改输出。 我在第一部分中承诺过更多,但与其在篇幅冗长而无聊的文章中介绍,不如在下一篇文章中继续我们的旅程。 我们甚至还没有涉及如何使用DLM语言通过视图插入数据,因此这就是我们在本系列下一部分中将要介绍的内容。

I hope this article has been informative for you and I thank you for reading it. Stay tuned for the next one…

希望本文对您有所帮助,也谢谢您阅读。 请继续关注下一个…

目录 (Table of contents)

CREATE VIEW SQL: Creating views in SQL Server
CREATE VIEW SQL: Modifying views in SQL Server
CREATE VIEW SQL: Inserting data through views in SQL Server
CREATE VIEW SQL: Working with indexed views in SQL Server
创建视图SQL:在SQL Server中创建视图
创建视图SQL:在SQL Server中修改视图
CREATE VIEW SQL:通过SQL Server中的视图插入数据
CREATE VIEW SQL:在SQL Server中使用索引视图

翻译自: https://www.sqlshack.com/create-view-sql-modifying-views-in-sql-server/

neruolib全脑建模计算框架 neurolib 是一个用python编写的全脑建模计算框架,也是可扩展的、允许轻松实现自定义神经质量模型,能够在介观尺度上表示脑区平均活动。 阅读详情

相关推荐

MOSFET雪崩特性参数解析

但是,一些电源在输出短路时,初级中会产生较大的电流,加上初级电感,器件就会有雪崩损坏的可能,因此在这样的应用条件下,就要考虑器件的雪崩能量。对于那些在MOSFET的D和S极产生较大电压的尖峰应用,就要考虑器件的雪崩能量,电压的尖峰所集中的能量主要由电感和电流所决定,因此对于反激的应用,MOSFET关断时会产生较大的电压尖峰。雪崩击穿能量标定了器件可以容忍的瞬时过冲电压的安全值,其依赖于雪崩击穿需要消散的能量。EAS单脉冲雪崩击穿能量, EAS标定了器件可以安全吸收反向雪崩击穿能量的高低。

Airer_00的博客 774

数据库 SQL Server 视图 创建视图 查询视图 修改视图 删除视图

文章目录1 视图概述2 创建视图3 修改视图4 删除视图 1 视图概述 视图就是将一个或多个表中的目标字段抽取出来形成的一个虚拟表。这个虚拟表和真实的表具有相同的功能。 2 创建视图 语法格式: create view view_name as select 字段 from tb1 where 条件 create view v_id_name as select departid,name from files 注: files表的建表语句,如下: create table files( userid

qq_740785701的博客 1万+

2025上海市五级行政区划SHP数据-省市县乡镇-村界矢量数据下载

2025上海市五级行政区划SHP数据-省、市、县、乡镇(街道)、社区(村界)矢量数据下载

SQL数据库语言基础之SqlServer视图创建修改视图数据的增删改查

一个视图是从一个特定的角度来查看数据库中的数据 。从数据库系统内部来看,一个视图是由SELECT语句组成的查询定义的虚拟表 。从数据库系统内部来看,视图是由一张或多张表中的数据组成的,从数据库系统外部来看,视图就如同一张表 一样,对表能够进行的一般操作都可以应用于视图

Viewinfinitely的博客 9903

SQLServer修改视图

修改视图注意事项 修改先前创建视图。 其中包括索引视图。 ALTER VIEW不影响相关的存储过程或触发器,并且不会更改权限。 如果原来的视图定义是使用 WITH ENCRYPTION 或 CHECK OPTION创建的,则只有在 ALTER VIEW 中也包含这些选项时,才会启用这些选项。 如果当前所用的视图使用 ALTER VIEW...

weixin_34248849的博客 4333

SQL Server 数据库视图

MySQL 数据库视图 视图的增删查改操作介绍

程序员小白的博客 9111

Sql Server数据库视图创建修改

1 if OBJECT_ID('Sales.USACusts') is not null 2 drop view Sales.USACusts; 3 go 4 5 create view Sales.USACusts 6 as 7 select custid,companyname,contactname,contacttitle,address,city,...

weixin_30507269的博客 398

SQL Server视图操作大全(创建视图修改视图、通过视图改数据)

目录 一、创建视图 方法一:图形化方式进行创建(如图) 方法二:SQL查询语句的方式创建视图(如图) 二、修改和删除视图 方法一:图形化方式——直接在界面上进行拖动操作,删除更加简单,单击鼠标右键直接删除就好。(如图) 方法二:SQL语句方式 三、通过视图修改数据 一、创建视图 一个数据库中有很多很多的表,我们每次不可能将它们全部查看一遍,有了视图以后我们就方便了很多,可以...

王华春_Jason 4万+

sqlsever2019:SQL视图创建修改

视图的操作

全栈川川 9638

sql server中如何修改视图中的数据?

那么,我们将上面创建视图,重新定义一下(0 as FlagState)把这个0改成数据表中存在的字段。数据库视图的数据如果想要修改,必须保证标记字段是持久化存在的(得有这个字段),才能修改;我做个测试,视图的数据有标记字段时,如果是这种方式(0 as FlagState),是无法修改的。假如,我们要修改视图的数据(33921,33922)的状态为1。--修改视图数据的语法----创建视图语句 -----查询视图的数据-----创建视图语句 -----删除视图的语法--

chentiebo的博客 1200

修改 sqlserver视图

视图修改完不用保存,直接执行一下就好了,

weixin_41589232的博客 509

sql server 视图修改的日志 (全)

随着数据量和复杂性的增加,未来的 SQL Server 版本可能会进一步改进视图修改的日志记录机制,提供更高效和可靠的数据恢复和审计功能。SQL Server 是一种关系型数据库管理系统,提供了视图(View)的功能,视图是基于一个或多个表的查询结果集,可以简化复杂的查询操作。事务日志采用了写前日志(Write-Ahead Logging)的原则,即在修改数据之前,先将相关的日志记录写入日志文件。SQL Server 视图修改的日志是通过事务日志记录的,它提供了数据恢复、数据审计和数据一致性等功能。

走向CTO的路上... 1445

sql-server视图

(2)因为视图来自表,所以在表中删除了视图中使用了的列(这时候没问题,因为视图不影响表),在查看视图时会报错,即便查看的仅是没删除的列。(1)当添加一个列时,因为这个列不在视图创建时要求的列中(即便创建时使用了*,也只表示那个时候的全部,并不包括这个列),是不会对视图有影响的。但是,如果你使用*的初衷不是为了只使用当前表中全部的列,还希望当表增加新的列时视图也能增加,这时候就需要去更新视图了。因为视图是基于表的,这相当于使用存储过程来刷新了视图。这个时候刷新视图,查询就正确了,删除的列也没有了。

weixin_47363690的博客 2900

SQL注入问题 修改SQL语句 视图 触发器 事物 存储过程 函数 流程控制 索引

【代码】SQL注入问题 修改SQL语句 视图 触发器 事物 存储过程 函数 流程控制 索引。

Lamb的博客 353

SQL语句创建修改视图的方法

5.向电子05的学生视图中添加一条记录,其中学号为0596,姓名为赵亦,性别为男,专业班级为电子05,出生日期为1986-6-8(除了电子05的学生视图发生变化之外,看看学生表中发生了什么变化?6.将电子05的学生视图中赵亦的性别改为“女”(除了电子05的学生视图发生变化之外,看看学生表中发生了什么变化?2.创建一个生物05的学生作业情况视图(包括学号、姓名、课程名、作业1成绩、作业2成绩、作业3成绩)。3.创建一个学生作业平均成绩视图(包括学号、作业1平均成绩、作业2平均成绩、作业3平均成绩)。

m0_61741424的博客 8588

SQL sever中的视图

SQL sever中的视图

m0_71406734的博客 8058

SQL进阶:视图的增删改

讲述了视图的增删改

一只不知疲倦的学习猿 733

SQL SERVER(29)修改视图

使用SQL语句的ALTER VIEW可以修改视图: ALTER VIEW 视图名称[字段1,字段2,…] AS SELECT查询语句 [WITH CHECK OPTION] 修改视图vwA.使其能够查询"邓小平理论"考试成绩大于等于95的学生的学号、姓名、所属院系和考试成绩。 ALTER VIEW vwA AS SELECT st.sno, st.sname, st. depart, s.exam FROM stu_into AS st

复行数十步 973

SQL第7篇 视图

视图 1视图.png 1.创建视图 create view 视图名 as sql查询语句 2.修改视图 法一:create or replace 视图名 as 法二:alter view 视图名 as 3.删除视图 drop view 视图1,视图2,... ps:删除视图要有权限,root用户有所有权限,哈哈 4.查看视图结构 法一:desc 视图名 法二:show create view 视图名 ps:法二用cmd命令行看,比较全,...

weixin_44990801的博客 1633

SQL语句——视图

SQL语句 视图的概述 试图(view)是数据库中的一个对象,它是数据库管理系统提供给用户的以多种角度观察数据库中数据的一种重要机制。它对应三种模式中的外模式。 在SQL中,试图是基于SQL语句的结果集的可视化的表。 视图包含行和列,就像一个真实的表。视图中的字段就是来自一个或多个数据库中的真实的表中的字段。但视图与基本表不同,试图是一个虚表。数据库中只存储视图的定义,而不存储视图所包含的数据,这些数据仍存放在原来的基本表中。这种模式有两个好处: 视图数据始终与基本表数据保持一致,当基本表发生变化时,从视

HMT的博客 7059
上一篇: 复杂sql 查询编写方法_学习SQL:如何编写复杂的SELECT查询
下一篇: ssis 转换中文字符乱码_SSIS软件包中的字符映射转换
culuo4781
博客等级 码龄10年 311粉丝 0原创
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值