什么时候以及为什么有些人决定他们需要在他们的数据库中创建一个视图?为什么不运行一个普通的存储过程或选择?
当前回答
我通常创建视图来反规范化和/或聚合经常用于报告目的的数据。
EDIT
By way of elaboration, if I were to have a database in which some of the entities were person, company, role, owner type, order, order detail, address and phone, where the person table stored both employees and contacts and the address and phone tables stored phone numbers for both persons and companies, and the development team were tasked with generating reports (or making reporting data accessible to non-developers) such as sales by employee, or sales by customer, or sales by region, sales by month, customers by state, etc I would create a set of views that de-normalized the relationships between the database entities so that a more integrated view (no pun intended) of the real world entities was available. Some of the benefits could include:
减少编写查询时的冗余 建立关联实体的标准 提供机会 评估和最大化绩效 用于复杂的计算和连接 (例如在Schemabound视图上建立索引 在该软件中) 让数据更容易获取 对于团队成员和非开发人员来说是直观的。
其他回答
关注特定的数据 视图允许用户专注于他们感兴趣的特定数据和他们负责的特定任务。不必要的数据可以从视图中删除。这还提高了数据的安全性,因为用户只能看到视图中定义的数据,而不能看到底层表中的数据。有关将视图用于安全目的的更多信息,请参见将视图用作安全机制。
To Simplify Data Manipulation Views can simplify how users manipulate data. You can define frequently used joins, projections, UNION queries, and SELECT queries as views so that users do not have to specify all the conditions and qualifications each time an additional operation is performed on that data. For example, a complex query that is used for reporting purposes and performs subqueries, outer joins, and aggregation to retrieve data from a group of tables can be created as a view. The view simplifies access to the data because the underlying query does not have to be written or submitted each time the report is generated; the view is queried instead. For more information about manipulating data.
还可以创建逻辑上作为参数化视图操作的内联用户定义函数,或者在where子句搜索条件中具有参数的视图。有关更多信息,请参见内联用户定义函数。
自定义数据 视图允许不同的用户以不同的方式查看数据,即使他们同时使用相同的数据。当具有许多不同兴趣和技能水平的用户共享同一个数据库时,这是特别有利的。例如,可以创建一个视图,该视图仅检索与客户经理打交道的客户的数据。该视图可以根据使用该视图的帐户经理的登录ID确定检索哪些数据。
To Export and Import Data Views can be used to export data to other applications. For example, you may want to use the stores and sales tables in the pubs database to analyze sales data using Microsoft® Excel. To do this, you can create a view based on the stores and sales tables. You can then use the bcp utility to export the data defined by the view. Data can also be imported into certain views from data files using the bcp utility or BULK INSERT statement providing that rows can be inserted into the view using the INSERT statement. For more information about the restrictions for copying data into views, see INSERT. For more information about using the bcp utility and BULK INSERT statement to copy data to and from a view, see Copying To or From a View.
To Combine Partitioned Data The Transact-SQL UNION set operator can be used within a view to combine the results of two or more queries from separate tables into a single result set. This appears to the user as a single table called a partitioned view. For example, if one table contains sales data for Washington, and another table contains sales data for California, a view could be created from the UNION of those tables. The view represents the sales data for both regions. To use partitioned views, you create several identical tables, specifying a constraint to determine the range of data that can be added to each table. The view is then created using these base tables. When the view is queried, SQL Server automatically determines which tables are affected by the query and references only those tables. For example, if a query specifies that only sales data for the state of Washington is required, SQL Server reads only the table containing the Washington sales data; no other tables are accessed.
Partitioned views can be based on data from multiple heterogeneous sources, such as remote servers, not just tables in the same database. For example, to combine data from different remote servers each of which stores data for a different region of your organization, you can create distributed queries that retrieve data from each data source, and then create a view based on those distributed queries. Any queries read only data from the tables on the remote servers that contains the data requested by the query; the other servers referenced by the distributed queries in the view are not accessed.
When you partition data across multiple tables or multiple servers, queries accessing only a fraction of the data can run faster because there is less data to scan. If the tables are located on different servers, or on a computer with multiple processors, each table involved in the query can also be scanned in parallel, thereby improving query performance. Additionally, maintenance tasks, such as rebuilding indexes or backing up a table, can execute more quickly. By using a partitioned view, the data still appears as a single table and can be queried as such without having to reference the correct underlying table manually.
如果满足以下条件之一,分区视图是可更新的: 在视图上定义了一个INSTEAD OF触发器,其逻辑支持INSERT、UPDATE和DELETE语句。
视图和INSERT、UPDATE和DELETE语句都遵循为可更新分区视图定义的规则。有关更多信息,请参见创建分区视图。
https://technet.microsoft.com/en-us/library/aa214282 (v = sql.80) . aspx # sql:加入
一般来说,我使用视图是为了让生活更简单,从存储在多个表中的实体中获得扩展细节(消除代码中的大量连接以增强可读性),有时还可以在多个数据库中共享数据,甚至可以使插入更容易阅读。
我认为第一个。隐藏查询的复杂性。它非常适合于视图。当我们规范化数据库表时如何增加。现在,当表数量增加时,获取数据是非常困难的。所以最好的处理方法是遵循视图。如果我错了,请纠正我。
在我看来,存储过程更多的是一种可以针对数据调用的方法,而视图提供了一种机制来创建基础数据的合成版本,可以根据它创建查询或存储过程。当简化或聚合有意义时,我将创建一个视图。当我想提供一个非常特定的服务时,我将编写一个存储过程。
我通常创建视图来反规范化和/或聚合经常用于报告目的的数据。
EDIT
By way of elaboration, if I were to have a database in which some of the entities were person, company, role, owner type, order, order detail, address and phone, where the person table stored both employees and contacts and the address and phone tables stored phone numbers for both persons and companies, and the development team were tasked with generating reports (or making reporting data accessible to non-developers) such as sales by employee, or sales by customer, or sales by region, sales by month, customers by state, etc I would create a set of views that de-normalized the relationships between the database entities so that a more integrated view (no pun intended) of the real world entities was available. Some of the benefits could include:
减少编写查询时的冗余 建立关联实体的标准 提供机会 评估和最大化绩效 用于复杂的计算和连接 (例如在Schemabound视图上建立索引 在该软件中) 让数据更容易获取 对于团队成员和非开发人员来说是直观的。
推荐文章
- NVL和Coalesce之间的Oracle差异
- 在SQL server查询中将NULL替换为0
- 在SQL中修改表的模式名
- 如何得到累计和
- 如何在SQL Server 2005的一条语句中更新两个表?
- 如何创建临时表与SELECT * INTO tempTable从CTE查询
- 用于查找计数为>的记录的SQL查询
- “从Table1左连接Table2”和“从Table2右连接Table1”可以互换吗?
- 在SQL Server的选择语句中使用带TOP的变量,而不是动态的
- SQL变量保存整数列表
- 自然连接和内部连接的区别
- MySQL现在()+1天
- 在SQL中转换月号到月名函数
- 不可重复读和幻影读的区别是什么?
- 改变一个varchar列的最大长度?