I am designing a MYSQL database for online tutoring website
Let’s say, I have:
- faculty table with (faculty_id, faculty name)
- subject table with (subject_id, subject name)
- student table with (student_id, student name)
- class table with (class_id, faculty_id, student_id, subject_id)
Now I would like to run SQL queries on the class table to find out all students enrolled with a particular faculty & under a particular subject for which I have:
$sql=("SELECT *
FROM class
WHERE (faculty_id = '" . mysql_real_escape_string($_POST['faculty_id']) . "')
and (subject_id = '" . mysql_real_escape_string($_POST['subject_id']) . "')");
However, I can’t seem to figure out how to retrieve student name, faculty name, and subject name instead of just the faculty_id, student_id & subject_id stored in the class table.
Is there some type of SCHEMA and foreign key relations I need to create which automatically link the ID numbers to their respective rows in the respective tables so i can run UPDATE, DELETE, and INSERT queries based on these ID numbers?
Am I missing something important here? I’m not an expert in DB design. Please help.
That’s not something that happens in your schema, it’s something that you do when you query the database.
To get information from a related table, you JOIN that table.
You’ll have to replace any column names with what they actually are (your examples had spaces in them, so I’m sure those aren’t the real names), and put your PHP code with the post values where the ? are… good time to read up on PDO parameterized queries too.