Your table 3 should be for all your users who have bought stocks. It will be what is known as a conjunction table which links between the users table and the stocks table. It will have two main keys, one that is the user's account id and the second which is the stock's id. This will allow you to form a many to many relationship which will allow users to purchase as many different stocks as they want and provide the flexibility for things like a person buying the same stock at two different prices.
Users
1 - User1
2 - User2
3 - User3
stocks
1 - GM
2 - Motorola
3 - Microsoft
Portfolio
id.... userid....stockid.....stockprice....shares
1 1 3 34.32 40
2 3 3 35.15 100
3 2 1 10.00 20
4 1 3 35.10 60
This above data shows that user with id 1 owns 100 shares of Microsoft (40 at 34.32 and 60 at 35.10) and that user 3 has shares of microsoft also 100 shares right at 35.15. User 2 has stock in GM, 20 shares at 10.00.
So how do you get the portfolio for user 1? Simple, you query the portfolio and use userid 1
CODE
select stocks.name, portfolio.stockprice, portfolio.shares
from stocks, portfolio where stocks.id = portfolio.stockid and portfolio.userid = 1
I hope this makes sense. Enjoy!
"At DIC we be stock trading code ninjas! Except being that we are secretive, we be like Enron also."