I don’t know if I am doing the below method correctly when using sql but here it goes.
I want to display all exam details (exams details (Also know as Session), questions in exam, answers, penalty, images, videos etc) But the way I am trying to do it is one big query but it is not working as no rows are being displayed.
Below are what I want to display for one exam (and only one exam):
- Exam Details
- All questions in Exam
- All Answers to each question and marks each answer worth
- Penalty (see if enabled or not)
- Penalty marks for incorrect answers
- Images in question
- Videos in questions
- Audio in questions
- Module Details
- Student
Below show the fields each section above require from database:
-
Exam Details
SessionId, SessionName, SessionDuration, TotalMarks, SessionWeight -
All questions in Exam
QuestionId, QuestionNo, QuestionContent, NoofAnswers, QuestionMarks, OptionId, OptionType, ReplyId, ReplyType -
All Answers to each question and marks each answer worth
AnswerId, Answer, AnswerMarks
- Penalty (see if enabled or not)
PenaltyEnabled
- Penalty answers
PenaltyAnswerId, PenaltyAnswer, PenaltyMarks
- Images in question
ImageId, ImageFile
- Videos in questions
VideoId VideoFile
- Audio in questions
AudioId, AudioFile
- Module Details
ModuleId, ModuleNo, ModuleName
- Student
StudentId
My question is how should the SQL code be written in order for all these data to be shown? Do I just need one big query or a lot of little queries? My attempt is at bottom of question but below is the database tables which shows details dealing with one exam and all its details.
TABLES:
Student
StudentId (PK) StudentForename StudentSurname
1 James Parker
Student_Session
SessionId (FK) StudentId (FK)
1 1
Session
SessionId (PK) SessionName SessionDuration TotalMarks SessionWeight ModuleId (FK)
1 AAA 01:00:00 30 20 1
Module
ModuleId (PK) ModuleNo ModuleName
1 CHI2513 ICT
Question
SessionId FK) QuestionId (PK) QuestionNo QuestionContent NoofAnswers QuestionMarks OptionId (FK) ReplyId (FK)
1 4 1 Question 1 1 5 1 1
1 5 2 Question 2 1 3 2 1
1 6 3 Question 3 2 6 2 2
1 7 4 Question 4 3 7 5 2
1 8 5 Question 5 1 9 5 1
Answer:
AnswerId (PK) Answer QuestionId (FK)
1 A 4
2 C 5
3 A 6
4 B 6
5 B 7
6 D 7
7 E 7
8 G 8
Individual Answer
IndividualId (PK) AnswerId (FK) AnswerMarks
1 1 3
2 2 5
3 3 3
4 4 3
5 5 2
6 6 2
7 7 3
8 8 9
Penalty
PenaltyId(PK) SessionId (FK) PenaltyEnalbed
1 1 1
PenaltyMarks
PenaltyAnswerId (PK) PenaltyAnswer PenaltyMarks QuestionId (FK)
1 B 1 4
2 C 1 4
3 A 1 5
4 B 1 5
5 D 1 5
6 C 2 6
7 D 2 6
8 A 1 7
9 C 1 7
10 F 1 7
11 G 1 7
12 A 0 8
13 B 0 8
14 C 1 8
15 D 1 8
16 E 1 8
17 F 0 8
Reply
ReplyId (PK) ReplyType
1 Single
2 Multiple
Option_Table
OptionId (PK) OptionType
1 A-C
2 A-D
3 A-E
4 A-F
5 A-G
Image
ImageId (PK) ImageFile
1 ImageFile/Tulips.png
2 ImageFile/Daisys.png
Video
VideoId (PK) VideoFile
1 VideoFile/Speech.png
Audio
AudioId (PK) AudioFile
1 AudioFile/Song.png
Image_Question
ImageQuestionId (PK) ImageId (FK) SessionId (FK) QuestionNo (FK)
1 1 1 2
Video_Question
VideoQuestionId (PK) VideoId (FK) SessionId (FK) QuestionNo (FK)
1 1 1 4
Audio_Question
AudioQuestionId (PK) AudioId (FK) SessionId (FK) QuestionNo (FK)
1 1 1 5
Below was my failed attempt:
SELECT s.SessionId,
SessionName,
SessionDuration,
TotalMarks,
SessionWeight,
q.QuestionId,
q.QuestionNo,
QuestionContent,
QuestionMarks,
q.OptionId,
OptionType,
q.ReplyId,
ReplyType,
a.AnswerId,
Answer,
NoofAnswers,
AnswerMarks,
PenaltyEnabled,
PenaltyAnswerId,
PenaltyAnswer,
PenaltyMarks,
i.ImageId,
au.AudioId,
v.VideoId,
ImageFile,
AudioFile,
VideoFile,
s.ModuleId,
ModuleNo,
ModuleName,
ss.StudentId
FROM Student st
INNER JOIN Student_Session ss
ON st.StudentId = ss.StudentId
INNER JOIN Session s
ON ss.SessionId = s.SessionId
INNER JOIN Question q
ON s.SessionId = q.SessionId
INNER JOIN Answer a
ON q.QuestionId = a.AnswerId
INNER JOIN Individual_Answer ia
ON a.AnswerId = ia.AnswerId
LEFT JOIN Module m
ON s.ModuleId = m.ModuleId
LEFT JOIN Penalty p
ON q.SessionId = p.SessionId
LEFT JOIN Option_Table o
ON q.OptionId = o.OptionId
LEFT JOIN Reply r
ON q.ReplyId = r.ReplyId
LEFT JOIN Penalty_Marks pm
ON q.QuestionId = pm.QuestionId
LEFT JOIN Image_Question iq
ON q.QuestionId = iq.QuestionNo
INNER JOIN Image i
ON iq.ImageId = i.ImageId
LEFT JOIN Audio_Question aq
ON q.QuestionId = aq.QuestionNo
INNER JOIN Audio au
ON aq.AudioId = au.AudioId
LEFT JOIN Video_Question vq
ON q.QuestionId = vq.QuestionNo
INNER JOIN Video v
ON vq.VideoId = v.VideoId
WHERE s.SessionId = 1
ORDER BY q.QuestionId
UPDATE:
Will the code below be able to insert QuestionId into Image_Question Table after questions submitted: (This is something I found on SO but updated to my table design)
ImageQuestionToken
{
ImageToken (PK auto)
SessionId
}
Image{
ImageId
ImageFile
}
Image_Question
{
Image_QuestionId
ImageId FK references Image(ImageId)
QuestionId FK references Question(QuestionId)
}
Question
{
QuestionId (PK Auto)
QuestionNo
QuestionContent
....
}
TempImage
{
ImageToken FK references ImageQuestionToken(ImageToken)
ImageFile
}
And the logic like so:
- User requests question form. Server sets token and includes in response to user.
- User uploads picture including token. Image is stored in temporary table.
- Step 2 repeated n times.
- If user submits question with token value, an entry is placed in the questions table and an id is assigned. All images in the TempImage table that share the token get inserted to the image table with the now known QuestionId. The ImageQuestionToken entry is then deleted and cascade deletes the temp images in TempImage.
- Else user doesn’t submit question then files are deleted and ImageQuestionToken entry deleted.
There’s a problem with your foreign keys in these tables: Image_Question, Audio_Question, and Video_Question.
You are using the column QuestionNo which is not indexed by the table Question.
I would suggest replacing the columns as QuestionId instead and used their values.
and your statement should look like this:
update:
You can use the column QuestionNo afterall, you could just add index so you don’t have to overhaul your codes. hehehe
i suggest you add this:
and instead of these:
you use these: