更新时间:2023-11-30 20:18:52
Instead of having dept_name as a primary key
, create another column DEPT_ID
and set it as a primary key. You can increment that in the same way.
Also, consider using a PreparedStatement for your SQL updates
As a rule, your FK would point to the PK of another table. So when you have created DEPT_ID as a primary key, you would also need to modify your student schema to reflect this change
Edit: Based on your requirement, you can do something like this (although I don't know your exact requirement) & assuming the aforementioned change, you can do something like this
Create a PreparedStatement
like this:
PreparedStatement pstmt = con.prepareStatement(INSERT INTO student(id, name, dept_id,tot_cred) values(?,?,?,?)");
Since you know the number of departments that you've, you can now create a loop to insert tuples into your department table and then use this information to insert into your student table, right? (This sounds more like a homework, so I would leave it to you to think of how to do this part)