如何删除 Firebird 中的现有索引?

How to drop an existing index in Firebird?

我应该创建什么脚本来检查这个索引是否存在?因为我希望这个索引如果已经创建就被删除然后重新创建索引

CREATE INDEX IF NOT EXISTS IDX_TABLE ON TABLE (ID, DATE)

我只需要一个脚本就可以自动生成。我还没有找到 with drop_existing = ON 的替代方案,就像在 MSSQL 中那样。

不幸的是,Firebird 不允许您有条件地删除或创建其 SQL 方言中的索引。如果您按语句执行,则可以捕获错误并忽略相关的错误代码。或者,您可以使用 execute block,例如:

execute block as
begin
  if (exists(select * from rdb$indices where rdb$index_name = 'IDX_TABLE')) then
    execute statement 'drop index IDX_TABLE';
end

使用execute statement是必要的,因为PSQL(Firebird过程语言)不直接支持DDL语句。

如果您想有条件地创建索引,您可以使用:

execute block as
begin
  if (not exists(select * from rdb$indices where rdb$index_name = 'IDX_TABLE')) then
    execute statement 'create index IDX_TABLE on table (id, name)';
end

RDB$INDICES table 是 Firebird system table.