17.06.2013 Views

Beginning Microsoft SQL Server 2008 ... - S3 Tech Training

Beginning Microsoft SQL Server 2008 ... - S3 Tech Training

Beginning Microsoft SQL Server 2008 ... - S3 Tech Training

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 10: Views<br />

302<br />

columns to a group of people, but don’t want them to be able to see the general table structure or data?<br />

One solution would be to keep a separate table that includes only the columns that we need:<br />

Employees<br />

EmployeeID<br />

FirstName<br />

MiddleInitial<br />

LastName<br />

Title<br />

HireDate<br />

TerminationDate<br />

ManagerEmpID<br />

Department<br />

While on the surface this would meet our needs, it is extremely problematic:<br />

❑ We use disk space twice.<br />

❑ We have a synchronization problem if one table gets updated and the other doesn’t.<br />

❑ We have double I/O operations (you have to read and write the data in two places instead of<br />

one) whenever we need to insert, update, or delete rows.<br />

Views provide an easy and relatively elegant solution to this problem. When we use a view, the data is<br />

stored only once (in the underlying table or tables) — eliminating all of the problems just described.<br />

Instead of building our completely separate table, we can just build a view that will function in a nearly<br />

identical fashion.<br />

To make sure we know the state of our Accounting database, let's create a special version of it for use in<br />

the next example or two. To do this, load the CChapter10AccountingDBCreate.sql file (supplied<br />

with the source code) into the Management Studio and run it. Then add the following view to the<br />

AccountingChapter10 database:<br />

USE AccountingChapter10;<br />

GO<br />

CREATE VIEW Employees_vw<br />

AS<br />

SELECT EmployeeID,<br />

FirstName,<br />

MiddleInitial,<br />

LastName,<br />

Title,

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

Saved successfully!

Ooh no, something went wrong!