VIEWS 2

  • Comments posted to this topic are about the item VIEWS 2

    If everything seems to be going well, you have obviously overlooked something.

    Ron

    Please help us, help you -before posting a question please read[/url]
    Before posting a performance problem please read[/url]

  • 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!

  • 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

  • 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.


    Hugo Kornelis, SQL Server/Data Platform MVP (2006-2016)
    Visit my SQL Server blog: https://sqlserverfast.com/blog/
    SQL Server Execution Plan Reference: https://sqlserverfast.com/epr/

  • Good job I wasn't in Oracle mode.

    Nice question.

  • Nice "real world" question today. Thanks.

  • This was removed by the editor as SPAM

  • 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.

  • Thanks for an easy one 🙂

    Peter Trast
    Microsoft Certified ...(insert many literal strings here)
    Microsoft Design Architect with Alexander Open Systems

  • Excellent question, thanks

  • 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.

  • 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

  • Simple yet important concept on views where multiple tables are involved regardless of indexes, primary or foreign keys. Thanks for the question.

  • 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

  • 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