tags:

views:

1543

answers:

3

Earlier we were using 'GENERATED ALWAYS' for generating the values for a primary key. But now it is suggested that we should, instead of using 'GENERATED ALWAYS' , use sequence for populating the value of primary key. What do you think can be the reason of this change? It this just a matter of choice?

Earlier Code:

CREATE TABLE SCH.TAB1
 (TAB_P         INTEGER         NOT NULL GENERATED ALWAYS AS IDENTITY (START WITH 1, INCREMENT BY 1, NO CACHE),
.
.
);

Now it is

CREATE TABLE SCH.TAB1
 (TAB_P         INTEGER ),
.
.
);

now while inserting, generate the value for TAB_P via sequence.

A: 

This might probably be to handle ids in case there are lots of deletes on the table.

For eg: In case of identity, if your ids are 1 2 3

Now if you delete record 3, your table will have 1 2

And then if your insert a new record, the ids will be 1 2 4

As opposed to this, if you are not using an identity column and are generating the id using code, then after delete for the new insert you can calculate id as max(id) + 1, so the ids will be in order 1 2 3

I can't think of any other reason, why an identity column should not be used.

Rashmi Pandit
max(id) + 1 ?what will happen if ID values are 1,2,3,4and now if 2 is deleted, then even by your case the next value of ID will be '5', also i said "generate the value for TAB_P via sequence", not by any calculation.
Rakesh Juyal
In that case, it will be 1,3,4,5 unless action is taken to re-adjust the id values for all rows after the deleted row which in itself will be a very time-consuming task for large no of records.
Rashmi Pandit
+1  A: 

I tend to use identity columns more than sequences, but I'll compare the two for you.

Sequences can generate numbers for any purpose, while an identity column is strictly attached to a column in a table.

Since a sequence is an independent object, it can generate numbers for multiple tables (or anything else), and is not affected when any table is dropped. When a table with a identity column is dropped, there is no memory of what value was last assigned by that identity column.

A table can have only one identity column, so if you want to want to record multiple sequential numbers into different columns in the same table, sequence objects can handle that.

The most common requirement for a sequential number generator in a database is to assign a technical key to a row, which is handled well by an identity column. For more complicated number generation needs, a sequence object offers more flexibility.

Fred Sobotka
+1 for "Since a sequence is an independent object, it can generate numbers for multiple tables (or anything else), and is not affected when any table is dropped"
Rakesh Juyal
I also would like to point out that you have the option of using an identity column that is GENERATED BY DEFAULT. This will allow you to migrate in old data without having its identity value clobbered.
Fred Sobotka
A: 

Heres something I found on the publib site:

Comparing IDENTITY columns and sequences

While there are similarities between IDENTITY columns and sequences, there are also differences. The characteristics of each can be used when designing your database and applications.

An identity column has the following characteristics:

  • An identity column can be defined as part of a table only when the table is created. Once a table is created, you cannot alter it to add an identity column. (However, existing identity column characteristics might be altered.)
  • An identity column automatically generates values for a single table.
  • When an identity column is defined as GENERATED ALWAYS, the values used are always generated by the database manager. Applications are not allowed to provide their own values during the modification of the contents of the table.

A sequence object has the following characteristics:

  • A sequence object is a database object that is not tied to any one table.
  • A sequence object generates sequential values that can be used in any SQL or XQuery statement.
  • Since a sequence object can be used by any application, there are two expressions used to control the retrieval of the next value in the specified sequence and the value generated previous to the statement being executed. The PREVIOUS VALUE expression returns the most recently generated value for the specified sequence for a previous statement within the current session. The NEXT VALUE expression returns the next value for the specified sequence. The use of these expressions allows the same value to be used across several SQL and XQuery statements within several tables.

While these are not all of the characteristics of these two items, these characteristics will assist you in determining which to use depending on your database design and the applications using the database.

Rashmi Pandit