When to use a View instead of a Table?

前端 未结 8 1246
没有蜡笔的小新
没有蜡笔的小新 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
    0 讨论(0)
  • 2020-12-04 07:31

    According to Wikipedia,

    Views can provide many advantages over tables:

    • Views can represent a subset of the data contained in a table.
    • Views can limit the degree of exposure of the underlying tables to the outer world: a given user may have permission to query the view, while denied access to the rest of the base table.

    • Views can join and simplify multiple tables into a single virtual table.

    • Views can act as aggregated tables, where the database engine aggregates data (sum, average, etc.) and presents the calculated results as part of the data.

    • Views can hide the complexity of data. For example, a view could appear as Sales2000 or Sales2001, transparently partitioning the actual underlying table.

    • Views take very little space to store; the database contains only the definition of a view, not a copy of all the data that it presents.

    • Views can provide extra security, depending on the SQL engine used.

    0 讨论(0)
提交回复
热议问题