sql - Create a Table to store shared attributes between tables, how to deal with Composite Keys? -


i'm looking database solution dealing scenario this:

a lot tables: tablea, tableb, tablec ... share attributes(fields), want store these shared attributes in table, let's call shared table. this:

tablea: | keya  | shared attr.1  | ... | other attr.   | | a_1   | a_svaluea1     | ... | a_ovaluea1    |  tableb: | keyb  | shared attr.1  | ... | other attr.   | | b_1   | b_svaluea1     | ... | b_ovaluea1    |  shared table: | keyshare  | entitytype | shared attr.1   | ... | | a_1       |          | a_svaluea1      | ... | | b_1       | b          | b_svaluea1      | ... | 

of course i'll create 1 table store specific attributes each table.

but need solve problem, table c, want store shared attributes in sharedtable, has composite keys, this:

tablec: | keyc1 | keyc2  | shared attr.1  | ... | other attr.   | | c1_1  | c1_2   | c_svaluea1     | ... | c_ovaluea1    | 

so can't deal tablea , tableb. there design deal composite keys in table c? i'm sure not new questions, lot people may have faced , solved it, searched lot , failed find existing questions.


in fact, scenario designed alternative of e-a-v antipattern, because tablea/b/c of same class, have shared attributes, , act subclass, have special attributes.

in class level, same things, can compared, sorted, calculated, designed placed in 1 table.

if want (and concur other answer bad[tm] idea) - turn solution upside-down:

don't store key particular entity in shared table, store reference new "shared entity" in particular table.

for example:

create table tablea (   id int primary key,    my_special_attr varchar,   shared_attr_set int references shared(attr_set_id) ); create table tablec (   id1 int,   id2 varchar(2),   my_special_attr varchar,   shared_attr_set int references shared(attr_set_id),   primary key (id1, id2) ); create table shared (   id int primary key,   attr_set_id int,   shared_attr_1 int,   shared_attr_2 varchar ); 

of course means you'll have cleanup after if remove instances of particular classes - you'll either need scan shared "orphaned" sets or delete them instance.

let me end word of warning: i've been there. messed up. recipe lot of headaches. consider making shared full entity , use composition on inheritance.


Comments

Popular posts from this blog

mysql - Dreamhost PyCharm Django Python 3 Launching a Site -

java - Sending SMS with SMSLib and Web Services -

java - How to resolve The method toString() in the type Object is not applicable for the arguments (InputStream) -