• 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

foreign key related problem

 
Ranch Hand
Posts: 53
Eclipse IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello guys,

I have 2 table in database

first is USERINFO
second is QUERYINFO

I have following situation.

In form user will enter whole info of him and query and submit it.
and
i want to add info of user into USERINFO TABLE and query into QUERYINFO table.

Attributes :

USERINFO
userid - IS AUTO INCREMENTED
name
firstname
....

QUERYINFO
queryid
query
queryanswer
userid
...

so my question is how do add value of userid to QUERYINFO table when newly user info is added to USERINFO...?





 
Bartender
Posts: 2407
36
Scala Python Oracle Postgres Database Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

dhaval thakor wrote:so my question is how do add value of userid to QUERYINFO table when newly user info is added to USERINFO...?



Any parent-child relationship implemented by a foreign key basically means the parent must exist before the child can be created, which isn't too hard to figure out - your parents existed before you did, right?

1. Write the user record.
2. Get the ID of the new user. The mechanism for doing this will depend on your set-up e.g. some databases such as Oracle support a RETURNING clause on DML, or your ORM might give you a way to do this.
3. Include the new user ID in your new Query record.
4. Write the Query record.

You could do steps 2 to 4 in a single SQL statement, but as you're posting on a Java forum, I'm guessing you want to use Java.
 
dhaval thakor
Ranch Hand
Posts: 53
Eclipse IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is what i need
Thanks dear...
 
reply
    Bookmark Topic Watch Topic
  • New Topic