我需要重命名我的表的序列。有很多表,它們很復雜,不希望丟棄任何東西。有沒有辦法重命名它們?
我試過了:
ALTER SEQUENCE ISEQ$$_149698 RENAME TO NEW_SEQUENCE;
RENAME ISEQ$$_149698 to NEW_SEQUENCE;
第一個選項引發以下錯誤:
SQL Error [2286] [42000]: ORA-02286: no options specified for ALTER SEQUENCE
第二:
SQL Error [32799] [99999]: ORA-32799: cannot rename a system-generated sequence
uj5u.com熱心網友回復:
您不能重命名為標識列生成的序列。(正如其他用戶所指出的,并且正如錯誤訊息所暗示的那樣。)因此,我建議您使用序列默認值而不是標識列。
例如:
--Create the sequence and a table to use it.
create sequence my_sequence;
create table my_table(a number default my_sequence.nextval, b number);
--Rename the sequence to whatever you want.
rename my_sequence to my_sequence2;
但是,默認方法有一些缺點:
- 此功能在 12.1 之前不可用。(雖然標識列也是一項新功能。)
- 您必須自己創建序列(顯然)。
- 您還需要記住將序列授予將向表中插入行的任何用戶:
grant insert, select on my_table to test_user;
grant select on my_sequence to test_user;
- 如果重命名默認序列,您還必須修改默認值以指向新序列。
--Afate a sequence rename, this INSERT fails with: ORA-02289: sequence does not exist
insert into my_table(b) values(1);
--You must first modify the table:
alter table my_table modify a number default my_sequence2.nextval;
--Now this will run:
insert into my_table(b) values(1);
盡管使用序列默認值有缺點,但我仍然更喜歡這種方法而不是標識列,因為我希望我的所有物件在每個環境中都具有完全相同的名稱。
uj5u.com熱心網友回復:
rename old_seq to new_sequence;
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/428101.html
