• George Heinrich (1/2/2008)


    These articles are very good...but the author lost me on article 4.

    Could you go into more detail on how the indexed view and using the negative matches enforces integrity in this case? Any deeper explanation (i.e. for dummies) would be helpful.

    Thanks,

    George H.

    Hi George;

    Thank you for the feedback on the article. I'm going to try another explanation, but since I don't like the "for Dummies" label and because I think that anyone that takes the time to read the content on SQLServerCentral.com is far from a dummy anyway (yourself included), I'll take a cue from Celko and give you an explanation for "Smarties" 🙂

    For context, I'll start with the executive summary, which I think you "get" already...

    To enforce the rule, we take the negation of logical expression of the rule (let's call it the "invalid condition expression"), JOIN it to the association table, and then take the product (CROSS JOIN) of that result with a table we know to have a cardinality > 1 (i.e., the "Digits" table). The unique constraint on the view is then violated (and an error raised) any time an association is made that conforms to the "invalid condition expression".

    More detail:

    We have a rule that only a "Senior" vet (attribute Vets.ExperienceRating = "Senior") may treat a pet with a "Difficult" disposition (attribute Pets.DispositionRating = "Difficult").

    Therefore, anytime we associate a Pet and a Vet via the association table, it must be the case that if the pet in the association is "Difficult", the vet must be "Senior". If the pet isn't "Difficult", we don't care the experience rating of the vet.

    The association table, however, does not have the ExperienceRating and DispositionRating attributes in it. So, we must take a JOIN of the 3 tables in order to "gather" all of the attributes we need. This JOIN is implemented with the view definition. Additionally, within that view definition, we are applying a filter that will _exclude_ any valid rows, and _include_ only the invalid rows. This is the expressed as the predicate for the JOIN clause -- the article has the walkthrough of creating this part of the T-SQL expression from the logical expression.

    The last part of the view definition is to take the relational product (CROSS JOIN) of the above result with a table known to have multiple rows (the "Digits" table). This way, whenever there is a row that conforms to the JOIN predicate of the view, the pet's LicenseNbr value will be repeated once for each row of the "Digits" table (10 times in this case).

    Now, since the view has a uniqueness constraint on the LicenseNbr attribute, the repeating of values for that attribute causes the constraint violation error to fire, and the database update is rejected.

    I realize the technique outlined in article 4 is rather a "tricky" implementation choice. As such, I considered taking a different approach for the realization of the business rule. In the end, though, I thought it would be better to present it. There are some other powerful things that can be done using the approach, and I wanted it available for reference in possible future articles.

    HTH,

    TroyK