sql server - Linking an address table to multiple other tables -
i have been asked add new address book table our database (sql server 2012).
to simplify related part of database, there 3 tables each linked each other in 1 many fashion: company (has many) products (has many) projects , idea 1 or many addresses able exist @ 1 of these levels. thinking in front-end system, user able view , select specific addresses project specify , more generic addresses relating parent product , company. issue if how best model in database.
i have thought of 2 possible ideas far wonder if has had similar type of relationship model , how implemented it?
idea one: new address table additionally contain 3 fields: companyid, productid , projectid. these fields related relevant tables , nullable represent company , product level addresses. e.g. companyid 2, productid 1, projectid null product level address. issue storing relationship information in table if project ever changed related different product, data in table incorrect. potentially null level interested in make getting parent addresses little harder get
idea two: on address table have typeid , genericid. genericid contain ids company, product , project tables typeid determining table came from. little stuck how set necessary constraints though , wonder if going tricky deal in future
many thanks,
i suggest using idea 1 , preventing idea two.
second idea called polymorphic association anti pattern
objective: reference multiple parents
resulting side effect: using dual-purpose foreign key violating first normal form (atomic issue), loosing referential integrity
solution: simplify relationship
the simplification of relationship obtained in 2 ways:
- having multiple null-able forging keys (idea number 1): simple , applicable if tables(product, project,...) using relation limited. (think when grow more)
- another more generic solution using inheritance. defining new entity base table (product, project,...) satisfy addressable. may naming organization-unit more rational. primary key of organization_unit table primary key of (product, project,...). other collections address, image, contract ... tables have relation base table.
Comments
Post a Comment