i have an Applications table and a Resources tables. Since many resources can work on a single application and many applications can be worked on by a single resource, i created a 3rd table:
ApplicationResources:
– ID (int)
– ApplicationID (int)
– ResourceID (int)
– PctOfTime (float)
is there anyway in the database itself that i can ensure that the total of all resources’s PctOfTime added up to 100%?
There’s no declarative way to do that in SQL.
You can accomplish this using a trigger (a stored procedure that is run whenever a row in the ApplicationResources table is INSERTed, UPDATEd or DELETEd. The trigger can test the condition and reject the change if it would cause the condition to be false.
The problem is that you might need to allow the table to be briefly invalid. For instance, if you DELETE a record for a resource, the condition will not be true until you add or update one or more records to get back to 100%. In order to get that to work, you’d need to defer your trigger until the end of a transaction and the ability to do that varies among databases.
Your problem is easier to deal with if you want the condition to be that no resource can ever be more than 100% allocated (and that allows for resources that are not fully utilized as well).