Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Table Design concerning forgien keys

Author  Topic 

coopervegas
Starting Member

2 Posts

Posted - 2011-03-26 : 18:11:09
This may be a basic question; I am fairly new to SQL.

I have two tables 'Players' and 'Company'. Each has a primary key 'PlayerID' and 'CompanyID' respectively.

I want to create a new table called 'Company Ownership' that would keep track of who owns shares of stock of a company.

My problem is that I want both players AND companies able to own stock.

For example:
Player1 owns 100 shares of CompanyA
Player1 owns 150 shares of CompanyB
Player2 owns 175 shres of CompanyA
Player2 owns 200 shares of CompanyB
CompanyA owns 500 shares of CompanyB

I'm not sure how to structure the 'Company Ownership' table where there is a multi-column primary key, one column for 'company' one for 'playerid_owner' and one for 'companyid_owner'. I guess I could make the player_id_owner zero/null when a companyid owns it and vice versa, but I'm thinking there is a better solution.

Thanks for any help.

coopervegas
Starting Member

2 Posts

Posted - 2011-03-26 : 19:03:48
I've been looking but I just don't see any SQL mechanic for my problem; maybe I need to redesign my database.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-03-27 : 01:13:53
what you could do is create two columns company_id or player_id that corresponds to company or player that owns stock. then create a check constraint to check if either one of them is not null. then add a new id as identity and make it pk.

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -