When to use ternary relationship instead of aggregation in RDBMS?

不羁岁月 提交于 2020-07-06 20:19:49

问题


I was wondering when one would represent a relationship between an entity set and a relationship with a ternary relationship? I understand the benefit of aggregation, but why use it if there is no attribute in the relationship between the entity set and the relationship set? For instance, a grad student (with a student # and name) works on a project (with pid, start date, and end date) and each project that a student works on has a supervising professor. Every project must have only one professor as supervisor. The supervisor relation has no attribute that is unique yet I have been told that aggregation should be used, but why?


回答1:


Aggregation is not just in case there is an attribute in the relationship, ternary is more restrictive in some cases, ill explain with an example:

Let's say you have a small database used for scheduling classes in various dates and assign teachers to those classes at those dates, using ternary relationship it would look something like that:

ERD: https://i.stack.imgur.com/8FQ87.png

CREATE TABLE teacher
{
  teacher_id int PRIMARY KEY
}

CREATE TABLE class
{
  class_id int PRIMARY KEY
}

CREATE TABLE date
{
  date_id int PRIMARY KEY
}

CREATE TABLE teaching_class_in_date
{
  date_id int,
  class_id int,
  teacher_id,
  constraint teaching_class_in_date_PK PRIMARY KEY (date_id,class_id,teacher_id)
  constraint teacher_FK FOREIGN KEY (teacher_id) references teacher (teacher_id)
  constraint class_FK FOREIGN KEY (class_id) references class (class_id)
  constraint date_FK FOREIGN KEY (date_id) references date (date_id)
}

This means that when you want to schedule a new class with a teacher you'll need all three records in teacher, class and date beforehand. but what if you want to assign first a class at a certain date but not sure about the teacher yet ?

you have 2 options, one which I usually see in a messy enterprises databases is to create a dummy record in teacher table to allocate to each new class at a certain date if you're not sure about the teacher yet, and it will show in the GUI as "default", or "empty". Option two, make the teacher_id not part of the primary key and allow null values to the teacher_id foreign key.

Both are bad decisions, the second is worse as it breaks the database third normal form.

To fix this you can use aggregation like this:

ERD: https://i.stack.imgur.com/wAEXF.png

So now you'll have an extra table:

CREATE TABLE class_in_date
{
  date_id int,
  class_id int,
  constraint class_in_date_PK PRIMARY KEY (date_id,class_id)
  constraint class_FK FOREIGN KEY (class_id) references class (class_id)
  constraint date_FK FOREIGN KEY (date_id) references date (date_id)
}

Now you can assign the two without worrying about the teacher. it all comes down to requirements, If I didn't create the requirement to assign a class at a certain date without a teacher before hand, then a ternary relationship would have been sufficient.

Hope this helped !




回答2:


Case 1, Binary ownership each project has only one student and one prof (1,1) (Ternary on project)

Student(sid,..)
Professor(pid,..)
Project(pid,sid,..)

Case 2, Each project has more than one student and one prof, each student has one project. (1,N) (Ternary on project aggregating student)

Student(sid,pid,...)
Professor(pid,..)
Project(pid,..)

Case 3 each project may have more than one student and each student may have more than one project (N,N) (aggregate)

Student(sid,...)
Professor(pid,..)
Project(pid,..)
StudentProject(pid,sud) //One entry for each student working on a project

If you've been told to use an aggregate for an entity without an (N,N) relationship, this might be a form of future-proofing.

The difference lies in what table you should store the relationship to project.



来源:https://stackoverflow.com/questions/36381446/when-to-use-ternary-relationship-instead-of-aggregation-in-rdbms

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