为什么使用数据库视图?

发布于 2024-08-25 06:49:51 字数 49 浏览 4 评论 0原文

在数据库设计中使用“视图”是正确的方法还是我们应该在代码端处理它?有什么优点或缺点?

Is using "view" in db design right method or we should handle it code side? What are the advantages or disadvantages?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

留蓝 2024-09-01 06:49:51

我看到使用视图的几个原因:

  • 提供一个更简单的界面:只需查询视图,而不是十几个表,进行连接等
  • 提供一个不会改变的界面(或更少)
    • 即使您更改表的结构,您也可以修改视图,使其仍然返回相同的内容
    • 这意味着您的应用程序代码无需更改:它仍然可以工作,因为它使用视图,而不是直接访问表
  • 只提供表的某些字段的接口
    • 用户无需查看一些他们不会使用的数据
    • 或者访问一些他们不应该使用的数据
  • 使用某些数据库引擎(我认为 MS SQL Server 支持),某些类型视图可以有索引
    • 这对于性能来说是一件好事:如果您有一些复杂的查询,请将其存储为视图,并在该视图上定义所需的索引

I see a couple of reasons to use views :

  • Provide a simpler interface : just query the view, and not a dozen tables, doing joins and all
  • Provide an interface that doesnt change (or less often) :
    • Even if you change the structure of the tables, you might be able to modify your view so it still returns the same thing
    • Which means no change is needed in your application's code : it'll still work, as it's using the view, and not directly accessing the tables
  • Only provide an interface to some fields of the tables
    • No need for the users to see some data they won't use
    • Or to access some data they should not use
  • With some database engines (I think MS SQL Server supports that), some type of views can have indexes
    • Which is a good thing for performances : if you have some complex query, store it as a view, and define the required indexes on that view
梦巷 2024-09-01 06:49:51

在我们的案例中,视图的两个典型场景是:

  • 表中的某些列包含只能由少数人看到的机密数据。您可以创建一个排除这些列的视图,并为大多数用户使用该视图。
  • 您将两个或多个表连接到非规范化视图中,该视图对于报告目的很实用,但作为数据库中存储的表没有意义。

希望这有帮助。

Two typical scenarios for views in our case are:

  • Some columns in a table contain confidential data that should be only seen by a few people. You can create a view that excludes those columns and use that view for most users.
  • You join two or more tables into a denormalised view that is practical for reporting purposes but would not make sense as a table for storage in the database.

Hope this helps.

厌倦 2024-09-01 06:49:51

视情况而定。我有时会使用它们,但不经常使用。不过,它们对于展示数据的解码视图以供最终用户(工具)使用非常有用,例如报告应用程序。通过这种方式,您可以向最终用户提供经常请求的信息的简化版本,隐藏一些技术细节。

Depends. I use them sometmies, but not that often. They are VERY usefull to expsoe decoded views on the data for use by end user (tools), like reporting applications, though. This way you can provide an end user with a simplified version of often requested information hiding some technical details.

浮云落日 2024-09-01 06:49:51

某些数据库系统不支持在 FROM 子句中嵌入 SELECT 语句。如果您使用的系统不支持此功能,则通常可以将内部 SELECT 语句保存为视图,并使用视图名称代替 select 语句。

因此它提供了某些数据库实现中可能缺少的行为。

Some database systems do not support embedding a SELECT statement inside a FROM clause. If you are using a system that does not support this feature, you can often save the inner SELECT statement as a view, and use the view name in place of the select statement.

So it provides behavior that can be missing in some db implementations.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文