I’m having a hard time figuring out the best way to organize a database. There are three tables relevant to the problem at hand: user, good, and transaction. In an ideal scenario, each transaction would comprise an identifying relationship with a single user and a single good. But what is the best way to handle multiple goods? (In my case, a user might purchase hundreds of different goods in a single transaction.) Should I simply create a comma-separated list and stick it in a TEXT field? This seems to go against conventional wisdom for database design…
I’m having a hard time figuring out the best way to organize a database.
Share
You need these tables and columns in addition to ones you’ve listed:
TRANSACTION_GOOD_MAPPING:
Trnsctn_good_id (pk), transaction_id (fk to transaction table), good_id (fk to good table)
USER_TRANSACTION_MAPPING:
user_trnsctn_id, user_id (fk to user table), tramsaction_id (fk into transaction table)
The above will allow you to have users with many transactions and transactions with many goods.
Here is a sample query to get a list of goods for a given transaction of a given user:
select good_id, good_name, good_desc
From user u, good g, transaction t,
Transaction_good_mapping tgm, user_tramsaction_mapping utm
Where u.user_id=utm.user_id
And utm.transaction_id=tgm.transaction_id
And g.good_id=tgm.good_id
And u.user_id=’someuserid’
And tramsaction_id=some_tramsaction_id
Note: for the last two lines in the above query make sure you use variable substitution in the query to avoid SQL injection.