It says student_id doesn't exist, but it's clearly there, I just want to connect these tables together. (Mysql 5.7)
student_id is missing on your Subject table, add student_id int unsigned not null in your query to create Subject table before you put the foreign key.
You need to have the column 'student_id' in the 'subject' table. The error denotes the 'subject' table.
You are trying to mark a column 'student_id' in 'subject' table as foreign key that references to column 'student_id' in 'students' table.
That's because you doesn't create student_id in subject table
you must create student_id after subject_id column
As other comments you Subjects Table doesnt have student_id field, and probably shouldnt have it. Otherwise you would be repeating the same data for each student attending that course.
So what you do:
Create the Tables Students, Subjects like you have it now (without the foreign key to Student)
Create a junction table student_subject {subject_id, student_id} where you store what subjects are enrolled each student.
Create Foreign keys to Students, Subjects
Then if you want get what subjects is enrolled one student.
SELECT st.*, su.*
FROM Students st
JOIN Student_Subjects ss
ON st.student_id = ss.student_id
JOIN Subjects su
ON ss.subject_id = su.subject_id
WHERE st.student_id = #student_id
Related
I am working with 3 tables in MySQL: students, registration, courses.
If under table courses I also have student_ID and student_Name, is it possible then if I update the the student_Name on the students table, it will also be updated on the courses table?
Or that when I insert a student_ID to the registration table, it will also insert the student_name that correspond with it to the column?
(all 3 table have the columns student_ID and student_name, I am trying to find a way to update the value on only one place..)
you can make student_id field in all tables as field with primary index. ater it try to add this feild as foreighn key to other tables
I have a few tables that need to be joined together multiple times but I'm not sure if there can be multiple join tables for the same two tables. The tables i have are as follows:
User
Group
Record
Id PK
Id PK
Id PK
Name
Name
Name
etc
etc
Date
GroupID FK
//A record can only belong to 1 group
UserID FK
//A record can only have 1 creator/admin
I then want to have two join tables that can keep track of which group a user is a member of, and which member is an admin of the group.
Group Members
Group Admins
GroupID
GroupID
UserID
UserID
A Group can have many members while a user can be a member of many groups.
A Group can have many admins while a user can be an admin of many groups.
I get an error when trying to create the foreign keys for the "Group Members" and "Group Admins" tables.
SQL Query: "ALTER TABLE GroupMembers ADD CONSTRAINT GroupId FOREIGN KEY (GroupId) REFERENCES Group(Id);"
SQL Error: "Error creating foreign key on GroupId (check data types)"
Is this possible or is there a better way of doing this?
While updating the question, I found that there were restrictions being placed on the foreign keys by my workbench which was causing the error. The solution was to remove the "on delete" and "on update" restrictions.
Given two relations:
Students = (St-Id, Name, Address, CourseNo, Cgpa)
Courses = (CourseN0, CourseName, Credits)
where primary keys are St-Id and CourseNo. CourseNo in Students relation is a foreign key references Courses relation.
Assume the following queries are frequent:
Question: What are the courses (CourseNo and CourseName) studied by each student?
SELECT Students.Name, Courses.CourseName, Course.CourseNO
FROM Students
INNER JOIN Courses
ON Students.CourseNo=Course.CourseNo;
Is that the right query by using join operation?
It's a a primary index because of course number. Can we consider it as rule to say courseNo is a primary index? It's also clustering? What is the difference between clustering and primary index?
Question: What is the Cgpa for each student?
Answer : Select Cgpa and name from students
I agree with simon at rcl, your design only allows one course per student. Try to put an intersection table between student and courses.
Students = (St-Id, Name, Address, Cgpa)
Courses = (CourseN0, CourseName, Credits)
StudentCourse = (St-Id, CourseN0)
If the table has a primary key, then the clustered index is the same as the primary key. If it doesn't have a primary key, InnoDB will create a clustered index on its own. The rules it uses to decide how to do this are described here.
Your syntax is not quite right. To select multiple columns from the table, you separate them with comma, not and. So it should be:
SELECT name, cpga FROM student
Also, if St-id is the primary key of Students then a student can only have one course. You design looks a bit lacking there.
SELECT
name
,cgpa
FROM
students
WHERE
1=1
try this
I have 2 tables one is students and the other is courses
the key for courses will be ID, however I'm having trouble connecting students with courses.
I need a datatype that will be similar to a list
so I can push an id into the data type or remove the id
also, I need a way to return * from courses if the id exists in the data type
You need to create a junction table StudentCourseAssoc
StudentCourseAssoc
----------------------
studentId
courseId
The columns being foreign keys to students to courses respectively.
That's not how you work with a relational database.
(It's possible to put comma separated values in a varchar field and use to join against a table, but it's slow and complicated to use. I have seen such attempts from time to time, and it quickly falls apart when you want to do anything other than the simplest possible queries.)
Add another table, where you reference a course and a student. Example:
StudentCourses
-------------------
StudenCourseId int autoincrement
StudentId int
CourseId int
(The autoincrement key for the relation table is optional, you can omit it and make the combination of the two foreign keys the key of the table.)
To get the courses for a student you join in the relation table. Example:
select
c.CourseName
from
Courses c
inner join StudentCourses sc on sc.CourseId = c.CourseId
where
sc.StudentId = 42
I'm new to SQL and I'm having a hard time figuring out how to execute queries with foreign keys on MySQL Workbench.
In my example, I have three tables: people, places, and people_places.
In people, the primary key is people_id and there's a column called name with someone's name.
In places, the primary key is places_id and there's a column called placename with the name of a place.
People_places is a junction table with three columns: idpeople_places (primary key), people_id (foreign key), and places_id (foreign key). So this table relates a person to a place using their numerical IDs from the other two tables.
Say I want the names of everyone associated with place #3. So the people_places table has those associations by number, and the people table relates those numbers back to the actual names I want.
How would I execute that query?
Try this to find all the people names who are associated with place id 3.
SELECT p.name
FROM people as p
INNER JOIN people_places as pp on pp.people_id = p.people_id
WHERE pp.places_id = 3
OK, so you need to "stitch" all three tables together, yeah?
Something like this:
select people.name
from people -- 1. I like to start with the table(s) that I want data from, and
, people_places -- 2. then the "joining" table(s), and
, places -- 3. finally the table(s) used "just" for filtering.
where people.people_id = people_places.people_id -- join table 1 to table 2
and people_places.place_id = places.place_id -- join table 2 to table 3
and places.name = "BERMUDA" -- restrict rows in table 3
I'm sure you can do the rest.
Cheers. Keith.