Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

I am one of those older programmers that has spent most of his life in C++ without databases.

 

Now, I am working more with VB and C# using more and more SQL Server data.

 

In the database, there are Tables and there are Views. Most of the old code that we have here collects data by calling one of the Views instead of reading directly from the Tables.

 

Why?

 

I tried asking someone before, but the answer didn't fully explain anything. I was told something like "a View presents a snapshot of the data and consumes fewer resources that querying data from a Table." Could someone expand on this? ...in layman's terms, please.

  • Leaders
Posted
That's not true at all. You can think of a view more like a stored query that you can access like a table but which holds no data of it's own.
"These Patriot playoff wins are like Ray Charles songs, Nantucket sunsets, and hot fudge sundaes. Each one is better than the last." - Dan Shaughnessy
  • Administrators
Posted

Views can make your life a bit easier in a couple of ways though - firstly security may be set on views to allow people to query the view but not requiring them to have permissions on the underlying table.

Secondly if the underlying tables change all code based on them will need to be modified, if views are being used then only the view will need to be updated to reflect the underlying changes - code using the view will still work.

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

  • 3 weeks later...
Posted

The beauty of views is that they can hide complexity and present information in a denormalised form.

 

Just for examples sake consider a database where you have tables for Employee, Department and JobRole.

 

If the data is normalised then a row for an Employee might look like

 

EmployeeID EmployeeName DepartmentID JobRoleID .... Field n

123 Joe Bloggs 456 789 etc.

 

And the Department table might look like

 

DepartmentID DepartmentName

101 Finance

456 Sales

 

And similarly JobRole might look like

 

JobeRoleID JobRoleDescription

123 Receptionist

789 Software Mechanic

 

So, in a world without views, to get some meaningful information about employees your query might be:

 

select E.EmployeeName,

D.DepartmentName,

J.JobRoleDescription

 

from Employee E

inner join Department D on E.DepartmentID = D.DepartmentID

inner join JobRole J on E.JobRoleID = J.JobRoleID

 

 

If you take the sql above and create a view from it, your query will simply become

 

select EmployeeName, DepartmentName, JobRoleDescription from vwEmployee

 

Which makes the whole thing more manageable, particularly once your database grows beyond a few tens of tables!

 

J

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...