Best Practice: mutually exclusive subtype pattern¶
If you have subtypes in your logical data model, and the pattern is mutually exclusive, you need to add a set constraint.
In the following figure, you see that a Customer is either a Person or a Company.
You need to know which values determine which subtype an instance belongs to. There are several ways to do this.
Suppose we add an attribute customer_type to differentiate between the two customer types. The possible values are 'Person' or 'Company'. To check if there are only Persons in the subtype Person, you need to create a Business Rule Helper and add the following query:
WITH QRY AS
(
SELECT C.[nbr]
, C.[customer_type]
, C.[acm_rownumber]
FROM [customer] AS C
LEFT OUTER JOIN [person] AS P
ON P.[nbr] = C.[nbr]
WHERE ((C.[customer_type] = 'Person' AND P.[nbr] IS NULL)
OR (C.[customer_type] = 'Company' AND P.[nbr] IS NOT NULL))
)
The query:
- Does a left outer join between Customer and Person.
- It checks if customer_type in
Customeris equal to 'Person' and the primary key ofPersonis empty. If this is the case, than there is a 'Person' record in Company that is not in the subtypePerson. - It checks if customer_type in
Customeris equal to 'Company' and the primary key ofPersonis not null. If this is the case, than there is a 'Company' record inPerson.