Database Design – ERD for Yearly Budget Ceiling Advice

database-designerd

I'm working on webdev that involving yearly budget ceiling, but I can't wrap my head around to design such budget ceiling (my first project involving budgeting). So I need some advice.

So the scenario is every year there's will be a budget ceiling for a program, where the program budget will be divided into derivatives such as activities, sub-activities, and purchase details that cannot exceed the budget of each parent.

Scenario Example:

Scenario Example.

ERD Example
ERD Example

I know that my design doesn't make sense for the budgeting part, because i need to know how to make the budget relation properly. Also is that possible if im using Trigger to sync when im inserting budget to the purchase detail (the lowest in hierarchy) and the parents will get the total of the children budget values?

Best Answer

Honestly this looks like a classic hierarchical problem that can usually be solved with a single table of each unique item and a separate self-referencing table that holds each parent-child relationship. This is a common problem in the manufacturing industry but evidently applicable even in budgeting use cases.

So you have a series of items that are all similar in object definition but just differ in hierarchy, a Program, Activity, Sub-Activity. Rather than defining separate tables for each, you should have a single table perhaps called BudgetItems, that would store the unique list of each instance of those objects. That table can have a separate field that identifies the type of object it is, BudgetItemType. (It would have it's Id and Name columns as well.)

Then you should have a table that stores every possible parent-child relationship between all BudgetItems, perhaps called BudgetItemHierarchy (or whatever better name you find appropriate). It would have two columns, BudgetItemParentId, and BudgetItemChildId, both foreign key references to your BudgetItems table. There would also be a BudgetAmount column that corresponds to the amount budgeted for the BudgetItemChildId.

Having these two tables in place will allow you store any number of budget objects and budget object types, without having a number of redundant tables. Then in most modern relational database management systems, you can use recursion, such as a recursive CTE (note this resource is Microsoft SQL Server specific, but is generally available in other database systems) to calculate any list of BudgetItems for any subset or full set of their hierarchy with all their individual BudgetAmounts at each level, and even the rolled up sum across any number or all levels.

Related Question