2016-07-15 2 views
0

У меня есть таблица DB2 с id столбец, который имеет автоматическое приращение, вот код:Как сделать столбец IDENTITY `id`, чтобы начать с другого значения?

"id" BIGINT GENERATED BY DEFAULT AS IDENTITY(START WITH 1) 

я вручную вставить значения, которые уже имеют id значения и не начинаются с 1. Теперь, когда я добавить записи к этой таблице он начинается с 1. Я хотел бы начать, когда мои записи закончатся. Например, у последней записи есть id 23, я бы хотел, чтобы новая запись имела id = 24.

Есть ли способ I может сделать это на всех моих столиках с минимальными усилиями?

+1

Смотрите, если это помогает: http://stackoverflow.com/a/32969802/1227152 – mustaccio

+0

https: // publib.boulder.ibm.com/iseries/v5r2/ic2924/index.htm?info/sqlp/rbafymst79.htm – Esperento57

ответ

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)>) 

-

[email protected]:/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')" 

-

[email protected]:/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. 

[email protected]:/home/db2inst1:>db2 alter table test alter column id drop identity 
DB20000I The SQL command completed successfully. 

I найдено начальное значение из max row_number;

[email protected]:/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. 
[email protected]:/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. 
+2

Вам не нужно бросать личность, вы можете просто «ОТПРАВИТЬ» его. – mustaccio

+0

Да, вы правы –

+0

Мне нужны были две первые строки вашего ответа. Возможно, отредактируйте свой ответ, чтобы отразить комментарий mustaccio? – John

0

на основе @mustaccio комментарий, самый простой для достижения этой цели является:

ALTER TABLE "tableName" ALTER COLUMN "columnName" RESTART WITH <new index value> 
Смежные вопросы