![]() | |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
Is there a database independent way of obtaining the generated key after the insertion? |
#3
| |||
| |||
|
|
Is there a database independent way of obtaining the generated key after the insertion? That is not how RDBMS works. By definition, a key is a subset of the attributes of an entity. The entity cannot exist without it. Your "generated key" is based on the internal state of the hardware at insertion and has nothing whatsoever with a valid data model. When you look for a key, start with your industry standards. I would estimate that this covers ~80% of the properly defined systems -- VIN, ISBN, UPC, EAN, etc. which can be verified and validated via a trusted exernal source. In the old days, this took time; you can Google it today. After that, there is a natural key in the data, such as (longitude,latitude), (store, cash register, ticket_nbr), etc. which can be verified and validated in the reality of the data. This is ~18% of the cases. if you have no natural key, then your schema is probably wrong. Then, if that fails and we are lokking at <2% of all situations, we invent a key with check digits, validation rules and an audit trail within our enterprise. This is a great screaming pain IF you do it right. This is why industry standards exists -- people got tired the pain Would you like to do Retail without standard UPC barcodes on products? We just that for centuries before barcodes. So, newbies prefer to do it wrong by using auto-increments or other proprietary stuff that cannot be verified or validated BECAUSE it is easier than real RDBMS design. They want to have a magical, universal "one-size-fits-all" answer that does not require you ACTUALLY understand the problem domain. |
#4
| |||
| |||
|
|
I am using Apache Derby which uses the following syntax to specify database generated unique key fields: CREATE TABLE MYTABLE (MYTABLE_ID INTEGER NOT NULL GENERATED BY DEFAULT AS IDENTITY, ...); I use the Derby function IDENTITY_VAL_LOCAL() to obtain the value of MYTABLE_ID for a newly inserted record. This method seems to be restricted to IBM DB2 and Derby. Is there a database independent way of obtaining the generated key after the insertion? I can live with different syntaxes for specifying the generated key. |
#5
| |||
| |||
|
|
I am using Apache Derby which uses the following syntax to specify database generated unique key fields: CREATE TABLE MYTABLE (MYTABLE_ID INTEGER NOT NULL GENERATED BY DEFAULT AS IDENTITY, ...); I use the Derby function IDENTITY_VAL_LOCAL() to obtain the value of MYTABLE_ID for a newly inserted record. This method seems to be restricted to IBM DB2 and Derby. Is there a database independent way of obtaining the generated key after the insertion? I can live with different syntaxes for specifying the generated key. |
![]() |
| Thread Tools | |
| Display Modes | |
| |