How to link(relate) this two tables MySQL - mysql

Problem
A student registers for a course.
Add that courses name, and registration date, to the student_courses that can be identified with the student that was registered
Combine the two tables to show the courses the student is registered to.
How should I do this, what is the relationship the Student and student_courses have in order to achieve my goal.

What I would do is add a field to student_courses being the student's personalNumber and also adding the courseId to student_courses. This way it is easy to join all 3 tables

You are on the right track with the student_courses table.
Because the relationship between the student and course can be many-to-many (i.e. multiple students can register for multiple courses) there is a need for a relational table to link a course to the student.
student_courses should store a foreign key reference to the primary key on student and course and include any registration-related fields (like registration date). By doing this you can generate a sql statement that builds a list of all courses the student is related to:
select
sc.registrationdate, c.*
from
student_courses sc
join student s on s.personalNumber = sc.student_id
join courses c on c.courseId = sc.courses_id
where
s.personalNumber = 123;
or if you want to find all the students related to a course:
select
s.personalNumber, s.firstName, s.lastName
from
student_courses sc
join student s on s.personalNumber = sc.student_id
join courses c on c.courseId = sc.courses_id
where
c.courseId = 123;
Hope that helps.

I added a many to many relationship between Courses, and Student, and a new table was generated. In that table then I added a date filed.

Related

Get all data from a table that contains two foreign key data

Table instructor: ID, name, dept_name, salary
Table student: ID, name, dept_name, tot_cred
Table advisor: s_ID, i_ID which contains student id and instructor id for the two table.
I need to find all the instructor and student's name where the department of the advisor is CComp.Sci
I can find all the id of the instructor and student whrer the intructor is fom Computer science. And only the name of the students.
But can't figure out both the name at the same time.
I wrote this:
SELECT student.name
FROM student
WHERE student.ID in (SELECT advisor.s_ID
FROM advisor
, instructor
WHERE advisor.i_ID = instructor.ID
and instructor.dept_name = 'Comp.Sci')
The root solution I think you need here is just a simple join between the three tables. But since you need a single list of both student and instructor names, this complicates things. One option is to union together a query which finds the matching students along with a query which finds the matching instructors.
SELECT s.name, 'student' AS type
FROM student s
INNER JOIN advisor a
ON s.ID = a.s_ID
INNER JOIN instructor i
ON a.i_ID = i.ID
WHERE i.dept_name = 'CComp.Sci'
UNION ALL
SELECT DISTINCT i.name, 'instructor'
FROM student s
INNER JOIN advisor a
ON s.ID = a.s_ID
INNER JOIN instructor i
ON a.i_ID = i.ID
WHERE i.dept_name = 'CComp.Sci'
Using "where .. in (..)" will give you bad performance, also it will not allow you to get the data from the table in the where clause.
Here is a solution if you mean to get the result of instructor name side alone with student name.
SELECT S.name AS Student,I.name AS Instructor
FROM Students AS S
JOIN Advisor AS A ON A.s_ID = S.Id
JOIN Instructor AS I ON I.Id = A.i_ID
WHERE I.dept_name = 'Comp.Sci'
*notice that I used alias
** if you want for example to get all students even those who lack instructor use LEFT JOIN

Listing later dates in comparison to another entry in SQL

How would I list the students who started later than start year of student with id = 8871?
This is what I have so far:
SELECT sid s1
FROM Student
WHERE s1.started <= sid = '8871';
I went to this link for some reference but I'm still having issues:
Query comparing dates in SQL
Student Table has:
Student (Lastname, Firstname, SID, Started)
any help would be great
I'd use a self join where one side of the join contains the students you want and the the reference student 8871:
SELECT a.*
FROM student a
JOIN student b ON b.sid = '8871' AND a.started > b.started

How to merge two tables according to the relation in a third table?

Suppose there are 3 tables:
student
teacher
advisor
Student has name and id columns, teacher also has name and id columns and advior has s_id(stands for student id) and t_id (stands for teacher id).
s_id references to id in student table and t_id references to id in teacher table.
How can I merge the name of the student with name of his advisor?
Thanks for any help.
Simply create supervisor id in student table to merge the supervisor with his students and use join Statement
You can use join to merge the tables.
Try this:
select b.name,c.name from advisor a
join student b on a.s_id = b.id
join teacher c on a.t_id = c.id
where a.s_id = 1
Note that a, b and c are aliases.

Multiple choices checkboxes sql design

I would like to know what would be the best way of storing multiple check-boxes values for easy search.
Is there any advantage if for a skills table (football, swimming, dancing ....) I assign unique id or should I use a unique string - the exact word to be stored in the database?
If the answer is "numbers" and the total available options are more than "10" should I start increment the id from ten so I can avoid finding 12, 2, 22 for %2% in case storing is made like 14,23,34 / football,swimming,dancing in the user table.
Is there a better approach?
Use 3 tables:
Person table. This contains single-valued attributed such as name, birthday, etc.
Skills table. This contains the name of each skill.
PersonSkills table. This is a relation table that contains the many-to-many relationship between the first two tables. It has two columns: Person_id and Skill_id, which are foreign keys into the first two tables.
To get all the skills, you join the tables:
SELECT person_name, GROUP_CONCAT(skill_name) skills
FROM Person p
LEFT JOIN PersonSkills ps ON p.person_id = ps.person_id
JOIN Skills s ON s.skill_id = ps.skill_id
To get all the people with a particular skill:
SELECT person_name
FROM Person p
JOIN PersonSkills ps ON p.person_id = ps.person_id
JOIN Skills s ON s.skill_id = ps.skill_id
WHERE skill_name = "swimming"

get data from 3 relational tables

I have three tables Guardian, Student and StudentsGuardian. Table information is as under
Guardian:
id(pk)
Student:
id(pk)
name
address
StudentsGuardian:
student_id(fk)
guardian_id(fk)
I want to select those students whose guardian_id=2(suppose). Actually these are relational tables so i am unable to think a way to accomplish it. If i apply join it would return a joint table but i need only the information of those students having guardian_id= specific id.
It could be a basic question but i am stuck in it. Thanks
Use below query:
SELECT s.id, s.name, s.address
FROM Student s
INNER JOIN StudentsGuardian sg ON s.id = sg.student_id
WHERE sg.guardian_id = 'somespecific_id'
SELECT
*
FROM Guardian
INNER JOIN StudentsGuardian ON StudentsGuardian.guardian_id = Guardian.id
INNER JOIN Student ON Student.id = StudentsGuardian.student_id
WHERE StudentsGuardian.guardian_id = 2
SELECT Student.name, Student.address
FROM Student JOIN StudentsGuardian ON Student.id = StudentsGuardian.student_id
WHERE StudentsGuardian.guardian_id = 2
That should do.