What is a good reason to use SQL views?
Solution 1:
Another use that none of the previous answers seem to have mentioned is easier deployment of table structure changes.
Say, you wish to retire a table (T_OLD
) containing data for active users, and instead use a new table with similar data (named T_NEW
) but one that has data for both active and inactive users, with one extra column active
.
If your system(s) have gazillion queries that do SELECT whatever FROM T_OLD WHERE whatever
, you have two choices for the roll-out:
1) Cold Turkey - Change the DB, and at the same time, change, test and release numerous pieces of code which contained said query. VERY hard to do (or even coordinate), very risky. Bad.
2) Gradual - change the DB by creating the T_NEW
table, dropping the T_OLD
table and instead creating a VIEW called T_OLD
that mimics the T_OLD
table 100% (e.g the view query is SELECT all_fields_except_active FROM T_NEW WHERE active=1
).
That would allow you to avoid releasing ANY code that currently selects from T_OLD
, and do the changes to migrate code from T_OLD
to T_NEW
at leisure.
This is a simple example, there are others a lot more involved.
P.S. On the other hand, you probably should have had a stored procedure API instead of direct queries from T_OLD
, but that's not always the case.
Solution 2:
(Copied from the first tutorial that came up in a Google search (link now dead), but it has all of the benefits I would have typed manually myself.)
Views have the following benefits:
- Security - Views can be made accessible to users while the underlying tables are not directly accessible. This allows the DBA to give users only the data they need, while protecting other data in the same table.
- Simplicity - Views can be used to hide and reuse complex queries.
- Column Name Simplication or Clarification - Views can be used to provide aliases on column names to make them more memorable and/or meaningful.
- Stepping Stone - Views can provide a stepping stone in a "multi-level" query. For example, you could create a view of a query that counted the number of sales each salesperson had made. You could then query that view to group the sales people by the number of sales they had made.