问题描述
我只是想知道,如何在ER图中的ISA关系将转换为数据库中的表。
I was simply wondering, how an ISA relationship in an ER diagram would translate into tables in a database.
有3张桌子吗?一个人,一个为学生,一个为老师?
Would there be 3 tables? One for person, one for student, and one for Teacher?
还是会有两张桌子?一个用于学生,一个用于教师,每个实体具有个人的属性和他们自己的属性?
Or would there be 2 tables? One for student, and one for teacher, with each entity having the attributes of person + their own?
或者会有一个表包含所有4个属性,表中的正方形是空的,取决于它是否是学生或老师在行中?
Or would there be one table with all 4 attributes and some of the squares in the table being null depending on whether it was a student or teacher in the row?
注意:我忘记添加这一点,但有完全覆盖的ISA
NOTE: I forgot to add this, but there is full coverage for the ISA relationship, so a person must be either a studen or a teacher.
推荐答案
假设这种关系是强制性的(正如你所说, 是一名学生或教师)和不相交(一个人既是学生也是教师,但不是两者),最好的解决方案是有两张表,一张给学生,一张给老师。
Assuming the relationship is mandatory (as you said, a person has to be a student or a teacher) and disjoint (a person is either a student or a teacher, but not both), the best solution is with 2 tables, one for students and one for teachers.
如果参与是可选的(这不是你的情况,但让我们把它作为完整性),那么3表选项是要走的路, Person(PersonID,Name)表,然后是另外两个引用Person表的表,例如
Student(PersonID,GPA),PersonID为PK和FK引用Person(PersonID)。
If the participation is instead optional (which is not your case, but let's put it for completeness), then the 3 tables option is the way to go, with a Person(PersonID, Name) table and then the two other tables which will reference the Person table, e.g.Student(PersonID, GPA), with PersonID being PK and FK referencing Person(PersonID).
1表选项可能不是最好的方法,并且它将生成几个具有空值的记录(如果某人是学生,则只有教师的属性将为空,反之亦然)。
The 1 table option is probably not the best way here, and it will produce several records with null values (if a person is a student, the teacher-only attributes will be null and vice-versa).
如果不相交是不同的,那就是另一回事了。
If the disjointness is different, then it's a different story.
这篇关于实体关系图。 IS A关系如何转换为表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!