Din*_*lae 3 sql oracle sequence
我需要重命名表的序列。有很多表,而且它们很复杂,不希望删除任何内容。有没有办法给它们重命名?
我试过:
ALTER SEQUENCE ISEQ$$_149698 RENAME TO NEW_SEQUENCE;
RENAME ISEQ$$_149698 to NEW_SEQUENCE;
Run Code Online (Sandbox Code Playgroud)
第一个选项会引发以下错误:
SQL Error [2286] [42000]: ORA-02286: no options specified for ALTER SEQUENCE
Run Code Online (Sandbox Code Playgroud)
第二:
SQL Error [32799] [99999]: ORA-32799: cannot rename a system-generated sequence
Run Code Online (Sandbox Code Playgroud)
您无法重命名为标识列生成的序列。(正如其他用户所指出的,并且正如错误消息所暗示的那样。)因此,我建议您使用序列默认值而不是标识列。
例如:
--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;
Run Code Online (Sandbox Code Playgroud)
但是,默认方法有一些缺点:
grant insert, select on my_table to test_user;
grant select on my_sequence to test_user;
Run Code Online (Sandbox Code Playgroud)
--After 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);
Run Code Online (Sandbox Code Playgroud)
尽管使用默认序列有缺点,但与标识列相比,我仍然更喜欢这种方法,因为我希望所有对象在每个环境中都具有完全相同的名称。