20.07.2013 Views

Beginning SQL

Beginning SQL

Beginning SQL

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 10<br />

Now that you know what views allow the DBA to do, the following list shows you how views can be<br />

enormously useful and provide a variety of benefits to the DBA.<br />

❑ Views are often used to provide security, limiting results sets to exactly the fields and records<br />

needed by a particular user. This security allows the DBA to set up results sets for users that<br />

prevent them from seeing data intended for other users or that allow them to see sensitive data<br />

that other users are not allowed to see.<br />

❑ Views can provide the basis for other views or queries. Once a results set is defined, you can<br />

manipulate that base data set using column functions such as Sum(), Avg(), Count(), Max(),<br />

and Min(). You can also sort or filter base data in another query or view. Views can thus be<br />

built up, layer after layer, each layer performing some specific function to transform the base<br />

data in some way.<br />

❑ Views can provide personalization of data, turning a bewildering array of normalized tables<br />

into one or more views of the data familiar to a user.<br />

❑ Views can rename fields so that users with different backgrounds can view data in familiar terms.<br />

❑ Because a view is just a saved query, they can often be used interchangeably with queries.<br />

Although they provide a variety of benefits, views also impose some limitations on what you can do<br />

with the data sets they create, and they may create performance problems.<br />

❑ Views are nothing more than queries saved with a query name. As such, if you base your query<br />

on a view, the view name must be looked up and the <strong>SQL</strong> statement retrieved and then executed.<br />

Creating views based on views based on views can eventually cause performance problems.<br />

❑ Each view can cause its own restrictions in updating data. As you build up views based on<br />

views, the results may not allow you to update the data. It may not matter for a given purpose,<br />

but you must keep this in mind when your intent is to create views that allow updates, and you<br />

should always test extensively.<br />

❑ Used improperly, views may allow users to add or delete records that are not part of the view,<br />

causing data integrity and security issues.<br />

Creating Views<br />

288<br />

A view is often called a virtual table because the view has the appearance of a table, but the data set is<br />

not physically stored in the database but rather pulled from the underlying tables on demand. The<br />

view’s data set doesn’t exist anywhere until the moment that the view is used. When the view is executed,<br />

the DBMS pulls the data and performs any operations or calculations required by the <strong>SQL</strong> statement,<br />

and then it presents that data to the user.<br />

To the user, the view looks exactly like a table, with columns and rows of data, but usually the data is<br />

not persisted. A view can be as simple as a handful of fields from a single table or as complex as a system<br />

of joined tables pulling fields from any or all tables in a database.<br />

The following Try It Out shows you how to create a simple view.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!