本文介绍了DB2 自动生成的列/GENERATED ALWAYS 在序列上的优缺点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

之前我们使用GENERATED ALWAYS"来生成主键的值.但是现在建议我们应该使用序列来填充主键的值,而不是使用 'GENERATED ALWAYS' .您认为这种变化的原因可能是什么?这只是一个选择问题?

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?

早期代码:

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

现在是

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

现在在插入时,通过序列生成 TAB_P 的值.

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

推荐答案

我倾向于使用标识列而不是序列,但我会为你比较两者.

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.

这篇关于DB2 自动生成的列/GENERATED ALWAYS 在序列上的优缺点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-03 01:03