mysql - Foreign key pointing to different tables -
i'm implementing table per subclass design discussed in previous question. it's product database products can have different attributes depending on type, attributes fixed each type , types not manageable @ all. have master table holds common attributes:
product_type ============ product_type_id int product_type_name varchar e.g.: 1 'magazine' 2 'web site' product ======= product_id int product_name varchar product_type_id int -> foreign key product_type.product_type_id valid_since datetime valid_to datetime e.g. 1 'foo magazine' 1 '1998-12-01' null 2 'bar weekly review' 1 '2005-01-01' null 3 'e-commerce app' 2 '2009-10-15' null 4 'cms' 2 '2010-02-01' null
... , 1 subtable each product type:
item_magazine ============= item_magazine_id int title varchar product_id int -> foreign key product.product_id issue_number int pages int copies int close_date datetime release_date datetime e.g. 1 'foo magazine regular issue' 1 89 52 150000 '2010-06-25' '2010-06-31' 2 'foo magazine summer special' 1 90 60 175000 '2010-07-25' '2010-07-31' 3 'bar weekly review regular issue' 2 12 16 20000 '2010-06-01' '2010-06-02' item_web_site ============= item_web_site_id int name varchar product_id int -> foreign key product.product_id bandwidth int hits int date_from datetime date_to datetime e.g. 1 'the carpet store' 3 10 90000 '2010-06-01' null 2 'penauts r us' 3 20 180000 '2010-08-01' null 3 'springfield cattle fair' 4 15 150000 '2010-05-01' '2010-10-31'
now want add fees relate 1 specific item. since there little subtypes, it's feasible this:
fee === fee_id int fee_description varchar item_magazine_id int -> foreign key item_magazine.item_magazine_id item_web_site_id int -> foreign key item_web_site.item_web_site_id net_price decimal e.g.: 1 'front cover' 2 null 1999.99 2 'half page' 2 null 500.00 3 'square banner' null 3 790.50 4 'animation' null 3 2000.00
i have tight foreign keys handle cascaded editions , presume can add constraint 1 of ids not null.
however, intuition suggests cleaner rid of item_whatever_id columns , keep separate table:
fee_to_item =========== fee_id int -> foreign key fee.fee_id product_id int -> foreign key product.product_id item_id int -> ???
but can't figure out how create foreign keys on item_id since source table varies depending on product_id. should stick original idea?
update
the alternative considering is:
fee === fee_id int fee_description varchar product_id int -> foreign key product.product_id item_id int -> ??? net_price decimal
i'm not sure why mentioned separate fee_to_item
table (i guess thinking of else) doesn't change question since key point same: foo1_id+foo2_id+foo3_id
vs source_id+foo_id
i go 1 fk column , flag column. instead of
fee === fee_id int fee_description varchar item_magazine_id int -> foreign key item_magazine.item_magazine_id item_web_site_id int -> foreign key item_web_site.item_web_site_id net_price decimal
you'd have
fee === fee_id int fee_description varchar item_id int -> foreign key item_magazine/website.item_magazine/website_id product_type_id int -> foreign key product_type.product_type_id net_price decimal
then queries generalizable. instead of 2 different queries, e.g.:
select * fee item_magazine_id=x select * fee item_website_id=y
you like:
select * fee item_id=x , product_type_id=1 select * fee item_id=y , product_type_id=2
Comments
Post a Comment