Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

mysql insert two tables at same time

 
Darshana Kapuge
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
its like this my student_register table have regNo as primary key and sDay and name & more
in the same database i have semester table is primary key is also regNo and same columns (name / joint_date ) in the [u] sem table [/u]this common columns must update by single query is it possible and is their are any methods to do that plaz enlighten me

sql ="INSERT INTO student_register (regNo,name,sname,idNo,age,birthD,gen,national,relegion,fee,institute,pro,sDay,eMail,phnNo,address,img) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)" ;
sqlSem="INSERT INTO sem (id,name,cu rSem,startDate,semEnd)VALUES(?,?,?,?,?)";
 
Dave Tolls
Ranch Hand
Posts: 2099
15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You don't insert into multiple tables in a single SQL statement.
You do multiple inserts in a single transaction.
This assumes that a student and its associated sem entries need to be committed together, or rolled back together.

Why does semester use the same primary key as student?
 
Darshana Kapuge
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can you give me example of it
i need it to store student semester calculation thats why i need it
its student attendance tacking system so i have to know when i check for attendance i have to show his current semester this table do all the semester calculations
 
Dave Tolls
Ranch Hand
Posts: 2099
15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm really not sure what you want to know.

If I have a table A and a table B that has a foreign key to , and I have a requirement that A and its associated B entries need to be created together then:

1. grab a Connection with auto-commit off (this is entirely db dependent).
2. Create my PreparedStatement for the insert into A, with autoGeneratedKeys.
3. Set the parameters and call executeUpdate.
4. Call getGeneratedKeys on the statement, which returns the list (in this case the single) primary key.
5. Do the insert into B with the foreign key set to the key returned in step 4.
6. Commit.

Now. All of this is dependent on your table structure, and exactly what your requirements are.

Apologies if I've missed something...I'm in the middle of a commit to live.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic