Ankit Suhail
Ankit Suhail

Reputation: 2105

is this database designed correctly?

I appeared for an interview which went quite well according to me but not according to the interviewer. He asked me several questions which I answered correctly and 2 practicle questions. One of them was related to database.

The question was something like this

A school consist of several classes and each class studies different subjects which are taught by several teachers. You have to design the database so that one can know that which teacher teaches what subject to what class?

It appeared quite simple and I designed it.

something like:

Teacher table

|  ID    |  Teacher_Name    |
-----------------------------
|   1     |   Ankit         |
|   2     |   Jack          |

Class table

    |  ID    |  class_Name      |
    -----------------------------
    |   1     |   First         |
    |   2     |   Second        |

Subject table

    |  ID    |  subject_Name    |
    -----------------------------
    |   1     |   English       |
    |   2     |   stats         |

and a master table to combine them all and to know what teacher teaches what subject to which class

Master table

    |  ID     | Teacher_id  |  class_id |  subject_id  |
    ----------------------------------------------------
    |   1     |   1         |    1      |      1       |
    |   2     |   1         |    2      |      2       |

Just to clear out what I made...I even wrote a select query for the problem even though he didn't asked me to do so.

I am just a beginner at sql so I don't know is this the right way or not but according to me it is quite useful in case I need to make changes to the database. Example addition of a class or a subject.

Now according to him this design will not work at all and He said that I should not even consider my self as a beginner but below the level of beginner.

So please be kind enough to tell me that is this right or not and if its not what is the right way to design the database.

thanks in advance.

Upvotes: 3

Views: 432

Answers (4)

Leeho
Leeho

Reputation: 1

Maybe you don't want to explicitly store information about what subjects can be taught by a particular teacher. Actually, we have a real application on educational process, where we don't store such information, and we haven't needed it badly enough to implement yet. So i guess you can go without it too, given that you weren't provided with requirements to restrict users on assigning teacher to subjects and classes.

What i think you would want to restrict is to have just one teacher for each subject taught to a particular class. So i would go for something simple, like table named "Courses", with columns {id, teacher_id, subject_id, class_id, semester_num}, and a unique constraint on {subject_id, class_id, semester_num}, keeping teacher as an attribute here.

Upvotes: 0

Damir Sudarevic
Damir Sudarevic

Reputation: 22177

About multi-valued-dependency and 4NF

enter image description here

Upvotes: 4

It appeared quite simple

That's probably part of the problem. Most database problems appear quite simple. But few of them actually are quite simple.

  • Not every teacher is qualified to teach every subject in every school. You probably need a table that pairs teachers with the subjects they're qualified to teach.
  • Some entities carry their full identification in their name. (Subjects seem to; people don't.) Failing to provide a unique constraint on, say, "subject_name" would be a mistake, regardless of whether that table has an id number (which might easily be another mistake).
  • "Master" isn't a term in the problem domain. That is, if you look at a college course catalog, you won't find a chapter or section entitled "Master chapter", or anything remotely like that. But you will find a section that tells you which subject is offered during each semester, and
    that's a clue. In a college, not every course is available during every semester.
  • You also need a unique constraint on the "master" table {teacher_id, class_id, subject_id}.
  • In that same table, you need overlapping foreign key constraints on {teacher_id, subject_id} and {class_id, subject_id}.

I could go on. But here's why I wouldn't have hired you.

You were given ambiguous, incomplete requirements. (That's probably not an accident.) You didn't do enough to clarify them.

I am just a beginner at sql

We were all beginners at some point.

Did they want to hire a beginner? If not, that might be another problem.

Upvotes: 6

Furqan Hameedi
Furqan Hameedi

Reputation: 4400

Except for one glitch it seems fine, the glitch it in the Master Table if you assign a teacher to a class with wrong subject, you cannot put a constraint for that in database, IMHO there shoul be one more intermediary table which maps classes and subjects i-e ClassSubjectMap

       ----------------------------------------------------
      |  CSMapID |  class_id |  subject_id  |
      ----------------------------------------------------
      |   1     |    1      |      1       |
      ----------------------------------------------------
      |   2     |    2      |      2       |
      ----------------------------------------------------

Then this map ID is used in Master table to map teachers with subject, this way a teacher cannot be assigned to teach biology to an engineering class. Except for this other tables seem fine.

Upvotes: 5

Related Questions