Click here to Skip to main content
15,912,329 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear All,
I Have a table as


id	pid	per
1	1	40
2	1	60
3	2	30
4	2	40
5	2	60

and i want to add constraint that for each pid(e.g. pid=1 then sum of per should not be more than 100)
how can i add this constraint.
constraint should be violete for pid=2 because sum of per is (30+40+60) is more than 100.
How can i do this
Thanks in Advance

What I have tried:

i want to add constraint that for each pid(e.g. pid=1 then sum of per should not be more than 100)
how can i add this constraint.
constraint should be violete for pid=2  because sum of per is (30+40+60) is more than 100.
How can i do this
Thanks in Advance
Posted
Updated 25-Jul-17 20:44pm
Comments
SriNivas IT 26-Jul-17 2:28am    
Pls look into the matter,
i was so disappointed :(

1 solution

Don't. There is a CHECK constraint, but it can't do complicated stuff like sum existing records and compare against the value.

There are three ways to do this:
1) Do it in your presentation software before you try to INSERT or UPDATE a row.
2) Do it in a stored procedure, instead of attempting an INSERT or UPDATE directly.
3) Use a TRIGGER to detect when the value exceeded a limit.

I'd prefer 1, then 2, and would use 3 with reluctance.
 
Share this answer
 
Comments
SriNivas IT 26-Jul-17 3:21am    
Thank you so much !!!
OriginalGriff 26-Jul-17 3:51am    
You're welcome!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900