Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Toward Integrity Part 4 Expand / Collapse
Author
Message
Posted Thursday, December 27, 2007 3:34 AM
Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Friday, December 5, 2014 5:54 PM
Points: 1,341, Visits: 805
Comments posted to this topic are about the item Toward Integrity Part 4


Post #436598
Posted Wednesday, January 2, 2008 8:12 AM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Saturday, May 4, 2013 5:21 AM
Points: 52, Visits: 117
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.
Post #437994
Posted Wednesday, January 2, 2008 10:37 AM
Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Friday, December 5, 2014 5:54 PM
Points: 1,341, Visits: 805
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



Post #438064
Posted Wednesday, January 2, 2008 11:33 AM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Saturday, May 4, 2013 5:21 AM
Points: 52, Visits: 117
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.
Post #438098
Posted Wednesday, January 2, 2008 11:44 AM
Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Friday, December 5, 2014 5:54 PM
Points: 1,341, Visits: 805
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



Post #438105
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse