为什么要在数据库中创建视图? [英] Why do you create a View in a database?

查看:33
本文介绍了为什么要在数据库中创建视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

何时以及为什么有人决定他们需要在他们的数据库中创建一个视图?为什么不直接运行一个普通的存储过程或选择?

When and Why does some one decide that they need to create a View in their database? Why not just run a normal stored procedure or select?

推荐答案

视图提供多种好处.

1.视图可以隐藏复杂性

如果您的查询需要连接多个表,或者具有复杂的逻辑或计算,您可以将所有逻辑编码到一个视图中,然后像选择表一样从视图中进行选择.

If you have a query that requires joining several tables, or has complex logic or calculations, you can code all that logic into a view, then select from the view just like you would a table.

2.视图可以用作安全机制

视图可以从一个(或多个)表中选择某些列和/或行,并在视图上而不是基础表上设置权限.这允许仅显示用户需要查看的数据.

A view can select certain columns and/or rows from a table (or tables), and permissions set on the view instead of the underlying tables. This allows surfacing only the data that a user needs to see.

3.视图可以简化支持遗留代码

如果你需要重构一个会破坏很多代码的表,你可以用一个同名的视图替换这个表.该视图提供与原始表完全相同的架构,而实际架构已更改.这可以防止引用该表的遗留代码被破坏,让您可以随意更改遗留代码.

If you need to refactor a table that would break a lot of code, you can replace the table with a view of the same name. The view provides the exact same schema as the original table, while the actual schema has changed. This keeps the legacy code that references the table from breaking, allowing you to change the legacy code at your leisure.

这些只是视图如何有用的众多示例中的一部分.

These are just some of the many examples of how views can be useful.

这篇关于为什么要在数据库中创建视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆