Hi Daniel,
Think of a view as the result of a SQL SELECT statement. It's a set of date which the SELECT generates. The data set itself is not stored permanently, but the defining SELECT is.
Let's suppose your database contains a customer table, a related invoice table, and a related invoice details table. You frequently need to work with a list of the current month's invoices, including the names and addresses of the relevant customers. Apart from those customer details, you only need to see the invoice ID, the number of invoice lines, and the total amount.
You could get the required data by running a SELECT, which would join the three tables and filter the invoices on the date (to show only the current month). The SELECT would send the data to a cursor, which you can then use more-or-less just like a table.
The problem is that, if you needed that data in more than place, you would have to code and run the SELECT multiple times. A view saves you the trouble of doing that. In effect, you are storing the SELECT itself (that is, the view definition) in the database, and having it run automatically each time you need it.
Each time you open the view, you are getting the most up-to-date version of the data. After that, you can refresh the data at any time to bring it up to date again.
The advantages are:
- You can treat the view almost exactly like a table, so you don't need to learn any new code (or not much).
- Once you've created the view, you don't need to code the SELECT again.
- You can also
parameterise a view, which means that you don't need to re-create it if, say, the filter condition changes.
Views are also useful if you want to restrict access to certain items of information by specific users. To continue with the above example, suppose that some of the customer details are confidential; a user needs to generate an invoice report, but is not allowed to see those confidential fields. If you didn't have a view, the user would need full access to the customer table, and therefore would see the fields in question. With a view, you can simply omit the relevant fields, and that particular user would never see them. (This is an over-simplification; in practice, it's much harder to hind information that's stored in a Foxpro database, but a view lets you do that up to a point.)
Finally, a view makes it easier to update the base tables. If you were using your view to populate a form, the user would be able to edit the view directly, and you can arrange for those edits to work their way back to the underlying tables. That's harder to do if you were using a cursor generated by a SELECT to populate the form. (Again, this is an over-simplification.)
So, that in a nutshell is why you might want to use a view. No doubt other people here will give you other reasons for and against.
(By the way, I assume you are asking about
local views, but most of what I have said here applies equally to
remote views, that is, views on a back-end database like SQL Server.)
Mike
__________________________________
Mike Lewis (Edinburgh, Scotland)
Visual FoxPro articles, tips and downloads