Hello. I have some beginners' dilemmas about creating a relational database so any help is really appreciated. I don't ask you to code for me and make my sql queries but please help me understand some things.
So, at the beginning let's say that i would like to make a database with some schools' information (students, teachers, classes, grades...). My "main" table is called 'students' and it contains some basic information about a student:
| SID | first_name | last_name | class | dob | street | city | etc...
----------------------------------------------------------------------------
| 1 | studentF1 | studentL1 | 5 | 2147483647 | street | city | etc...
| 2 | studentF2 | studentL2 | 3 | 2147348421 | street | city | etc...
etc.
Other tables contain less information and some of them are only tables which describe some information. For example, table 'parents' looks like this:
| PID | SID | first_name | last_name | type |
-----------------------------------------------
| 1 | 1 | parentsF1 | parentsL1 | father |
| 2 | 1 | parentsF2 | parentsL2 | mother |
| 3 | 2 | parentsF3 | parentsL4 | father |
| 4 | 2 | parentsF4 | parentsL5 | mother |
etc.
'PID' in this table is unique parent's ID, 'SID' refers to 'SID' from 'students' table.
Table 'grades' looks like this:
| GID | SID | subject | grade | type |
----------------------------------------
| 1 | 1 | 1 | 4 | 1 |
| 2 | 1 | 2 | 2 | 1 |
| 3 | 1 | 3 | 4 | 1 |
| 4 | 1 | 3 | 4 | 2 |
| 5 | 2 | 1 | 3 | 1 |
| 6 | 2 | 2 | 2 | 3 |
etc.
In this table 'GID' means grade ID and it is unique value, 'SID' refers to student's ID, 'subject' is just a 'pointer' to another table where all subjects are described, 'grade' is actual grade and 'type' describes what kind of grade is it (oral exam, written exam, etc).
According to this I created few more 'needed' tables.... For example, table 'grades_description':
| GDID | level | description |
--------------------------------
| 1 | 1 | insufficient |
| 2 | 2 | sufficient |
| 3 | 3 | good |
| 4 | 4 | very good |
| 5 | 5 | excellent |
Table 'subjects' looks like this:
| SUID | description |
-----------------------
| 1 | maths |
| 2 | language |
| 3 | chemistry |
etc..
Table 'teachers':
| TID | first_name | last_name | subject |
------------------------------------------
| 1 | teacherF1 | teacherL1 | 4 |
| 2 | teacherF2 | teacherL2 | 3 |
etc.
Every occurrence of 'SID' in tables other than 'students' refers to 'SID' in that same 'students' table.
My first dilemma is:
did I set up my tables correctly?
I tried to break information to smallest pieces possible but I want to be sure if I set it up correctly or am I missing some basic logic here? =)
Should I keep it that way?
Second thing on my mind is:
how should i run queries to get all the information i need from a single student? For example, if i want to get some kind of associative array at the end of my script which looks like this:
student_first_name=>student1
student_last_name=>student2
student_mother_first_name=>parent1
student_mother_last_name=>parent2
student_grade_subject1_oral=>4
student_grade_subject3_written=>3
etc.
i should make really big query to retrieve all I need, like this:
SELECT students.first_name, students.last_name, students.dob, students.city, parents.first_name, parents.last_name, parents.type, grades,subject, grades.grade, grades.type (........) FROM students,parents,grades (....) WHERE parents.SID = students.SID AND grades.SID = students.SID AND grades.subject = subjects.description AND ........
Does my query really needs to be so huge or is there any other method of retrieving data from 'relational' model?
Uf, I hope I explained everything good =) Sorry for possible confusion and thanks in advance for any help =)