我有一个带有id
列的DB2表,它有一个自动增量,这里是代码:
"id" BIGINT GENERATED BY DEFAULT AS IDENTITY(START WITH 1)
我手动插入已经有id
值且不以1开头的值。
现在当我向这个表添加记录时,它从1开始。我想在我的记录结束时开始。即,说最新记录有id 23,我希望新记录的id = 24。
我能用最少的努力在所有桌面上做到这一点吗?
答案 0 :(得分:1)
您可以使用
db2 alter table <table_name> alter column <column_name> drop identity
和
db2 alter table <table_name> alter column <column_name> set generated always as identity (start with <max(column_identity_name)>)
-
user@host:/home/db2inst1:>db2 "CREATE TABLE TEST (ID BIGINT GENERATED BY DEFAULT AS IDENTITY(START WITH 1), NAME CHAR(6))"
insert into test (NAME) VALUES ('test1')"
insert into test (NAME) VALUES ('test2')"
insert into test (NAME) VALUES ('test3')"
insert into test (NAME) VALUES ('test4')"
insert into test (NAME) VALUES ('test5')"
insert into test (NAME) VALUES ('test6')"
insert into test (NAME) VALUES ('test7')"
insert into test (ID,NAME) VALUES (4,'test8')"
insert into test (ID,NAME) VALUES (4,'test9')"
insert into test (ID,NAME) VALUES (4,'test10')"
insert into test (ID,NAME) VALUES (4,'test11')"
insert into test (ID,NAME) VALUES (4,'test12')"
insert into test (ID,NAME) VALUES (2,'test13')"
insert into test (ID,NAME) VALUES (2,'test14')"
insert into test (ID,NAME) VALUES (2,'test15')"
insert into test (ID,NAME) VALUES (3,'test16')"
insert into test (NAME) VALUES ('test17')"
insert into test (NAME) VALUES ('test18')"
insert into test (NAME) VALUES ('test19')"
insert into test (NAME) VALUES ('test20')"
insert into test (NAME) VALUES ('test21')"
insert into test (NAME) VALUES ('test22')"
insert into test (NAME) VALUES ('test23')"
insert into test (NAME) VALUES ('test24')"
-
user@host:/home/db2inst1:>db2 "select row_number() over (order by ID) as ROWID,ID,NAME from test"
ID NAME
-------------------- -------------------- ------
1 1 test1
2 2 test2
3 2 test13
4 2 test14
5 2 test15
6 3 test3
7 3 test16
8 4 test4
9 4 test8
10 4 test9
11 4 test10
12 4 test11
13 4 test12
14 5 test5
15 6 test6
16 7 test7
17 8 test17
18 9 test18
19 10 test19
20 11 test20
21 12 test21
22 13 test22
23 14 test23
24 15 test24
24 record(s) selected.
user@host:/home/db2inst1:>db2 alter table test alter column id drop identity
DB20000I The SQL command completed successfully.
我从max row_number找到起始值;
user@host:/home/db2inst1:>db2 "alter table test alter column id set generated always as identity (start with 25)"
DB20000I The SQL command completed successfully.
insert into test (NAME) VALUES ('test25')"
DB20000I The SQL command completed successfully.
user@host:/home/db2inst1:>db2 "select row_number() over (order by ID) as ROWID,ID,NAME from test"
ROWID ID NAME
-------------------- -------------------- ------
1 1 test1
2 2 test2
3 2 test13
4 2 test14
5 2 test15
6 3 test3
7 3 test16
8 4 test4
9 4 test8
10 4 test9
11 4 test10
12 4 test11
13 4 test12
14 5 test5
15 6 test6
16 7 test7
17 8 test17
18 9 test18
19 10 test19
20 11 test20
21 12 test21
22 13 test22
23 14 test23
24 15 test24
25 25 test25
25 record(s) selected.
答案 1 :(得分:0)
基于@mustaccio评论,最直接的目标是:
ALTER TABLE "tableName" ALTER COLUMN "columnName" RESTART WITH <new index value>