Click here to Skip to main content
15,895,462 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I want to about the real use of views in sql server. In which situation i can prefer views for my project also In which situation views are used?. Can you explain this with one real time example.

Most of them are said views are used to restrict some users to see the contents of real tables.

But I have a doubt... When we creating a web application the database user of this application may be a single user. Then how this views are used in web applications.


The database user of web application should be a administrator then we will restrict which user to prevent to see the original tables.



Thanks,
Velkumar.
Posted
Comments
Sergey Alexandrovich Kryukov 23-Oct-12 1:22am    
This is very productive way of thinking. You need to learn how views work, that's it. If you don't see the situations where you need them, remember about the existing possibility and recall it later, when you see the need in it, which is pretty obvious. I think if you read about it, you will see a need.

And the doubt about roles is just irrelevant to the topic. You can have a similar concern on any changes in database, any at all. Views are no special.
--SA

Hi...

Consider that your database has many number of tables and there may be a situation that you need data from many tables at a time by using JOINS.
SQL
select *
from a inner join b on a.col1 = b.col1
    inner join c on b.col2 = c.col2
    inner join d on c.col3 = d.col3
    inner join e on d.col4 = e.col4
    inner join f on e.col5 = f.col5
    inner join g on f.col6 = g.col6
where a.col1 = 10

Now it may happen that you need this data at many places, say in 10 SPs. Then instead of using JOINS in each SP, you can create a view which contains data from multiple tables.
SQL
create view View1 AS
select *
from a inner join b on a.col1 = b.col1
    inner join c on b.col2 = c.col2
    inner join d on c.col3 = d.col3
    inner join e on d.col4 = e.col4
    inner join f on e.col5 = f.col5
    inner join g on f.col6 = g.col6

Then depending on situation, you can get data from View:
SQL
select * from View1 where col1 = 10

Now consider that in all the SPs u need to add one more table in the join. In that case instead of modifying all the SPs, you just need to modify one view.

This was just a simple example of view, but there are many more advantages of using it.
 
Share this answer
 
v2
Comments
Velkumar Kannan 23-Oct-12 2:06am    
Ok good example.

Can u explain the how views are used for security mechanism such as preventing some users to access the tables, columns or rows of a table.
Gautam Raithatha 23-Oct-12 2:14am    
In sql server, there are inbuilt security mechanisms which can be used to prevent or allow some users the access of some or all objects (objects means tables, views, stored procedures etc.). By using such mechanisms, some users can be denied permission on tables, but permission can be granted on view. The view may contain only data from some of the columns of some tables.

So the user will not be able to access all the data and is unaware of table structure.

You can refer below or many other articles are present:
http://msdn.microsoft.com/en-us/library/ms188371.aspx
Velkumar Kannan 23-Oct-12 2:27am    
Can u tell which user you are mentioning about?
DB user or any other user.
We are writing views in DB. Then which has a restriction to access these views?
Gautam Raithatha 23-Oct-12 2:43am    
DB user, identified from username and password used to login in sql server.
Velkumar Kannan 23-Oct-12 3:33am    
k. i accept this.

A web application should have one user. This user should have all permission to access the resources. Then which user has restrictions to access this resources.
This may be common for all web application.
Please see:
http://en.wikipedia.org/wiki/View_%28database%29[^].

Isn't it quite enough to get the idea?

Anyway, I think the most principle part of my answer is in my comment to the question. Just think about it.

—SA
 
Share this answer
 
Comments
Velkumar Kannan 23-Oct-12 1:27am    
I have to know the situation where it can be used
Sergey Alexandrovich Kryukov 23-Oct-12 2:04am    
Of course you have. Just think about it -- it will be the best answer. The motivation is explained. Any questions?
--SA
We Use Views in the places where the no of columns are not known to us
then we use a view to keep data in it .. it can not be used as a permanent table
 
Share this answer
 
Comments
Velkumar Kannan 23-Oct-12 4:45am    
I cant understand. Can you give an explanation

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900