This article forgot to mention that constraints can be used by the optimizer, while TRIGGERs cannot. Constraints are also portable, while TRIGGER code is not very portable. Another old trick to avoid triggers for constraints is to give access to the base table only thru a VIEW that has a WITH CHECK OPTION. This leads to declarative code that the optimizer can use.
>> MyCompany.com recently implemented a business rule that the number of BigScreen products sold in a day can never exceed five if the product is sold for less than $500. The move is an effort to keep discount prices from exceeding the company's wishes. We will look at one of the several ways in which this can be accomplished.
I had to correct the data element names to conform to ISO-11179 rules, but here is a VIEW to enforce that business rule.
CREATE VIEW ConstraintedSalesHistory (sale_ticket, product_type, sale_date, sale_price)
AS
SELECT S1.sale_ticket, S1.product_type, S1.sale_date, S1.sale_price
FROM SalesHistory AS S1
WHERE 5 = ALL (SELECT COUNT(*)
FROM SalesHistory AS S2
WHERE S2.product_type = 'BigScreen'
AND S2.sale_price 500.00
GROUP BY S2.sale_date)
WITH CHECK OPTION;
Discussion on:
Message 3 of 7









































