4 ms·
Foreign keys does not correspond to class hierarchies in OOP. If anything, they correspond to object composition. E.g. you might have a Customer table and an O
by goto11 5y ago
Foreign keys does not correspond to class hierarchies in OOP. If anything, they correspond to object composition.
E.g. you might have a Customer table and an Order table with FK from Order to Customer. This would correspond to a Customer object holding a collection of Order objects - it would not correspond to Order being a subclass of Customer.
In any case, the relational model is not supposed to map directly to any particular programming model or paradigm. You should be able to interact with relational data from OOP, imperative, functional and declarative languages.
- yobbo 5y ago> Foreign keys does not correspond to class hierarchies If a foreign key is primary in two tables (1:1 relation), then it can correspond well to a hierarchical relation. In other cases, foreign keys can also correspond to composition as you say, and it depends on intention. An example of inheritance could be the table Parties with primary key Party_ID. The table Customers has primary FK Party_ID (Parties.Party_ID). If you must use ORM, you can implement this is as Customer extends Party. The class Customer then inherits whatever ability the class Party has to access further tables with FK Party_ID, ex something like PartyAddresses. This might make more sense if you also have a table Providers with primary FK Party_ID (Parties.Party_ID), and the table Orders with FKs provider_Party_ID (Providers.Party_ID) and customer_Party_ID (Customers.Party_ID). Normalizing like this allows you to have inheritance, polymorphism, and encapsulation in OOP.