• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

mysql insert two tables at same time

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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(?,?,?,?,?)";
 
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic