如果您的 RDBMS 是SQLServer
,那么我将按照 podiluska 的联合线抽象一个视图,将 Student 和 Teacher 中的特定字段映射到通用名称,并在无法映射的情况下使用 NULL 填充
并假设学生和教师从人继承(即两者都是 0..1 到 1 与人),那么他们可以共享相同的主键,即不需要教师和学生的新代理键。
我假设这person.type
决定了这个人是学生(S)还是教师(T)。
CREATE VIEW SubClassesOfPerson AS
SELECT p.id as PersonId,
p.name as PersonName,
p.OtherBaseFieldsHere,
s.SomeStudentSpecificField AS MappedField1,
s.SomeStudentSpecificFieldX AS MappedFieldX,
s.SomeStudentSpecificField as MappedFieldForStudentOnly,
NULL as MappedFieldForTeacherOnly -- Pad this because it can't be mapped
FROM person p
INNER JOIN student s
on s.person_id = p.id AND p.type = 'S'
UNION
SELECT p.id as PersonId,
p.name as PersonName,
p.OtherBaseFieldsHere,
t.SomeTeacherSpecificField AS MappedField1,
t.SomeTeacherSpecificFieldX AS MappedFieldX,
NULL as MappedFieldForStudentOnly, -- Pad this because it can't be mapped
t.SomeTeacherSpecificField as MappedFieldForTeacherOnly
FROM person p
INNER JOIN teacher t
on t.person_id = p.id AND p.type = 'T'