472,782 Members | 1,152 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,782 software developers and data experts.

Views vs Queries

Hello:

I am new to T-SQL programing, and relativly new to SQL statements in
general, although I have a good understanding of database theory. I'm a
little confused as to the fundamental differences between a view and a
query, or rather, when it is more appropriate to use one vs. the other.

It seems to me that most select queries can be implemented as views, and I
can't see the downside to doing so.

I have a rather complicated database structure in terms of the relationships
between various tables, and to get the information that I need into the user
interface forms I have to use a number of nested queries (first to combine
tables together that have been normalized to place the related info in a
single table, then next to extract only the info that I require. In some
cases I would need a 3 level deep nested query to get to what I need. By
implementing the first step, that of combing the normalized tables back into
a single table as a view, it seems to me that I've completed the first step
at the database level, thus making my queries or stored procedures easier to
implement.

I would appreciate any comments which can help me to decide when it is or is
not appropriate to use a view in these circumstances. I should point out
that I don't plan on using the views as part of action queries since they
are made up of multiple tables, I'm primarily interested in using them to
output data which has been consiolidated from multiple tables.

Thanks in advance to any and all who take the time to reply.

Jul 20 '05 #1
8 15108
Hi

Views are a way to encapsulate code and abstract the user from some of the
complexity of the underlying structure, as you state. Whether you use a
view, stored procedure, function or query will depend on the task in hand.

Advantages of views include reduced maintainance and the ability to be
indexed. The biggest dis-advantage is security in that you have access to
base tables.

The most common security model dictates that code is written in stored
procedure therefore direct access to base tables can be denied, and actions
can be maintained and controlled easily.

Therefore a combination of views used in stored procedures is probably the
solution.

John


"Mike N." <Ba****@thepound.com> wrote in message
news:eS****************@newssvr25.news.prodigy.com ...
Hello:

I am new to T-SQL programing, and relativly new to SQL statements in
general, although I have a good understanding of database theory. I'm a
little confused as to the fundamental differences between a view and a
query, or rather, when it is more appropriate to use one vs. the other.

It seems to me that most select queries can be implemented as views, and I
can't see the downside to doing so.

I have a rather complicated database structure in terms of the relationships between various tables, and to get the information that I need into the user interface forms I have to use a number of nested queries (first to combine
tables together that have been normalized to place the related info in a
single table, then next to extract only the info that I require. In some
cases I would need a 3 level deep nested query to get to what I need. By
implementing the first step, that of combing the normalized tables back into a single table as a view, it seems to me that I've completed the first step at the database level, thus making my queries or stored procedures easier to implement.

I would appreciate any comments which can help me to decide when it is or is not appropriate to use a view in these circumstances. I should point out
that I don't plan on using the views as part of action queries since they
are made up of multiple tables, I'm primarily interested in using them to
output data which has been consiolidated from multiple tables.

Thanks in advance to any and all who take the time to reply.

Jul 20 '05 #2
Thanks you for taking the time to reply. I don't intend to have direct
access to any views, they will all be used in the same way the base tables
are, i.e. as data suppliers for windows forms objects. This sounds like what
you are saying, i.e. I establish the view, and then use it from a stored
procedure to send the data in it back to the middle tier.

Part of my concern had to do with the updating of views since they don't
automatically update with the base tables, but this seems to be the same
issue as data in a query.

FYI, I can't index my views as my version of SQL-Server doesn't include that
option (this is an ameteur type project on my home system). Will that make
a difference between choosing a query over a view?

Once again, thank you for replying to my question (I have lots more too :)

Mike
"John Bell" <jb************@hotmail.com> wrote in message
news:bo**********@titan.btinternet.com...
Hi

Views are a way to encapsulate code and abstract the user from some of the
complexity of the underlying structure, as you state. Whether you use a
view, stored procedure, function or query will depend on the task in hand.

Advantages of views include reduced maintainance and the ability to be
indexed. The biggest dis-advantage is security in that you have access to
base tables.

The most common security model dictates that code is written in stored
procedure therefore direct access to base tables can be denied, and actions can be maintained and controlled easily.

Therefore a combination of views used in stored procedures is probably the
solution.

John


"Mike N." <Ba****@thepound.com> wrote in message
news:eS****************@newssvr25.news.prodigy.com ...
Hello:

I am new to T-SQL programing, and relativly new to SQL statements in
general, although I have a good understanding of database theory. I'm a
little confused as to the fundamental differences between a view and a
query, or rather, when it is more appropriate to use one vs. the other.

It seems to me that most select queries can be implemented as views, and I can't see the downside to doing so.

I have a rather complicated database structure in terms of the relationships
between various tables, and to get the information that I need into the

user
interface forms I have to use a number of nested queries (first to combine tables together that have been normalized to place the related info in a
single table, then next to extract only the info that I require. In some cases I would need a 3 level deep nested query to get to what I need. By implementing the first step, that of combing the normalized tables back

into
a single table as a view, it seems to me that I've completed the first

step
at the database level, thus making my queries or stored procedures easier to
implement.

I would appreciate any comments which can help me to decide when it is
or is
not appropriate to use a view in these circumstances. I should point

out that I don't plan on using the views as part of action queries since they are made up of multiple tables, I'm primarily interested in using them to output data which has been consiolidated from multiple tables.

Thanks in advance to any and all who take the time to reply.


Jul 20 '05 #3
As a general rule of thumb, I use queries where I need to obtain
something particularily complex (can be several hundred lines) and
have to use temporary tables (something you can't do in a view). We
often build up a series of statements into a final result this way. We
use views for more simplistic ways of getting the data (although still
quite complex), where it is possible to fully understand the entire
SQL statement and can obtain the data in a single statement. You can
always use a view into another view and SQL will treat it as a single
statement. It's a very broad explanation at best.

Don't forget you can use UNION ALL in views (and queries) to bring two
identical sets of results together.

Using a view does give you the advantage that you can modify the view
/ results to some extent without changing the application and
re-releasing it.

You can't update the data back in the tables through a view. For
exaple, an UPDATE statement will work in a query, but not in a view.
The other option for this is to use a stored procedure.

One thing worth having a look at is the execution plan of a query /
view. SQL determines the best method of obtaining the data and
compiles an execution plan which it caches. By working with your query
/ view and optimising it, you can have an affect on this and speed up
the query. It may reduce table scans which take up valuable time and
resource.

All of this depends on what it is you want to do with your data.

"Mike N." <Ba****@thepound.com> wrote in message news:<ic*****************@newssvr25.news.prodigy.c om>...
Thanks you for taking the time to reply. I don't intend to have direct
access to any views, they will all be used in the same way the base tables
are, i.e. as data suppliers for windows forms objects. This sounds like what
you are saying, i.e. I establish the view, and then use it from a stored
procedure to send the data in it back to the middle tier.

Part of my concern had to do with the updating of views since they don't
automatically update with the base tables, but this seems to be the same
issue as data in a query.

FYI, I can't index my views as my version of SQL-Server doesn't include that
option (this is an ameteur type project on my home system). Will that make
a difference between choosing a query over a view?

Once again, thank you for replying to my question (I have lots more too :)

Mike
"John Bell" <jb************@hotmail.com> wrote in message
news:bo**********@titan.btinternet.com...
Hi

Views are a way to encapsulate code and abstract the user from some of the
complexity of the underlying structure, as you state. Whether you use a
view, stored procedure, function or query will depend on the task in hand.

Advantages of views include reduced maintainance and the ability to be
indexed. The biggest dis-advantage is security in that you have access to
base tables.

The most common security model dictates that code is written in stored
procedure therefore direct access to base tables can be denied, and

actions
can be maintained and controlled easily.

Therefore a combination of views used in stored procedures is probably the
solution.

John


"Mike N." <Ba****@thepound.com> wrote in message
news:eS****************@newssvr25.news.prodigy.com ...
Hello:

I am new to T-SQL programing, and relativly new to SQL statements in
general, although I have a good understanding of database theory. I'm a
little confused as to the fundamental differences between a view and a
query, or rather, when it is more appropriate to use one vs. the other.

It seems to me that most select queries can be implemented as views, and I can't see the downside to doing so.

I have a rather complicated database structure in terms of the relationships between various tables, and to get the information that I need into the user interface forms I have to use a number of nested queries (first to combine tables together that have been normalized to place the related info in a
single table, then next to extract only the info that I require. In some cases I would need a 3 level deep nested query to get to what I need. By implementing the first step, that of combing the normalized tables back into a single table as a view, it seems to me that I've completed the first step at the database level, thus making my queries or stored procedures easier
to implement.

I would appreciate any comments which can help me to decide when it is or
is not appropriate to use a view in these circumstances. I should point out that I don't plan on using the views as part of action queries since they are made up of multiple tables, I'm primarily interested in using them to output data which has been consiolidated from multiple tables.

Thanks in advance to any and all who take the time to reply.


Jul 20 '05 #4
Thank you for your reply Ryan, your comments have been helpful to me.

"Ryan" <ry********@hotmail.com> wrote in message
news:78**************************@posting.google.c om...
As a general rule of thumb, I use queries where I need to obtain
something particularily complex (can be several hundred lines) and
have to use temporary tables (something you can't do in a view). We
often build up a series of statements into a final result this way. We
use views for more simplistic ways of getting the data (although still
quite complex), where it is possible to fully understand the entire
SQL statement and can obtain the data in a single statement. You can
always use a view into another view and SQL will treat it as a single
statement. It's a very broad explanation at best.

Don't forget you can use UNION ALL in views (and queries) to bring two
identical sets of results together.

Using a view does give you the advantage that you can modify the view
/ results to some extent without changing the application and
re-releasing it.

You can't update the data back in the tables through a view. For
exaple, an UPDATE statement will work in a query, but not in a view.
The other option for this is to use a stored procedure.

One thing worth having a look at is the execution plan of a query /
view. SQL determines the best method of obtaining the data and
compiles an execution plan which it caches. By working with your query
/ view and optimising it, you can have an affect on this and speed up
the query. It may reduce table scans which take up valuable time and
resource.

All of this depends on what it is you want to do with your data.

"Mike N." <Ba****@thepound.com> wrote in message

news:<ic*****************@newssvr25.news.prodigy.c om>...
Thanks you for taking the time to reply. I don't intend to have direct
access to any views, they will all be used in the same way the base tables are, i.e. as data suppliers for windows forms objects. This sounds like what you are saying, i.e. I establish the view, and then use it from a stored
procedure to send the data in it back to the middle tier.

Part of my concern had to do with the updating of views since they don't
automatically update with the base tables, but this seems to be the same
issue as data in a query.

FYI, I can't index my views as my version of SQL-Server doesn't include that option (this is an ameteur type project on my home system). Will that make a difference between choosing a query over a view?

Once again, thank you for replying to my question (I have lots more too :)
Mike
"John Bell" <jb************@hotmail.com> wrote in message
news:bo**********@titan.btinternet.com...
Hi

Views are a way to encapsulate code and abstract the user from some of the complexity of the underlying structure, as you state. Whether you use a view, stored procedure, function or query will depend on the task in hand.
Advantages of views include reduced maintainance and the ability to be
indexed. The biggest dis-advantage is security in that you have access to base tables.

The most common security model dictates that code is written in stored
procedure therefore direct access to base tables can be denied, and

actions
can be maintained and controlled easily.

Therefore a combination of views used in stored procedures is probably the solution.

John


"Mike N." <Ba****@thepound.com> wrote in message
news:eS****************@newssvr25.news.prodigy.com ...
> Hello:
>
> I am new to T-SQL programing, and relativly new to SQL statements in
> general, although I have a good understanding of database theory. I'm a > little confused as to the fundamental differences between a view and a > query, or rather, when it is more appropriate to use one vs. the other. >
> It seems to me that most select queries can be implemented as views, and
I
> can't see the downside to doing so.
>
> I have a rather complicated database structure in terms of the

relationships
> between various tables, and to get the information that I need into
the user
> interface forms I have to use a number of nested queries (first to

combine
> tables together that have been normalized to place the related info
in a > single table, then next to extract only the info that I require. In

some
> cases I would need a 3 level deep nested query to get to what I

need. By
> implementing the first step, that of combing the normalized tables
back into
> a single table as a view, it seems to me that I've completed the
first step
> at the database level, thus making my queries or stored procedures

easier
to
> implement.
>
> I would appreciate any comments which can help me to decide when it
is or
is
> not appropriate to use a view in these circumstances. I should
point out
> that I don't plan on using the views as part of action queries since

they
> are made up of multiple tables, I'm primarily interested in using
them to
> output data which has been consiolidated from multiple tables.
>
> Thanks in advance to any and all who take the time to reply.
>
>
>

Jul 20 '05 #5
>>>>>
The biggest dis-advantage [of views] is security in that you have access
to base tables.
<<<<<

I don't believe that you need to grant the user access to the base table
in order to have the view be able to access the base table. As long as
the owner of the view has the appropriate rights granted, the chain of
ownership should handle the rest.

HTH

=======================================
Everyone here speaks SQL; some are more fluent, others less. When
describing your SQL object (table, etc.), do so in the language that we
all understand - SQL, not English. It makes it easier to understand
your issue and makes it more likely that you will get the assistance
that you are asking for.

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 20 '05 #6
See books online:

To execute the view, the user must have SELECT permission on the
tables, views, and table-valued functions being referenced in the
view, and EXECUTE permission on the scalar-valued functions being
invoked in the view.

http://msdn.microsoft.com/library/de...eate2_30hj.asp

John

Stephen Hendricks <ha***@londonfg.com> wrote in message news:<3f*********************@news.frii.net>...
>>

The biggest dis-advantage [of views] is security in that you have access
to base tables.
<<<<<

I don't believe that you need to grant the user access to the base table
in order to have the view be able to access the base table. As long as
the owner of the view has the appropriate rights granted, the chain of
ownership should handle the rest.

HTH

=======================================
Everyone here speaks SQL; some are more fluent, others less. When
describing your SQL object (table, etc.), do so in the language that we
all understand - SQL, not English. It makes it easier to understand
your issue and makes it more likely that you will get the assistance
that you are asking for.

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Jul 20 '05 #7
In order to access any database object you need the proper permissions.
This does NOT mean that the user needs to have these permissions
directly; they can get the permission through the chain of ownership.
In this case, the owner of the view (dbo?) would have the select
permissions and the user would be granted permissions to the view; not
the table.

One of the uses of a view is to limit access to sensitive data in a
table. For instance, a Personnel table that had both public (Name,
Title, Phone Extension, etc.) and private (Salary, Medical, etc.)

A view would be created (PersonnelPublic) that only selected the public
data. Select rights to this view would be granted to the employees.

Now if an employee selects on the view PersonnelPublic, they get the
data returned but if they select on the base table Personnel, they get a
permission denied error.

HTH

=======================================
Everyone here speaks SQL; some are more fluent, others less. When
describing your SQL object (table, etc.), do so in the language that we
all understand - SQL, not English. It makes it easier to understand
your issue and makes it more likely that you will get the assistance
that you are asking for.

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 20 '05 #8
A full description of ownership chains is given at:

http://msdn.microsoft.com/library/de...urity_4iyb.asp

John

Stephen Hendricks <ha***@londonfg.com> wrote in message news:<3f*********************@news.frii.net>...
In order to access any database object you need the proper permissions.
This does NOT mean that the user needs to have these permissions
directly; they can get the permission through the chain of ownership.
In this case, the owner of the view (dbo?) would have the select
permissions and the user would be granted permissions to the view; not
the table.

One of the uses of a view is to limit access to sensitive data in a
table. For instance, a Personnel table that had both public (Name,
Title, Phone Extension, etc.) and private (Salary, Medical, etc.)

A view would be created (PersonnelPublic) that only selected the public
data. Select rights to this view would be granted to the employees.

Now if an employee selects on the view PersonnelPublic, they get the
data returned but if they select on the base table Personnel, they get a
permission denied error.

HTH

=======================================
Everyone here speaks SQL; some are more fluent, others less. When
describing your SQL object (table, etc.), do so in the language that we
all understand - SQL, not English. It makes it easier to understand
your issue and makes it more likely that you will get the assistance
that you are asking for.

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Jul 20 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Marko Poutiainen | last post by:
Situation: We had to make our SQLServer 2000 database multi-lingual. That is, certain things (such as product names) in the database should be shown in the language the user is using (Finnish,...
8
by: lyn.duong | last post by:
Hi, I have a large table (about 50G) which stores data for over 7 years. I decided to split this table up into a yearly basis and in order to allow minimum changes to the applications which...
3
by: KemperR | last post by:
Hello Experts outhere, may be someone can tell me whats going wrong with my ADOX trial. I have an Access 2002 database with some tables and queries (views) The code listed below works well up...
8
by: Martijn van Oosterhout | last post by:
Currently you can create temporary tables that are deleted at the end of the session. But how about temporary views? It's just a table with a rule so I don't imagine it would be terribly difficult....
28
by: mooreit | last post by:
The purpose for my questions is accessing these technologies from applications. I develop both applications and databases. Working with Microsoft C#.NET and Microsoft SQL Server 2000 Production and...
15
by: rod.weir | last post by:
Fellow database developers, I would like to draw on your experience with views. I have a database that includes many views. Sometimes, views contains other views, and those views in turn may...
2
by: umair.cheema | last post by:
Hi! 1-I read in MS Access help that Indexes are automatically made when we declare a primary key and they are used for fast searching and sorting purpose. But i am still confused that how can i...
1
pbmods
by: pbmods | last post by:
Today I'd like to talk about a new feature introduced in MySQL 5: Views. Views are like snapshots of queries. You can save any query as a view, even complex queries with joins and subqueries,...
7
by: Gary | last post by:
Hello guys! Bear with me, I am a newbie. She is the Data Warehouse manager. She has about 50 users to use the Oracle database from M$ Access via ODBC connection. All those users have only...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.