I'm struggling to design my database. My database involves financial transactions of many kinds that are going to be used to calculate users' balances. Different transactions are different in structure and at the moment I'm storing them in different tables. When it comes to calculating user's balance I realized it was a big mess as I needed to select many different non-consistent rows from multiple tables and add them all up. So I thought what if I create a single table that would summarize the transaction (for example amount and user id who owns it) and reference a more detailed record and have a field to specify what kind of transaction it is. As it turned out: the thing that I was trying to implement is called 'polymorphic associations' and there's no way to implement it with MySQL in a reliable way. There are some workarounds but none of them guarantee the data integrity on the level I want it to be. In the end, I got so desperate that I started looking at other DBMSs but none of them seem to fit my case as well.
Do you have any recommendations? Perhaps, should I not be afraid of writing large and growing functions for calculating users' balances? Or should I go with the best option of modelling polymorphic associations in MySQL and leave a slight risk of data inconsistency? Or maybe there are DBMS that can solve my problem without workarounds?
The structure I have now
Some transactions are linked directly with the user, while others through another table. My aim is to calculate the sum of all the transactions for a single user, and it would be a really big and growing SQL statement to get all the rows for every single kind of transaction.
This is how I want it to be structured
That is I want to have a single table for easier calculations, but the table has to enforce that the record it presents exists.