sql - One-to-One Relationship between tables that can't share primary key -
scenario: each "user" has 0 or 1 "bankaccount". each "company" has 0 or 1 "bankaccount". "bankaccount" records can not shared. how setup database model this? tried this:
"companybankaccount" table "bankaccountid" (primary key) foreign key "bankaccount" table , column "companyid" foreign key "company" table.
"company" table nullable "bankaccountid" column foreign key "companybankaccount" table. followed same pattern user tables.
while works (enforces rule no company or user can have more 1 bank account , no bank account can shared user or company), creation of new company or user bank account cumbersome (i must first insert company/user null bankaccountid, insert new bankaccount record, insert new [company/user]bankaccountrecord, update "bankaccountid" field on company/user record inserted). seems there should easier way. have more elegant solution?
it might easier omit bankaccountid column in company , user tables-- index join table companyid or userid , can pull bankaccountid when need it.
Comments
Post a Comment