You are viewing a plain text version of this content. The canonical link for it is here.
Posted to derby-user@db.apache.org by Legolas Woodland <le...@gmail.com> on 2006/04/12 09:39:52 UTC

How to find master record primary key to use it in inserting details records

Hi
Thank you for reading my post
I have two tables which are related to eachother using a column (master 
details)
my problem is that master table primary key is auto increment
so when i insert a recoed to it i can not determine the primary key value
so i do not know the key ,
Now my problem is that how i can find the primary key of master record 
to use it when i inert
details records ?
I should say that it is a very high hit application , before i post this 
message i though that i can execute a query and select latest inserted 
record
but latest inserted record could be from another user not the current user.
Thanks


Re: How to find master record primary key to use it in inserting details records

Posted by Christine Johnson <cj...@willowtech.com>.
> Legolas Woodland wrote:
> >
> > Hi
> > Thank you for reading my post
> > I have two tables which are related to eachother using a column (master
> > details)
> > my problem is that master table primary key is auto increment
> > so when i insert a recoed to it i can not determine the primary key
value
> > so i do not know the key ,
> > Now my problem is that how i can find the primary key of master record
> > to use it when i inert
> > details records ?
> > I should say that it is a very high hit application , before i post this
> > message i though that i can execute a query and select latest inserted
> > record
> > but latest inserted record could be from another user not the current
user.
> > Thanks
> >
> >
> you may use IDENTITY_VAL_LOCAL()  , which return latest identity value
> inserted for a table by the connection that call this function.
> so you will have no problem about multiple connections because  each
> connection will have its own state
>
> -- 
> Regards , Masoud Kalali
> http://weblogs.java.net/blog/kalali/
>
>
The getGeneratedKeys method of a Statement or PreparedStatement will work
too.  Find the "Autogenerated Keys" page in the Derby Reference Manual for
more information.

Regards,
Chris Johnson
cjohnson@willowtech.com



Re: How to find master record primary key to use it in inserting details records

Posted by Masoud Kalali <ka...@gmail.com>.
Legolas Woodland wrote:
> 
> Hi
> Thank you for reading my post
> I have two tables which are related to eachother using a column (master 
> details)
> my problem is that master table primary key is auto increment
> so when i insert a recoed to it i can not determine the primary key value
> so i do not know the key ,
> Now my problem is that how i can find the primary key of master record 
> to use it when i inert
> details records ?
> I should say that it is a very high hit application , before i post this 
> message i though that i can execute a query and select latest inserted 
> record
> but latest inserted record could be from another user not the current user.
> Thanks
> 
> 
you may use IDENTITY_VAL_LOCAL()  , which return latest identity value 
inserted for a table by the connection that call this function.
so you will have no problem about multiple connections because  each 
connection will have its own state

-- 
Regards , Masoud Kalali
http://weblogs.java.net/blog/kalali/


Re: How to find master record primary key to use it in inserting details records

Posted by Craig L Russell <Cr...@Sun.COM>.
Hi Legolas,

If you're using JDBC, you can use the Statement.getGeneratedKeys()  
method. You define the master table with the primary key column being  
IDENTITY. You insert a row using Statement.execute(), call  
getGeneratedKeys, and the ResultSet returned from the method contains  
the value of the IDENTITY primary key you just inserted.

Craig

On Apr 12, 2006, at 12:39 AM, Legolas Woodland wrote:

>
> Hi
> Thank you for reading my post
> I have two tables which are related to eachother using a column  
> (master details)
> my problem is that master table primary key is auto increment
> so when i insert a recoed to it i can not determine the primary key  
> value
> so i do not know the key ,
> Now my problem is that how i can find the primary key of master  
> record to use it when i inert
> details records ?
> I should say that it is a very high hit application , before i post  
> this message i though that i can execute a query and select latest  
> inserted record
> but latest inserted record could be from another user not the  
> current user.
> Thanks
>

Craig Russell
Architect, Sun Java Enterprise System http://java.sun.com/products/jdo
408 276-5638 mailto:Craig.Russell@sun.com
P.S. A good JDO? O, Gasp!