473,395 Members | 1,647 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Identity OR sequence which is better

Hello,

I would like to know Identity OR sequence which is better?.

My requirement is simple. I have a parent table with ID (generated or
from sequence) and I want that ID after inserting parent record so
that I could use it for Child table(S).

I read in on the the thread ibm is planning to deprecate
identity_val_local() function in future. Appreciate the DB2 experts
advice.

Thanks,
Srini.
Nov 12 '05 #1
3 8554
Either of them have their place.
If the key you need is local to a single table and you don't need
explicit control over it then IDENTITY gives you a few benefits.
E.g. IDENTITY can be generated by LOAD, sequences surrently cannot.

One of the few drawbacks of identity today is that you cannot switch it
OFF which can be a pain for IMPORT. This however will be addressed in
DB2 Stinger.

identity_val_local() shudl be deprecated because:
SELECT FROM INSERT in DB2 V8.1 FP4 does a much better job in retrieving
generated values (not only identity) in a set oriented fashion.
So instead of doing:
INSERT INTO parent(pk, c1) VALUES(DEFAULT, 5);
INSERT INTO child (pk, fk) VALUES(DEFAULT, identity_val_local());

you can now say:
SELECT pk INTO :pk
FROM NEW TABLE(INSERT INTO parent(pk, c1) VALUES(DEFAULT, 5);
INSERT INTO child (pk, fk) VALUES(DEFAULT, :pk);

Or even:
WITH parent AS (SELECT pk
FROM NEW TABLE(INSERT INTO parent(pk, c1)
VALUES(DEFAULT, 5))
SELECT fk INTO :parent
FROM NEW TABLE(INSERT INTO child(fk)
SELECT pk FROM parent);

One statement :-)

Cheers
Serge
--
Serge Rielau
DB2 SQL Compiler Development
IBM Toronto Lab
Nov 12 '05 #2
Hi Serge,
Thank you very much for your help.
I have learned something new which is good.
I understand i have a better control with SEQUENCE than IDENTITY.

Question -1
-----------
identity_val_local() SHOULD be deprecated or WILL be deprecated or
BOTH?

Question -2
-----------
Will IDENTITY work fine with Concurrent INSERTS.
For Example, We have two tables that are NOT related in any way.

TABLE_A has ID (GENERATED AS IDENTITY)
TABLE_B has ID (GENERATED AS IDENTITY)

USER-A will INSERT a record in TABLE_A
USER-B will INSERT a record in TABLE_B

Now if USER-A calls identity_val_local() function, How he can be sure
he is getting ID for TABLE_A and not TABLE_B. Both the user will have
their own Transaction.

Thanks for your time,
Srini.

Serge Rielau <sr*****@ca.eye-be-em.com> wrote in message news:<c0**********@hanover.torolab.ibm.com>...
Either of them have their place.
If the key you need is local to a single table and you don't need
explicit control over it then IDENTITY gives you a few benefits.
E.g. IDENTITY can be generated by LOAD, sequences surrently cannot.

One of the few drawbacks of identity today is that you cannot switch it
OFF which can be a pain for IMPORT. This however will be addressed in
DB2 Stinger.

identity_val_local() shudl be deprecated because:
SELECT FROM INSERT in DB2 V8.1 FP4 does a much better job in retrieving
generated values (not only identity) in a set oriented fashion.
So instead of doing:
INSERT INTO parent(pk, c1) VALUES(DEFAULT, 5);
INSERT INTO child (pk, fk) VALUES(DEFAULT, identity_val_local());

you can now say:
SELECT pk INTO :pk
FROM NEW TABLE(INSERT INTO parent(pk, c1) VALUES(DEFAULT, 5);
INSERT INTO child (pk, fk) VALUES(DEFAULT, :pk);

Or even:
WITH parent AS (SELECT pk
FROM NEW TABLE(INSERT INTO parent(pk, c1)
VALUES(DEFAULT, 5))
SELECT fk INTO :parent
FROM NEW TABLE(INSERT INTO child(fk)
SELECT pk FROM parent);

One statement :-)

Cheers
Serge

Nov 12 '05 #3
Srini,

Deprecation is a very fuzzy word. E.g. LONAG VARCHAR and LONG VARGRAPHIC
are deprecated. You can see that from the fact that none of the new
features coming into DB2 support these old types.
Nonetheless neither support nor documentation has been withdrawn.

identity_val_local() was from the very beginning and consciously placed
into a small box (single row insert with values).
You will not see this function being extended and it is also very
unlikely you will see an identity_val_global() or so...

Will IBM formally pull support on this function at some point? I don't
know. It's to small a fish to worry about, IMHO.
If that happens there will be ample warning though.

W.r.t. concurrency.
identity_val_local() is local to the connection. IDENTITY generation in
another connection (whether the same or different table) will not affect
its value.

Cheers
Serge
--
Serge Rielau
DB2 SQL Compiler Development
IBM Toronto Lab
Nov 12 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: DBA | last post by:
I have an identity field on a table in SQL Server. The identity seed is 1 and the identity increment is 1. If I remove a record from this table, the identity sequence is broken. For example: ...
1
by: Ken | last post by:
Need help on the Auto Number or Identity Seed on the Oracle Database I got an Access database that need to be converted to Oracle 9i. Somehow the Trigger we created to simulate the "AUTO NUMBER"...
4
by: UDBDBA | last post by:
Hi: we have column with GENERATED ALWAYS AS DEFAULT. So, we can insert into this column manually and also let db2 generate a value for this column. Given a scenario, how can i find the NEXTVAL...
41
by: pb648174 | last post by:
In a multi-user environment, I would like to get a list of Ids generated, similar to: declare @LastId int select @LastId = Max(Id) From TableMania INSERT INTO TableMania (ColumnA, ColumnB)...
4
by: shorti | last post by:
Can anyone explain in greater (and more comprehensive) detail what the RESTART option does in the ALTER TABLE table ALTER COLUMN statement. This is the description in Info Center: RESTART or...
5
by: Veeru71 | last post by:
Given a table with an identity column (GENERATED BY DEFAULT AS IDENTITY), is there any way to get the last generated value by DB2 for the identity column? I can't use identity_val_local() as...
15
by: gunnar.sigurjonsson | last post by:
I´m having some problem retrieving identity value from my newly inserted row into a view. I have two tables T1 and T2 which I define as following CREATE TABLE T1 ( id BIGINT GENERATED ALWAYS...
0
by: Frank Swarbrick | last post by:
So we're trying to decide if it's better to use IDENTITY columns or sequences to create a surrogate key as the primary key for our tables. I kind of like the identity column, because it's more...
11
by: stegze | last post by:
Hi All, I have a problem with a DB2 server of my customer. It is a Debian Linux running DB2 Express-C. I have an IDENTITY field as PK in a table and I use this value as FK in another table. Two...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.