I'm new to working with relational databases especially nested set hierarchies. I have an existing bank hierarchy with bank branches as the "leaves" of the bank. I have hit a wall trying to establish a relationship between a user and a bank as well as a relationship between the same user and the type of account each branch has to offer. Each branch offers different types of accounts and each account has different levels of specification. (Ex. One branch might only have one level of account specification but another could have four levels). I have the tables I created below.
DESIRED RESULT: Ultimately, I just want to be able to run a select statement to retrieve all the accounts a chosen branch has to offer down to it's specification. I would also like to know what account a chosen user has as well as the bank they are connected to.
I don't want to add the account level data to the existing bank hierarchy because I feel that would complicate things even further. (My bank branches could end up having their own leaves in the future). What is it that I am missing below?
(Bank)Heirachy Table:
Top Level -"Global Bank"---> Level 2-"Global Bank-East Coast" && "Global Bank-West Coast"
I created a "Branch" Table with the following data:
+---------+---------+---------+----------+---------+-----------+
| Id | name | address 1 | telephone | city |
|
+---------+---------+---------+----------+---------+------------+
| 1 |GB-New York | East ST |212-555-9999 | NY |
|
| 2 |GB-California | West ST |619-555-5555 | CAL |
I then created a "branch membership" table that contains userId and what branch the user has an account with.
+---------+---------+---------+--------
| Id | userId | branchId |
|
+---------+---------+---------+---------
| 1 | 123 | 1 |
|
| 2 | 123 | 2 |
I would like to track what type of accounts each branch has so I created a "branch_accounts" table
+---------+---------+---------+----------+--+
| Id | account_type | branch_id |
|
+---------+---------+---------+------+
| 1 |Checkings | 1 |
| 2 |Savings | 1 |
| 3 |401K | 2 |
Here is where things get complicated. I created tables for each level of account types. For example a Checking account could be free checking account but a free checking account could either be a student account or not. A Business Checking could be for small businesses or big businesses...etc. All account type have up to 3 specific levels.
Account_Level 2 Tables
+---------+---------+---------+------------------------+
| Id | name | account_type_id |
|
+---------+---------+---------+-------------------------+
| 1 | Free Checking | 1 |
|
| 2 | Business Checking | 1 |
Account_Level 3 table
+---------+---------+---------+------------------------+
| Id | name | account_lvl2_ID |
|
+---------+---------+---------+-------------------------+
| 1 | Small Business Checking | 2 |
|
| 2 | Big Business Checking | 2 |
Please let me know how to establish my relationships to get the desired result above.