Question

I am designing a datatable (-s) for storing the list of shareholders. In case a shareholder is a nominee, it discloses the list of share owners. These all need to have a pile of similar references. Thus, I would like to store all in one table. Nominees are registered shareholders and thus, they have an account number in the system, from which I get the data. Opposed to this, share owners that are coming from nominee disclosure have no their own account number, and share account number of the nominee.

I add a uniquifier to the table. I would like to assume that all registered shareholders and the nominees have 0 in this field. The disclosure list (non-registered share owners, that work via the nominee) have a value above zero in this uniquifier filed. Basically, a numbered list.

I need to know the nominee, to whom this disclosed share owner belongs. So, we are coming to a self-reference. It could be Ok, if not for the uniquifier field. It is illegal to make a foreign key on part of a primary key and assume that there is zero in the uniquifier field. So, could you please suggest a walkthrough.

To better explain, here is the excerpt:

Create Table Account (
  AccountId Int,
  Uniquifier Int,

  NomineeId Int,

  Constraint PK$Account Primary Key (AccountId, Uniquifier)     
)

What I'm trying to do is something like:

Alter Table Account 
  Add Constraint FK$SelfReference Foreign Key (NomineeId) References Account (AccountId)

which is illegal, since

There are no primary or candidate keys in the referenced table 'Account' that match the referencing column list in the foreign key 'FK$SelfReference'.

Any help is appreciated.

Était-ce utile?

La solution

You can add a computed column which will be 0 when the FK ought to be enforced and NULL otherwise. This can then be used in the foreign key reference:

Create Table Account (
  AccountId Int,
  Uniquifier Int,

  NomineeId Int,
  NomineeXRef as CASE WHEN Uniqueifier > 0 THEN 0 END persisted

  Constraint PK$Account Primary Key (AccountId, Uniquifier),
  Constraint FK$SelfReference Foreign Key (NomineeId,NomineeXRef) References Account (AccountId, Uniquifier)     
)
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top