简体   繁体   中英

mapping generalization constraints to sql (STI approcach)

I'm trying to model the following relationships between entities, mainly consisting of a partial, disjoint generalization.

original EERD

1

'mapped' to relational

2

Since I didn't need the subclasses to have any particular attributes I decided to use the "single table inheritance" approach, added the "type" field and moved the relationships towards the parent. After that I had two choices to make:

1- type for the "business type" attribute

2- way to constraint participation to at most one of the 4 relationships based on the type attribute

For the sake of portability and extensibility I decided to implement no.1 as a lookup table (rather than enum or a hardcoded check).

About no.2 I figured the best way to enforce participation and exclusivity constraints on the four relationships would be a trigger.

The problem is that now I'm not really sure how to write a trigger function; for instance it would have to reference values inserted into business type, so I'd have to make sure those can't be changed or deleted in the future. I feel like I'm doing something wrong so I wanted to ask for feedback before going further; is this a suitable approach in your opinion?

I found an interesting article describing a possible solution: it feels a bit like an 'hack' but it should be working (it's intended for SQL Server, but it can be easily applied in postgres too). The only issue is that it requires a whole column for every subclass, each containing the same generated value repeated over and over.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM