|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
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? |
|
#2
|
|||
|
|||
|
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 |
|
#3
|
|||
|
|||
|
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. |
|
#4
|
|||
|
|||
|
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! ![]() |
|
#5
|
||||
|
||||
|
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! |
|
#6
|
|||
|
|||
|
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. |
![]() |
| Viewing: Dev Shed Forums > Databases > Firebird SQL Development > Auto-generated ids |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|