Entity Relationship Diagram. How does the IS A relationship translate into tables?

我的梦境 提交于 2019-12-09 08:28:42

问题


I was simply wondering, how an ISA relationship in an ER diagram would translate into tables in a database.

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?

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?

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.


回答1:


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.

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).

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.




回答2:


there are 4 options you can use to map this into an ER,

option 1

  • Person(SIN,Name)
  • Student(SIN,GPA)
  • Teacher(SIN,Salary)

option 2 Since this is a covering relationship, option 2 is not a good match.

  • Student(SIN,Name,GPA)
  • Teacher(SIN,Name,Salary)

option 3

  • Person(SIN,Name,GPA,Salary,Person_Type) person type can be student/teacher

option 4

  • Person(SIN,Name,GPA,Salary,Student,Teacher) Student and Teacher are bool type fields, it can be yes or no,a good option for overlapping

Since the sub classes don't have much attributes, option 3 and option 4 are better to map this into an ER




回答3:


It depends entirely on the nature of the relationships.

IF the relationship between a Person and a Student is 1 to N (one to many), then the correct way would be to create a foreign key relationship, where the Student has a foreign key back to the Person's ID Primary Key Column. Same thing for the Person to Teacher relationship.

However, if the relationship is M to N (many to many), then you would want to create a separate table containing those relationships.

Assuming your ERD uses 1 to N relationships, your table structure ought to look something like this:

CREATE TABLE Person ( sin bigint, name text, PRIMARY KEY (sin) );

CREATE TABLE Student ( GPA float, fk_sin bigint, FOREIGN KEY (fk_sin) REFERENCES Person(sin) );

and follow the same example for the Teacher table. This approach will get you to 3rd Normal Form most of the time.



来源:https://stackoverflow.com/questions/18992653/entity-relationship-diagram-how-does-the-is-a-relationship-translate-into-table

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!