什么时候以及为什么有些人决定他们需要在他们的数据库中创建一个视图?为什么不运行一个普通的存储过程或选择?
当前回答
我的生产数据库中只有10个左右的视图。我用了几个我一直在用的列。我使用的一组来自7个表,一些具有外部连接,而不是不断重写,我只需要在一个选择中调用该视图,并使一个或2个连接。对我来说,这只是节省时间。
其他回答
下面是如何使用视图以及权限来限制用户可以在表中更新的列。
/* This creates the view, limiting user to only 2 columns from MyTestTable */
CREATE VIEW dbo.myTESTview
WITH SCHEMABINDING AS
SELECT ID, Quantity FROM dbo.MyTestTable;
/* This uses the view to execute an update on the table MyTestTable */
UPDATE dbo.myTESTview
SET Quantity = 7
WHERE ID = 1
以下是两个常见的原因:
你可以用它来保证安全。在主表上不授予权限,创建限制列或行访问的视图,并向用户授予查看视图的权限。
你可以为了方便使用它。将视图中经常使用的一些表连接在一起。这可以使查询保持一致并更容易。
我们创建view来限制或严格访问表中的所有行/列。如果所有者希望只有特定或有限的行/列需要共享,那么他将使用这些列创建一个视图。
我通常创建视图来反规范化和/或聚合经常用于报告目的的数据。
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视图上建立索引 在该软件中) 让数据更容易获取 对于团队成员和非开发人员来说是直观的。
视图是查询的封装。转换为视图的查询往往比较复杂,因此将它们保存为视图以供重用是有好处的。
推荐文章
- 如何在Ruby On Rails中使用NuoDB手动执行SQL命令
- 查询JSON类型内的数组元素
- 确定记录是否存在的最快方法
- 获得PostgreSQL数据库中当前连接数的正确查询
- 在SQL选择语句Order By 1的目的是什么?
- MySQL数据库表中的最大记录数
- 从现有模式生成表关系图(SQL Server)
- 我如何循环通过一组记录在SQL Server?
- HyperLogLog算法是如何工作的?
- 数据库和模式的区别
- 如何在SQL Server中一次更改多个列
- 如何从命令行通过mysql运行一个查询?
- 外键约束可能导致循环或多条级联路径?
- 使用LIMIT/OFFSET运行查询,还可以获得总行数
- 当恢复sql时,psql无效命令\N