I have two tables in my ClassSelector DB:
students
student_id | student_name | hometown
classes
class_id | classname | description
I am trying to write a program that will take a user-entered student_id and allow them to select a class from a list of class ID's. I want the application to write student_id, student_name, class_id, classname to a separate table. To do this, would it be best to create a new table(student_x_class) and use an INSERT statement:
INSERT INTO ClassSelector.student_x_class ClassSelector.students.student_id,ClassSelector.students.student_name, ClassSelector.classes.class_id,ClassSelector.classes.classname VALUES(W, X, Y, Z);
or create a new column in the classes table (student_x_class) and use this as a foreign key to the student_id column and use inner join.
Sorry if this is confusing, I'm new to MySQL. Any help would be appreciated!
ResultSet rs3 = myStmt3.executeQuery("SELECT * FROM ClassSelector.classes WHERE class_id = " + selectedClass);
while(rs3.next()){
String innerJoin = (userEnterId + " has been added to " + rs3.getString("classname") + " " + rs3.getString("class_id"));
System.out.println(innerJoin);
String student_classJoin = "INSERT INTO students_x_classes" + "(student_name, class_id, classname)" + "VALUES (?, ?, ?)";
PreparedStatement pStmt = con.prepareStatement(student_classJoin);
pStmt.setString(1, userEnterId);
pStmt.setString(2, rs2.getString("class_id"));
pStmt.setString(3, rs2.getString("classname"));
pStmt.executeUpdate();
input.close();
}
Since student-classes is an M:N
relationship (meaning 1 student can attend many classes and 1 class can have many students) I would suggest you create table student_x_class
with columns (class_id, student_id)
.
Then you can select all classes for a specific student with a simple join select
SELECT *
FROM students s
INNER JOIN student_x_class sc ON sc.student_id = s.student_id
INNER JOIN classes c ON c.class_id = sc.class_id
WHERE s.student_id = ?
When you want to INSERT
rows into the student_x_class
table, you don't need to join anything. Simply insert the class_id and student_id pair (no need for classname in that table), eg
String student_classJoin = "INSERT INTO student_x_class" + "(student_id, class_id)" + "VALUES (?, ?)";
And don't forget to adjust the binds, ie remove line pStmt.setString(3, rs2.getString("classname"));
Btw this is really basic stuff, try reading some SQL book/tutorial to get the whole concept.
That depends on the nature of the student-class relation. If a given class is only related to one student (let's imagine is its delegate or something like that) then a foreign key in classes table would be enough. If a class can be related to more than one student then you should think in creating a new table to model that relation. If you use this second approach, a good recommendation would be to include in the new table only the two ids.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.