[quoted text, click to view] On 15 Mar 2004 15:54:56 -0800, Maellic wrote:
>However when I need to access all the data for my total calculations,
>I might actually follow Hugo's suggestion to use a view that joins all
>the tables.
Beware though, that to get all data from the main table joined with
all subtables, you'd need to use either OUTER JOIN or UNION. If you
try it with INNER JOIN, the result set will be empty.
[quoted text, click to view] >I'm not very familiar with views and although I've researched it, I
>still haven't really understood the difference between them and stored
>procedures. What's the difference physically on SQL server, and also
>logically ?
A stored procedure is basically a (usually small) program that is
stored on the server and executed on demand. It may do anything: show
data, change data, access other servers, drop or create database
objects, commit or rollback transactions or even shut down your
server. Some stored procedures return data, others don't.
A view always consists of one select statement. The SQL for that
statement is stored in the server as the view definition. Now, the
view can be used anywhere you'd normally use a table: you can select
all or part of the data from the view and in many cases, you can
insert, update and delete data through the view. Since the view has
it's own authorization settings, it's a useful method to allow people
access to some but not all data in a table. A common example is the
table with personnel data. The manager of the sales department is
entitled to see data of "his" employees, but nothing else. And even
for "his" employees, he is not entitled to see salary and some other
data. This can be done with a view:
CREATE VIEW Sales_Personnel AS
SELECT EmpID, FirstName, LastName, Dept, Address, HireDate
FROM Employees
WHERE Dept = 'Sales'
GO
You can now remove all rights of the Sales manager to the Employees
table but give him access rights to this view instead. This would
allow him to see only the columns mentioned of only the employees in
his own department.
Neither a view nor a stored procedure result in extra actual user data
being stored somewhere. Both are executed when needed to produce the
results from the tables used in either the stored procedure's code or
in the view definition.
[quoted text, click to view] >I designed a very similar database with Access and I know that when I
>tried joining all the tables in a query, Access run out of memory
>because they were too many tables in my join. Is that likely to happen
>as well with SQL Server ?
You can push any database over it's limit if you try hard enough, but
SQL Server really needs a whole lot of pushing before it's limits are
reached.
[quoted text, click to view] >* Second point is the natural key versus artificial key argument. I've
>seen a lot of conflicting opinions on this. I can see that using
>natural keys is going to avoid joins: as an e.g if I want to get all
>sources from a facility in a certain year, if I use an artificial key
>I will have to join tblFRPSources to tblFacilityReportingYear, however
>if I use a natural key I can just query tblFRPSources.
My main argument against unneeded artifical keys is not avoiding joins
(though that can be a useful side effect - as well as improving
performance on lots of other queries as well), but user friendliness.
Why would you introduce a new naming scheme on a user if (s)he's
perfectly happy with the current naming scheme?
Suppose the French mail delivery service introduced artificial keys as
a replacement for city names. How would you like it if you could not
simply jot "Marseille" or "Paris" on an envelope but would have to
look up these names in a long list and write "18" or "1" on the
envelope instead?
[quoted text, click to view] > But then, why
>do a lot of people swear by using only artificial keys ?
<soapbox>
Maybe they don't think. Or they think in old-fashioned ways. Or they
think that something that is really useful (sometimes even necessary)
in some cases and doesn't hurt *them* in other cases might as well be
used in all cases. Or -most probably- they do it because everybody
else does it.
It's a bit like starting all table names with "tbl", like you do. Why
do you do it? Are you afraid that when you get back to a working query
later, you might be in doubt whether the name after "from" would be a
table, a column or a database name?
</soapbox>
Best, Hugo
--