I'm having a problem on how to design a table that must have a Primary Key which is a foreign key to two mutually exclusive tables.
+----------------+ +-----------------+
| PARTY | + PERSON +
+----------------+ +-----------------+
| Unique ID (PK) | -|------O|-| Unique ID (FK) |
| Party Type | | some details |
| | +-----------------+
| |
| | +-----------------+
| | | BUSINESS |
| | +-----------------+
| | | Scheme Code (PK)|
| | -|------O|<| Serial No (PK) |
| | | Unique ID (FK) |
| | | some details |
+----------------+ +-----------------+
+-----------+ +-----------------+
| PERSON | -|-----------O|<| BANK ACCOUNT |
+-----------+ +-----------------+
| Account No (PK) |
+-----------+ | Unique ID (FK) |
| BUSINESS | -|-----------O|<| Scheme Code (FK)|
+-----------+ | Serial No (FK) |
| some details |
+-----------------+
There are 4 entities. A PARTY can be a PERSON or BUSINESS (but cannot be both at the same instance). A PERSON can have one or more BANK ACCOUNT and a BUSINESS can also have one or more BANK ACCOUNT.
For a PERSON, only 1 Unique ID can be assigned.
For a BUSINESS, it can share the same unique PARTY id given it has a unique combination for (Scheme Code, Serial No, and Unique Id)
PARTY's primary key is 'Unique ID'
PERSON's primary key is 'Unique ID'
BUSINESS' primary keys are 'Unique ID', 'Scheme Code', and 'Serial No'
BANK ACCOUNT's primary keys are a combination of all they keys of PERSON and BUSINESS with 'Unique ID' as the only common foreign key and 'Scheme Code'/'Serial No' is a foreign key exclusive only from BUSINESS.
I thought the problem would be in the BANK ACCOUNT entity. If the PARTY is a BUSINESS, there will be no problem since all primary/foreign keys are populated (Unique ID, Scheme Code, Serial No). However, if a PARTY is a PERSON, only Unique ID will be populated and the other two keys will have NULL values. And I think NULL primary keys are not allowed.