-1

I already read through other questions such as these:

Is it fine to have foreign key as primary key?

Primary and Foreign Key at the same time

However, it seems to me as the answers are wildly different and the answer of whether or not it can be okay to have keys be both primary and foreign depends on the specific database and purpose of the table containing the keys.

This was done as part of an exam project and I have to either be able to explain my database design or come up with something else for my presentation.

My question(s) are:

How would you best justify the "selected_answers" table in my DB? i.e. does it make sense?

Is is still in 3rd normal form?

Could I make it different and simple while fulfilling the same purpose and functions of the current table?

This is my current relational model:

Relational model

This is my current SQL tables script:

SQL tables

The idea is to create a database which can store "audit" data on a firm that wants to audit how well they are complying with GDPR. It was an assignment for an exam.

The reason why I have "selected answers" is so that I can compare selected answers from users with the questions being asked.

Keys being both primary and foreign keys is so the numbers are "unique" while still referencing audit and question table.

The database is also supposed to be able to store each audit and the answers thereof individually.

For some more context I populated my tables with "test data" so that I could use stored triggers, procedures and a function.

Test data 1

Test data 2

All questions are pre set as question_ID 1 - 12 and all answers are answer_ID 1-5. So these numbers will definitely repeat themselves, but for the purposes of storing audits and what was answered.

This is the final result which the database calculates: Company name and registration number (CVR - danish), Time of audit (is updated through a stored trigger), average score of answers and a "compliance level" based on the average score.

Final result

I hope that I have provided enough information for you to help me :-) if you also need to see stored procedures, function and trigger tell me and I'll upload 'em.

  • 1
    please don't use images for showing code – Mark Jun 13 '18 at 11:23
  • Your `selected_answers` table does not actually contain a key that is both primary and foreign key. That would e.g. be the case if you had a foreign key `(Audit_Id, Question_id)`. So you seem to worry about having to justify something that is not there. – Solarflare Jun 13 '18 at 17:06
  • Please [use text, not images/links, for text (including code, tables & ERDs)](https://meta.stackoverflow.com/q/285551/3404097). Use an image only for convenience to supplement text and/or for what cannot be given in text. Use edit functions to inline, not link, if you have the rep--make your post self-contained. And never give a diagram without a legend/key. – philipxy Jun 13 '18 at 20:21
  • Hi. Please find, quote & apply definitions of the terms you are using--PK, FK, 3NF in a published academic textbook. (Dozens are free online in pdf.) Which is yours? Your question shows no understanding of them. You are missing info relevant to answering questions using them. Ask one question per post. Don't use links. – philipxy Jun 13 '18 at 20:29
  • Hi phillipxy, thank you for your feedback. I am new to using Stackoverflow so I will keep what you say in mind. I have not been taught academic terms as my curicullum is obviously lacking. Could you perhaps link me to some good academic sources? – Gustav Michael Lfstrm Jandrup Jun 13 '18 at 21:26
  • Dozens of published academic information modeling & database design textbooks are online free in pdf. stanford.edu has a free online course. (But asking for resources outside SO is off-topic.) Google 'stackexchange notifications' to learn to use @x to notify non-poster non-sole commenter x about a comment. – philipxy Feb 27 '19 at 05:14

1 Answers1

0

Your table makes sense. You can view it as a many-to-many-relationship between audit and question (each audit can answer many questions; each question can be answered in many audits) with an attribute indicating what the audit's answer to the question is, constrained (by the foreign key to the answer table) to be one of a number of predefined answers. I also advise you write down the relvar predicates and see if they make sense.

It is in 3NF, unless there are odd functional dependencies (FDs) you haven't told us about. There seems to be a single non-trivial FD, {Audit_ID, Answer_ID} -> {Answer_ID}, and the left-hand side is a superkey (a key, in fact), so the table is in Boyce-Codd normal form, which implies 3NF. (I really don't know why people bother with 3NF when there is BCNF.)

I can't think of any simpler solution that fulfils the same requirements. Your design may have issues due to the use of surrogate keys combined with the lack of natural keys, but that is a different question.

Jon Heggland
  • 274
  • 1
  • 7
  • Thank you so much for your answer Jon! I have some follow up questions, if that is okay :-) I have never heard of relvar predicates at uni, what is that? Furthermore, I am trying my hardest to grasp the notion of the difference between BCNF and 3NF - what is the difference between a trivial and non-trivial FD? My curriculum at uni is obviously severely lacking. – Gustav Michael Lfstrm Jandrup Jun 13 '18 at 21:08
  • The comments section is not the place for follow-up questions. Please use this site correctly, cf. philipxy's comments to your original question. – Jon Heggland Jun 14 '18 at 09:05