npertuset Posted May 25, 2011 Share Posted May 25, 2011 Ok. I'm trying to figure out the best/most optimized way to design my tables. I have multiple purchases go through my website. For each purchase, a percentage is given to different places (the user, company profit, and a bonus pool - more needs to be able to be added at any time). I want to keep track of all of this, be able to trace every transaction, pull up transactions at will and know the balance of each account (company profit and bonus pool will only have 1 account, but there could be 50-10,000 users). One way I've thought of is to create an "account type" for each user. IE: Table: Type_Of_Accounts (this table holds the option for different payable accounts) id (primary key - auto increment) name Examples: 1 / user 2 / company_profit 3 / bonus_pool Table: Transaction_Log (this table holds all transaction information.) id (primary key - auto increment) type_of_account_id (matches Type_Of_Accounts) account_id (if it's a user account, this field will hold the user id. if not, it will be set to 0). amount time Examples: 1 / 1 (user account) / 40 (user_id) / 59.99 / <time> 2 / 3 (bonus pool) / / 19.99 / <time> Table: balances (this would just update each time a transaction went through, this way I don't have to calculate it on the fly each time). id type_of_account_id account_id balance Examples: (using data from above) 1 / 1 / 40 / 59.99 2 / 2 / / 19.99 Here are some example queries: Get a users transaction log: select * from Transaction_Log where type_of_account_id='1' AND account_id='x' Get the transaction log for the bonus pool: select * from Transaction_Log where type_of_account_id='3' and account_id='0' To get the balance of the companies profit you could do: select balance from balances WHERE type_of_account_id='2' AND account_id='0' ----------------- What are your thoughts on this, and do you have a better way of designing this? I want it to be optimized/designed the best way possible. Quote Link to comment https://forums.phpfreaks.com/topic/237397-database-design/ Share on other sites More sharing options...
The Little Guy Posted May 25, 2011 Share Posted May 25, 2011 I would create a separate table for each. It will be easier to manage. Quote Link to comment https://forums.phpfreaks.com/topic/237397-database-design/#findComment-1220068 Share on other sites More sharing options...
npertuset Posted May 25, 2011 Author Share Posted May 25, 2011 like this? [attachment deleted by admin] Quote Link to comment https://forums.phpfreaks.com/topic/237397-database-design/#findComment-1220217 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.