SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Toward Integrity Part 4


Toward Integrity Part 4

Author
Message
cs_troyk
cs_troyk
SSCommitted
SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)

Group: General Forum Members
Points: 1704 Visits: 969
Comments posted to this topic are about the item Toward Integrity Part 4



George H.
George H.
SSC-Enthusiastic
SSC-Enthusiastic (116 reputation)SSC-Enthusiastic (116 reputation)SSC-Enthusiastic (116 reputation)SSC-Enthusiastic (116 reputation)SSC-Enthusiastic (116 reputation)SSC-Enthusiastic (116 reputation)SSC-Enthusiastic (116 reputation)SSC-Enthusiastic (116 reputation)

Group: General Forum Members
Points: 116 Visits: 118
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.
cs_troyk
cs_troyk
SSCommitted
SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)

Group: General Forum Members
Points: 1704 Visits: 969
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" Smile

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



George H.
George H.
SSC-Enthusiastic
SSC-Enthusiastic (116 reputation)SSC-Enthusiastic (116 reputation)SSC-Enthusiastic (116 reputation)SSC-Enthusiastic (116 reputation)SSC-Enthusiastic (116 reputation)SSC-Enthusiastic (116 reputation)SSC-Enthusiastic (116 reputation)SSC-Enthusiastic (116 reputation)

Group: General Forum Members
Points: 116 Visits: 118
Ahhh...ok. I think I get it now. The view and the digits table are just there to throw an error in the event that the wrong criteria are caught (i.e., animal = difficult and vet <> advanced).

Thanks for the supplemental explanation.

George H.
cs_troyk
cs_troyk
SSCommitted
SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)

Group: General Forum Members
Points: 1704 Visits: 969
George Heinrich (1/2/2008)
Ahhh...ok. I think I get it now. The view and the digits table are just there to throw an error in the event that the wrong criteria are caught (i.e., animal = difficult and vet <> advanced).

Thanks for the supplemental explanation.

George H.


Bingo!

TroyK



Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search