当我更改标识列的属性时,已经填写的表的标识列的值会保留吗?
我有两个带有一个INTEGER标识列的表。在一个表table1
中,我只想禁用此属性。在另一个表table2
中,我想更改身份计数器的值范围。
执行此操作的想法如下:
db2 "alter table <table1> ALTER COLUMN <column> drop IDENTITY"
db2 "alter table <table2> ALTER COLUMN <column> drop IDENTITY"
db2 "alter table <table2> ALTER COLUMN <column> set generated always as identity
(START WITH 1, INCREMENT BY 1, MINVALUE 1, MAXVALUE 65536, CYCLE, NO CACHE, NO ORDER)".
由于我仅更改表1中列的属性并增加表2中列的值范围,因此我希望保留受影响列中的值。还是将所有值始终都设置为空(带有删除IDENTITY)?
答案 0 :(得分:2)
自己测试很容易,不是吗?
[db2inst1@somehost ~]$ db2 "create table test(f1 int not null generated always as identity, f2 varchar(20))"
DB20000I The SQL command completed successfully.
[db2inst1@somehost ~]$ db2 "insert into test (f2) values ('foobar')"
DB20000I The SQL command completed successfully.
[db2inst1@somehost ~]$ db2 "insert into test (f2) values ('foobar')"
DB20000I The SQL command completed successfully.
[db2inst1@somehost ~]$ db2 "select * from test"
F1 F2
----------- --------------------
1 foobar
2 foobar
2 record(s) selected.
[db2inst1@somehost ~]$ db2 "alter table test alter column f1 drop identity"
DB20000I The SQL command completed successfully.
[db2inst1@somehost ~]$ db2 "select * from test"
F1 F2
----------- --------------------
1 foobar
2 foobar
2 record(s) selected.
因此,不会,以前的IDENTITY
的现有值将不会被修改。
但是请注意,如果您以table2
的显示方式重新创建身份,它将至少生成一些与现有值重叠的值,从而导致重复的“身份”或插入异常。
[db2inst1@somehost ~]$ db2 "alter table test alter column f1 set generated always as identity
> (START WITH 1, INCREMENT BY 1, MINVALUE 1)"
DB20000I The SQL command completed successfully.
[db2inst1@somehost ~]$ db2 "insert into test (f2) values ('foobar')"
DB20000I The SQL command completed successfully.
[db2inst1@somehost ~]$ db2 "select * from test"
F1 F2
----------- --------------------
1 foobar
2 foobar
1 foobar
3 record(s) selected.