How do I make a table in SQL Server only allow four instances of one person to be entered?

MY SPOUSE AND I don’t desire there for being more in comparison with 4 regarding any unique personId connected to buying any one product (if that they buy SOMETHING LIKE 20 different purchases, then it can be fine regarding them being in presently there 20 times, but not when they buy 5 of the same thing).Therefore the primary crucial is personId as well as productId.

Are you saying that it’s OK for a given person in making 20 different purchase belonging to the same product but just around 4 in the given transaction or a maximum involving 4 full Either is definitely doable though the latter more tricky (and seems more like what you need…if the former, you merely will have to define your CHECK concern against OrderItem.amount.) It’d also help if you describe something with the schema, but I will take a educated speculate.Given a schema something similar to the next:

Client (person INSIDE DIAMETER,…)
Product or service (productID,…)
Order (orderID, personID,…)
OrderItem (orderID, lineID, productID, amount,…)

you can define your INSERT or maybe UPDATE cause against OrderItem (DELETE would not be needed however, since taking away products from your order would not break your enterprise rule.) The particular basic gist of the trigger could well be:

PICK SUM(quantity) + brand new.quantity IN productTotal
BY OrderItem oi SUBSCRIBE TO Order o ON oi.orderID = to.orderID
WHEN productID = brand new.productID AND ALSO personID =
(SELECT personID BY Order WHEN orderID = new.orderID)

In that case, if productTotal > 5, disallow the particular INSERT/UPDATE.

Leave a Reply