问题
I have the below UML class diagram with Abstract Class, and sub-Classes that extends from it. and i want to make an ER diagram using this class diagram.
My question is how can i represent the Abstract class in ER diagram ? as a Table ? or should i just ignore it ?
Thank you.
回答1:
There are basically three choices to translate generalization into a database model
1. One table per concrete class
Create tables Admin
, Teacher
and Student
. Each of these table contain columns for all of the attributes and relations of User
- Pro
- All fields of a concrete subclass are in the same table, so no join needed to get all Student data
- Easy data validation constraints (such as mandatory fields for
Student
)
- Con
- All fields of
User
are duplicated in each subclass table - Foreign keys to
User
have to be split into three FK fields. One forAdmin
, one forTeacher
and one forStudent
.
- All fields of
2. On table for whole generalization set
In this case you just have one table call User
that contains all fields of User
+ all fields of all sub-classes of User
- Pro
- All fields are in the same table, so no join needed to get all
User
data - No splitting of FK's to
User
- All fields are in the same table, so no join needed to get all
- Con
- There are a bunch of fields that are never used. All fields specific for
Student
andTeacher
are never filled in forAdmins
and vice versa - Data validation such as mandatory fields for a concrete class such as
Student
become rather complex as it is no longer a simpleNot Null
constraint.
- There are a bunch of fields that are never used. All fields specific for
3. One table per concrete class, and one for the superclass
In this case you create tables for each of the concrete sub-classes and you create a table for the class User
. Each of the concrete sub-class tables has a mandatory FK to User
- Pro
- Most normalized schema: No repeated fields for the attributes of user, and no unused fields.
- No splitting of FK's to
User
- Easy data validation constraints (such as mandatory fields for
Student
)
- Con
- You have to query two tables if you want all data of a
Student
- Complex validation rules to make sure each
User
record has exactly oneAdmin
,Teacher
orStudent
record.
- You have to query two tables if you want all data of a
Which one of these options you choose depends on a number of things such as the number of sub-classes, the number of attributes in either subclass or superclass, the number of FK's to the superclass, and probably a few other things I didn't think about.
回答2:
An abstract class can not be instantiated. That means you can not represent it as a table. You can only instantiate concrete classes (as table).
If you use foreign keys in e.g. Admin
and Teacher
to refer to User
you'd be in trouble since they are different. They will likely have different user/password and the activation is also different even it were the same real person with the same name.
来源:https://stackoverflow.com/questions/57828990/abstract-class-from-uml-to-er-diagram-possible-how