Search code examples
database-designviewdatabase-schemaadvantage-database-server

Why is database view used?


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


Solution

  • 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