March 10, 2012 at 6:36 pm
nice and easy question since it is mostly what I am doing now 🙂
I would not have said that some time ago: I deleted a row in a view and discovered that the row in the parent table has been deleted too!!!! :crazy: I always tought that a view "is a view", not that it could be an "interface" with a table!!!
Since then, I put only "SELECT" permission on the views to make sure that nobody messed up with the data base without knowing it!
Thanks for the question!
March 12, 2012 at 12:07 am
Nice back to basics question, thanks.
Need an answer? No, you need a question
My blog at https://sqlkover.com.
MCSE Business Intelligence - Microsoft Data Platform MVP
March 12, 2012 at 2:03 am
A good, solid question. Too bad it's only worth 1 point, I think 2 points would have been called for here.
tilew-948340 (3/10/2012)
Since then, I put only "SELECT" permission on the views to make sure that nobody messed up with the data base without knowing it!
Many people do the reverse - they give nobody any permissions on the base tables and give permissions on views only. For insert and delete, that does not really change much, but for update and select, it's a good way to show only relevant information. For instance, if your table contains a column is_active and you have a view that includes WHERE is_active = 1 and does not include columns that are only relevant to non-active members, you would give permissions on that view to the relevant users and they would never be able to see data about inactive members - neither the members, nor the columns that are relevant for inactive members.
And if the view includes the WITH CHECK option, those people would also be unable to change active members to non-active members.
March 12, 2012 at 2:56 am
Good job I wasn't in Oracle mode.
Nice question.
March 12, 2012 at 4:30 am
Nice "real world" question today. Thanks.
March 12, 2012 at 4:46 am
This was removed by the editor as SPAM
March 12, 2012 at 7:01 am
This was a good question, but please check your spelling before submitting the question.
In the options available to be checked, results was spelled wrong twice and delete was spelled wrong once.
This make a good question look bad.
March 12, 2012 at 7:23 am
Thanks for an easy one 🙂
Peter Trast
Microsoft Certified ...(insert many literal strings here)
Microsoft Design Architect with Alexander Open Systems
March 12, 2012 at 7:23 am
Excellent question, thanks
March 12, 2012 at 7:30 am
Thanks for the question. Got one option right, the other wrong so I have learnt something today. Thanks for this and as always for the discussion.
March 12, 2012 at 8:11 am
Neela Khatri (3/12/2012)
...In the options available to be checked, results was spelled wrong twice ...
And I thought this was to help find the correct answers :laugh:
Check it out--the two options where it reads resutls are the correct ones.
Cheers,
Michael
March 12, 2012 at 10:25 am
Simple yet important concept on views where multiple tables are involved regardless of indexes, primary or foreign keys. Thanks for the question.
March 12, 2012 at 11:24 am
I don't say it's a bad question because it's obvious what answer is intended, and the intended answer for the first view is right whenever the view is an updateable one, which most views whose columns are columns of a single underlying table are, and the intended answer for the second view is right whenever that view doesn't have an "instead of delete" trigger. It will probably make people think and/or consult BoL and learn something, and perhaps at least learn to be careful about deleting through views, so it's far more good than bad.
But it's not really good because the real answer about delete from each view is "it depends".
The question doesn't specify that the first view is an updateable view. Just create a trivial view with with select distinct (so that it is not an updatabale view) and straight away any attempt to delete a row using this view will fail with an error. Since it's made "with columns in table one" some of the ways of making an non-updateable table are ruled out but certainly using "distinct" isn't and neither is anything involving both TOP and the WITH CHECK OPTION. It isn't even clear that a view where a column is constructed from columns (plural) of the underlying table rather than from a single column of the underlying table is ruled out by the wording of the question (try having a column made by adding two columns of the underlying table, and once again it won't work), and it's certainly triue for example that an aggregates like MAX(col_x) is make from column col_x so the wording doesn't rule out the use of aggregates (and aggregates make the view non-updateable). It depends on teh view definition of the first view.
fr teh second view, although the question specifies that neither table has any trigger it doesn't specify that the views don't have triggers - so the second view could perfectly well have an "instead of delete" trigger that did something sensible, so no error message. It depends on whether a suitable trigger exists for the second view.
Tom
March 12, 2012 at 11:33 am
Another thing that people should be cautious about when deleting a single row in a view: if the view is updateable (so that you can delete without an instead of trigger), has no trigger, and has duplicate rows and the (single) underlying table doesn't (so it's a view that doesn't include the whole of the primary key of the underlying table) and you ensure deleting a single row in the view by using "delete top (1) ...." it will delete one of the rows in the underlying table. Since the underlying table doesn't have any duplicates, it makes sense to ask "which row" for the underlying table (it doesn't make sense to ask that for the view, because in the view you can't distinguish between the duplicate rows). The only way you can find out is to suck it and see.
Tom
Viewing 15 posts - 1 through 15 (of 27 total)
You must be logged in to reply to this topic. Login to reply