When to use a View instead of a Table?

前端 未结 8 1244
没有蜡笔的小新
没有蜡笔的小新 2020-12-04 07:02

When should a View actually be used over an actual Table? What gains should I expect this to produce?

Overall, what are the advantages of using a view over a table?

8条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-04 07:26

    Oh there are many differences you will need to consider

    Views for selection:

    1. Views provide abstraction over tables. You can add/remove fields easily in a view without modifying your underlying schema
    2. Views can model complex joins easily.
    3. Views can hide database-specific stuff from you. E.g. if you need to do some checks using Oracles SYS_CONTEXT function or many other things
    4. You can easily manage your GRANTS directly on views, rather than the actual tables. It's easier to manage if you know a certain user may only access a view.
    5. Views can help you with backwards compatibility. You can change the underlying schema, but the views can hide those facts from a certain client.

    Views for insertion/updates:

    1. You can handle security issues with views by using such functionality as Oracle's "WITH CHECK OPTION" clause directly in the view

    Drawbacks

    1. You lose information about relations (primary keys, foreign keys)
    2. It's not obvious whether you will be able to insert/update a view, because the view hides its underlying joins from you

提交回复
热议问题