Why is database view used?

后端 未结 4 1987
[愿得一人]
[愿得一人] 2021-01-04 07:25

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

4条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-04 08:21

    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

提交回复
热议问题