Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

foreign key related problem

 
dhaval thakor
Ranch Hand
Posts: 53
Eclipse IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • 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...?





 
chris webster
Bartender
Posts: 2407
32
Linux Oracle Postgres Database Python Scala
  • Mark post as helpful
  • send pies
  • 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 Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is what i need
Thanks dear...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic