Reputation: 4898
I'm having trouble seeing the best way to set up a database for a simple bank with customers that have multiple accounts. I imagine a basic table as below, to start with:
Customers
But what do we put in the Accounts field? It seems that the thing that needs to go here is a pointer to a complete table, like Customer_1_accounts
, Customer_2_accounts
, etc.
How is this normally handled?
Upvotes: 2
Views: 3290
Reputation: 6147
Customers table:
CustomerID, Address, Phone
Accounts table: AccountID, CustomerID, etc
The Accounts table keeps track of information about the account including who the owner of the account is. The Customer table keeps track of information about the customer. Whenever you have a many to one relationship such as with accounts and customers, the many side (accounts) creates a pointer to the one side (customers)
Upvotes: 1
Reputation: 85056
You probably want something like this:
Customers
-----------
Id
Address
Phone
Account
-----------
Id
Whatever else
CustomerId
This allows customers to have multiple accounts, but accounts can only have one customer. If you want to allow accounts to have multiple customers (my wife and I share an account for instance) and customers to have multiple accounts then you will need a third table:
Customer
--------
Id
Address
Phone
Account
---------
Id
Whatever else
CustomerAccounts
---------------
CustomerId
AccountId
Under no circumstances should you try to stick information about multiple accounts in a single column in the customers table. This will make your life a living hell.
Upvotes: 3