Polymorphysm in relay OBD. Is it possible?



  • There is a database (postgresql) containing three tables -
    People (ID, Name, Name, Destination, Name)
    Organizations (ID, Name, Address, Estimate),
    Motor vehicles (ID, Marka, Model, Probe, owner).

    Problem:
    Linkages must be established between cars and owners. The owner of the vehicle may be both a person (the list in the table) and an organization (the list in the table of the Organization).

    Question:
    How can this link be implemented? Could the outer key be polymorphous and, therefore, how during JOIN to know what table would be combined (how to store type information on the outside key)?

    PS:
    I apologize earlier for perhaps a stupid question, and please take into account the fact that SQL and RBBB have met as such, and there is an urgent need to address this issue within a short time frame.

    Update:
    In the first version of the question, the general field - Name - if the owner is a man, his name is Ivanov I.I.
    As an example, I'll give you the fiction code, I think it'll be clearer:

    SELECT Авто.Марка, Авто.Модель, Наименование FROM Авто
    INNER JOIN Авто.Внешний_ключ_владельца.Таблица ON Авто.Внешний_ключ_владельца.ID = Авто.Владелец.ID
    

    And the possible result:
    "Daewoo" "Nexia" "Ivans."
    "Ford" "Focus" "Oh, TRADE."
    "Schevrolet" "Camaro" "Sidors S.V.."



  • The data structure should be such that its requests are as simple as possible. If you only need a name from the owner in your request, there's no point in sharing people and organizations. This should be one table of the owners. The extra fields can be both in the same table and in the subsidiaries of "data yurls"/data physicals. It depends on requests, compare your option:

    select cars.*, persons.fullname from cars 
      join persons on cars.owner = persons.id and cars.isorg = 0
    union all
    select cars.*, orgs.fullname from cars 
      join orgs on cars.owner = orgs.id and cars.isorg = 1
    

    and request with one table:

    select cars.*, owners.fullname from cars join owners on cars.owner = owners.id 
    

    If you need all the extra fields on this request (it's still a good idea how to put them in the same table) you'll get the same data: they'll be null for people in the address and the account and null for the organizations.

    If you're planning a more complex logic of requests (which you have not indicated on the question) go away from her. And go straight.




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2