Firebird SQL Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsDatabasesFirebird SQL Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old February 7th, 2005, 12:00 PM
martinblech martinblech is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2005
Posts: 2 martinblech User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 43 m 26 sec
Reputation Power: 0
Question Auto-generated ids

I'm using Firebird in a Java web application and need to use auto-generated id's on some tables. Currently, I use a generator and a trigger. That's ok when I'm inserting records in only one table, but the problem arises when I'm about to insert a record that references the generated id. Which would be the 'proper' way to find out the generated id for the just inserted row? I've come up with two solutions which don't convince me at all:
- One would be to search for the last inserted record. Impossible: another thread could have inserted another row just after I did.
- Search for a record whose fields are all but the id equal to the one I inserted. Generally ok, assuming those fields are a candidate key (which isn't always the case).
Any ideas? Can anyone help me with this?

Reply With Quote
  #2  
Old February 14th, 2005, 08:08 AM
martinblech martinblech is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2005
Posts: 2 martinblech User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 43 m 26 sec
Reputation Power: 0
Unhappy

Well, it seems nobody is interested in answering my question.
Can anyone at least suggest another forum where my questions would be worth a reply?
Thanks,
Martin

Reply With Quote
  #3  
Old February 14th, 2005, 08:16 AM
shammat shammat is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Oct 2003
Location: Munich, Bavaria
Posts: 1,005 shammat User rank is Second Lieutenant (5000 - 10000 Reputation Level)shammat User rank is Second Lieutenant (5000 - 10000 Reputation Level)shammat User rank is Second Lieutenant (5000 - 10000 Reputation Level)shammat User rank is Second Lieutenant (5000 - 10000 Reputation Level)shammat User rank is Second Lieutenant (5000 - 10000 Reputation Level)shammat User rank is Second Lieutenant (5000 - 10000 Reputation Level)shammat User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 3 Days 11 h 33 m 53 sec
Reputation Power: 67
The situation with Oracle is very similar and the way I usually solve it, is to first retrieve the new ID from the sequence (which would be the generator in your case). Then I do the insert with that ID. As I have the ID present, I can also do the INSERTs into the related tables.

I don't use triggers for this kind of columns.

Reply With Quote
  #4  
Old February 14th, 2005, 02:31 PM
jlees81 jlees81 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2005
Location: Udine, Italy
Posts: 1 jlees81 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 27 m 41 sec
Reputation Power: 0
mhhh.. i'm not a db-expert (actually i'm still a student.. graduated but student ), but i've heard of something called "transaction" that, i think, con be useful in situations like these.. since a "transaction" is atomic (or, better, if i've understood it the right way), you can insert the record, retrive the ID in a variable (through a s.p.) and use it the way you need to use it (ok,ok,sorry for my bad english, i'm used to read in that language, but i'm not really good at speak/write, sorry!)

ah,those info comes from my uni professor of "advanced database system", so if they'r false don't blame me!

Reply With Quote
  #5  
Old February 14th, 2005, 03:35 PM
tim snl's Avatar
tim snl tim snl is offline
Code Cruncher
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2001
Location: Tasmania, Australia
Posts: 119 tim snl User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 9 h 21 m 20 sec
Reputation Power: 8
Instead of putting the generator inside an insert trigger call it manually using a query. This is a secure way to get an id and then you also have it and know what record you are about to create.
__________________
Beware of a programmer with a screwdriver!

Reply With Quote
  #6  
Old February 15th, 2005, 06:49 AM
ipaw ipaw is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2004
Location: NS Canada
Posts: 20 ipaw User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 9 h 35 m 37 sec
Reputation Power: 0
In my DB def's I create a GENERATOR for each table. For example I have a table called USERS that looks something like this:
CREATE TABLE USERS(
USER_ID INTEGER DEFAULT 0 NOT NULL,
USER_INITIALS VARCHAR(3) DEFAULT '' NOT NULL,
USER_LOGIN VARCHAR(20) DEFAULT '' NOT NULL,
USER_PASSWORD VARCHAR(40) DEFAULT '' NOT NULL,
primary key(USER_ID));

I then create a generator for the table:
CREATE GENERATOR USER_ID_GEN;

In my "Delphi" code I execute SQL to get my next USER_ID:

qSQL.SQL.Clear;
qSQL.SQL.Append('SELECT gen_id(USER_ID_GEN, 1) AS NEXT_ID FROM RDB$DATABASE');
qSQL.Open;
qSQL.First;
Result := qSQL.FieldByName('NEXT_ID').AsInteger;
qSQL.Close

Hope this helps.

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesFirebird SQL Development > Auto-generated ids


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway
Stay green...Green IT