I'm creating a feature that has an option to create infinite bank account transactions by month, for example: A user buy something in 12x parcels, will be created 12 rows on database with relation with this purchase in a table called bank_account_transactions.
Now the problematic scenario: A user has a salary that increase the balance of bank account every month for the rest of his life, how can i store this in database?
I thought store data equivalent to 100 years (12*100=1200) but it is so slow because when the user update the value of his salary, will update 1200 rows in database.
How can I solve this in a performatic way? The project is developed with Nodejs and MongoDB.
Related
i need to create a complex trigger in my database.
I try to explain my problem:
In my database I have 3 tables: Cash table, invoice table and installment payment table. Now I need to create a trigger. When I enter a payment in the collection table, the trigger should divide the amount entered by covering the uncovered amounts of the invoices (invoice table) starting from the oldest invoice due date. Furthermore, if installment payments are envisaged, I would need to divide the amount also on any installments starting from the oldest installment due date.
like this function: https://andrewtuerk.wordpress.com/2013/01/17/splitting-allocations-across-multiple-records-in-a-single-statement/
or this: SQL Server Allocation of amount in bill
How can I activate a trigger when I insert row in table?
Many thanks,
Andrea.
I was wondering how people are securely storing money balances in a database. E.g how do you make sure that the database administrator does not modify balances or transactions? How do you make sure that the code that does a transaction not accidentally or intentionally by a rogue employee does not work correctly.
Banks, PayPal and any other apps that hold balances in USD or any other currency should have this problem
All the banking packages I've worked with store the balance with the account entity. Calculating it on the fly from movement history is unthinkable.
The right way is:
The movement table has an 'opening
balance' transaction for each and every account. You'll need
this in a few year's time when you
need to move old movements out of the
active movement table to a history
table.
The account entity has a balance
field
There is a trigger on the movement
table which updates the account
balances for the credited and debited accounts. Obviously, it has commitment
control. If you can't have a trigger, then there needs to be a unique module which writes movements under commitment control
You have a 'safety net' program you
can run offline, which re-calculates
all the balances and displays (and
optionally corrects) erroneous
balances. This is very useful for
testing.
Some systems store all movements as positive numbers, and express the credit/debit by inverting the from/to fields or with a flag. Personally, I prefer a credit field, a debit field and a signed amount, this makes reversals much easier to follow.
Notice that these methods applies both to cash and securities.
Securities transactions can be much trickier, especially for corporate actions, you will need to accommodate a single transaction that updates one or more buyer and seller cash balances, their security position balances and possibly the broker/depository.
From smirkingman answer on Database design: Calculating the Account Balance
I have payroll system with attendance log-in and log-out, am and pm.
These are the tables:
DTR: list of all emloyees with there log-ins and log-outs for the day
Date Empid timein_am timeout_pm timein_pm timeout_pm status tothrslate
12-4-13 1000 7:00 12:00 1:00 5:00 log-out 0
dtrlogs:
Columns are equal with the dtr tables. The attendance above will be moved here after the employee made an attendance for the day.
This table is list of all attendance of all employees everyday.
After the records of dtr table will be moved here, the time-ins and time-outs status, tothrslate will be deleted for the next day.
Questions:
Is it a good idea to have a dtrlogs in my database schema?
In any sense, is there anyway to do this for my attendance?
Btw, my client application is vb6 and I am using msaccess. Im just having a hard time designing database for payroll with attendance. I want to record all attendances of an employee everyday in a table. I was thinking to store attendances by months, meaning, I will have to create tables Jan-Dec. But I don't know if this is a good idea. Any advice? Any help would be apreciated.
The first thing I noticed was that your DTR table is very restrictive. What if a boss decides that everyone needs to clock out when they take smoke breaks? With your DTR table, the employee can only clock in and out once in the afternoon.
You also need an Employee table with the employee's ID, name, phone number, emergency contact, etc.
I recommend 1 source table to hold your permanent data. It will never be deleted. Give it 3 fields:
EmpID
Clock_In
Clock_Out
This allows multiple 'work periods' during the day, including if someone skips lunch completely. From this 1 table, you can schedule a nightly process that will clock out people who forget, build daily and monthly summaries, etc.
I also imagine this is a fairly common task. Try googling it.
Let's say I have an app (a Monopoly game) with many users, where each user has a balance, and they make transactions between them.
I have a table with all the users (userID, name, email) and a table with all the transactions and the users who made them (user1, user 2, amount).
Now if I need to present a balance for each user, would it be better to keep the balance for each user in the users table and update it every time a transaction is made, or should I calculate it from the transactions table every time I wont to present it? Assume this game will have a lot of traffic, so what is a more reasonable approach for 10,000+ users and about 120,000+ transactions per week?
Having a new column balance in the user table is a redundancy beacause the value could be calculated with a query on the transactions table.
However one of the DB design process is the estimation of how many read and writes could happen on a particular table. If this estimation is high it's logic and permitted to simplify the result of some read or write and have a redundancy on the DB to afford the many reads requested by the web pages and display values with less processing time.
In your case the balance column update could be done with a trigger on the transactions table that calculate the balance for the two users involed.
Option 1) user can have multiple "accounts" (eg payment, deposits, withdraws etc)
Option 2) user can only 1 single account, and transaction has types (payment, deposit, withdraw)
Both option will work just fine! They both can produce the same result! However option 1 uses more resources, but it's more flexible, option 1 is not flexible but uses less resources!
What is the question ?
Option 1 is a piece of garbage, that no accountant can use, and no auditor will pass. payment, deposits, withdraws eta re transactions, not "accounts". So what if it uses less resources. SO do cave men.
Option 2 starts to look like an accounting system, with (a) accounts and (b) transactions against accounts, as expected in most developed countries.
So there is no choice.
Start from a journal table, that link to a chart of accounts table. In your journal table is where you will store fieldnames for transaction date, account code, description, amount. chart of account table is where you will store fieldnames like account code, account type (balance sheet or profit and loss),account status (active or inactive). For greater details on the accounting database schema, download derek liew's book on accounting database design.