不过好像很危险
并非一切都是危险的。
第一个场景——你现在正在处理的那个(增加列大小,同时有一个功能索引)——应该没问题。
SQL> create table test (id varchar2(2));
Table created.
基于函数的索引:
SQL> create index i1test on test (upper(id));
Index created.
修改列大小不起作用(如您所知):
SQL> alter table test modify (id varchar2(5));
alter table test modify (id varchar2(5))
*
ERROR at line 1:
ORA-30556: either functional or bitmap join index is defined on the column to
be modified
所以,再次删除索引/更改表/创建索引:
SQL> drop index i1test;
Index dropped.
SQL> alter table test modify (id varchar2(5));
Table altered.
SQL> create index i1test on test (upper(id));
Index created.
SQL>
它危险吗?我不这么认为。如果表没有索引,性能可能会受到影响。如果表是巨大的,那么重新创建索引需要时间,但是——除了性能——我想这还可以。
但请注意,基于函数的索引不能用于强制执行该约束:
SQL> create table test (id varchar2(2));
Table created.
SQL> create index i1test on test (upper(id));
Index created.
SQL> alter table test add constraint pk_test primary key (id) using index i1test;
alter table test add constraint pk_test primary key (id) using index i1test
*
ERROR at line 1:
ORA-14196: Specified index cannot be used to enforce the constraint.
SQL>
但是,当我们使用用于强制主键约束的索引时,是的 - 这可能很危险,因为
-
主键还强制唯一性所以 - 如果你放弃它 - 有人可能能够插入/更新表并违反唯一性
-
主键可能被一个(或多个)外键约束引用。当外键存在时,您不能删除主键约束,因为
ORA-02273: this unique/primary key is referenced by some foreign keys
所以你首先必须删除外键约束(你不能只是禁用它们),这也允许有人在那个/那些列中输入无效值
因此,这仅取决于当前情况。有时它更简单,有时它很复杂,您处理它的方式取决于复杂性。
顺便说一句,如果您要修改列的大小,您是否真的必须删除用于主键约束的索引?没有:
SQL> create table test (id varchar2(2));
Table created.
SQL> create index i1test on test (id);
Index created.
SQL> alter table test add constraint pk_test primary key (id) using index i1test;
Table altered.
SQL> alter table test modify (id varchar2(5));
Table altered.
SQL>