【问题标题】:How to increase size of column with index如何使用索引增加列的大小
【发布时间】:2021-10-07 06:00:59
【问题描述】:

我想将我的列 (NVARCHAR2) 的大小从 32 增加到 64。但我收到一个错误:

ORA-30556: 在要修改的列上定义了功能或位图连接索引

我找到了应该删除索引然后重新创建它的解决方案。但是我可以不做就做吗? (也许禁用和启用,或类似的东西)因为对我来说,当我增加列的大小时似乎没有任何意义。

但即使我尝试像上面所说的那样做,有时我也会遇到另一个错误

ORA-02429: 无法删除用于强制执行唯一/主键的索引

您可以使用 HTML 标记 和结束标记 删除线。

像这样

我想我需要让索引不可用,然后删除它并重新创建所有索引。不过好像很危险 好像我想做也行不通

【问题讨论】:

    标签: sql database oracle


    【解决方案1】:

    不过好像很危险

    并非一切都是危险的。

    第一个场景——你现在正在处理的那个(增加列大小,同时有一个功能索引)——应该没问题。

    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>
    

    【讨论】:

    • 谢谢,好像知道的更清楚了。但我不明白最后一部分。为什么alter table test add constraint pk_test primary key (id) using index i1test; 允许我改变表格,即使上面你显示没有这条线是不可能的?
    • 这个 ALTER TABLE 只是在 TEST 表上添加了主键约束,而 PK 约束使用了 ID 列上已经存在的索引。本来可以更简单的,例如create table test (id varchar2(2) primary key); 和 Oracle 将创建主键约束和(在 background 中)支持它的索引)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-25
    • 2019-10-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多