Database Reference
In-Depth Information
reference columns from one base table. For example, let's assume that we
have a view that returns customer data from two tables. One table stores
the customer's information, and the other holds the address data for that
customer. The definition of the customer_address view is as follows:
CREATE VIEW customer_address
AS
SELECT customer.first_name,
customer.last_name,
customer.phone,
address.address_line1,
address.city,
address.state,
address.zip
FROM customer
JOIN address
ON address.customer_id = customer.customer_id
WHERE address.type = 'home'
You can perform INSERT, UPDATE, and DELETE operations against
the customer_address view as long as you reference only the customer
table or the address table.
You may be asking yourself, “Why would I use a view instead of just
referencing the tables directly?” There are several reasons to use views in
your database. First, you can use a view to obscure the complexity of the
underlying tables. If you have a single view that displays customer and ad-
dress information, developers or end users can access the information they
need from the view instead of needing to go to both tables. This technique
eliminates the need for users to understand the entire database; they can
focus on a single object. You gain an exponential benefit when you start
working with many base tables in a single view.
Using views also allows you to change the tables or the location where
the data is stored without affecting users. In the end, as long as you update
the view definition so that it accommodates the table changes you made,
your users will never need to know that there was a change. You can also
use views to better manage security. If you have users who need to see
some employee data but not sensitive data such as social security numbers
or salary, you can build a view that displays only the information they need.
Finally, consider how using views can save you time when querying
your database . Every time you run T-SQL code, SQL Server must first
Search WWH ::




Custom Search