Why do you create a View in a database?

前端 未结 25 1874
一个人的身影
一个人的身影 2020-11-28 17:10

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?

相关标签:
25条回答
  • 2020-11-28 17:34

    Generally i go with views to make life easier, get extended details from some entity that's stored over multiple tables (eliminate lots of joins in code to enhance readability) and sometimes to share data over multiple databases or even to make inserts easier to read.

    0 讨论(0)
  • 2020-11-28 17:35

    Several reasons: If you have complicated joins, it is sometimes best to have a view so that any access will always have the joins correct and the developers don;t have to remember all the tables they might need. Typically this might be for a financial application where it would be extremely important that all financial reports are based on the same set of data.

    If you have users you want to limit the records they can ever see, you can use a view, give them access only to the view not the underlying tables and then query the view

    Crystal reports seems to prefer to use views to stored procs, so people who do a lot of report writing tend to use a lot of views

    Views are also very useful when refactoring databases. You can often hide the change so that the old code doesn't see it by creating a view. Read on refactoring databases to see how this work as it is a very powerful way to refactor.

    0 讨论(0)
  • 2020-11-28 17:36

    When I want to see a snapshot of a table(s), and/or view (in a read-only way)

    0 讨论(0)
  • 2020-11-28 17:36

    Views also break down very complex configuration and tables into managable chunks that are easily queried against. In our database, our entire table managment system is broken down into views from one large table.

    0 讨论(0)
  • 2020-11-28 17:37

    Here is how to use a View along with permissions to limit the columns a user can update in the table.

    /* This creates the view, limiting user to only 2 columns from MyTestTable */
    CREATE VIEW dbo.myTESTview 
    WITH SCHEMABINDING AS
    SELECT ID, Quantity FROM dbo.MyTestTable;
    
    /* This uses the view to execute an update on the table MyTestTable */
    UPDATE dbo.myTESTview
    SET Quantity = 7
    WHERE ID = 1
    
    0 讨论(0)
  • 2020-11-28 17:38

    Think of it as refactoring your database schema.

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