在不知道INFORMIX中的外键名称的情况下,在两个表的列之间删除外部约束

时间:2017-09-28 10:34:29

标签: mysql informix

我正在使用Informix数据库。我已经在表中添加了一些外键而没有给它们命名,例如。

ALTER TABLE myreport
  ADD CONSTRAINT FOREIGN KEY (rid) REFERENCES report(id),
  ADD CONSTRAINT FOREIGN KEY (uid) REFERENCES user(id);

现在我要删除uid表的iduser之间的外键。

我可以使用以下命令获取表格上的约束名称:

SELECT constrname
  FROM sysconstraints
 WHERE tabid = (SELECT tabid FROM systables WHERE tabname = 'myreport');

给出以下输出:

constrname  u169_201    
constrname  n169_202
constrname  n169_203
constrname  n169_204
constrname  n169_205
constrname  n169_206
constrname  n169_207
constrname  r169_276
constrname  r169_277

我不知道哪些约束属于uidid user表之间的外键。

2 个答案:

答案 0 :(得分:2)

尝试使用driver.requestData()目录表。这样的事情可以为您提供所需的信息:

sysreferences

答案 1 :(得分:1)

我尝试过它的工作:

CREATE PROCEDURE INFORMIX.REMOVE_FK()
    DEFINE tmpvr varchar(250);
    FOREACH cur1 FOR select  b.constrname into tmpvr
            from systables a, sysconstraints b, sysindexes c, syscolumns d
            WHERE a.tabname = 'myreport' AND (d.colname='uid' or d.colname='rid')
            AND b.tabid = a.tabid AND c.idxname = b.idxname AND d.tabid = a.tabid AND (
                d.colno = c.part1 or d.colno = c.part2 or d.colno = c.part3 or d.colno = c.part4 or
                d.colno = c.part5 or d.colno = c.part6 or d.colno = c.part7 or d.colno = c.part8 or
                d.colno = c.part9 or d.colno = c.part10 or d.colno = c.part11 or d.colno = c.part12 or
                d.colno = c.part13 or d.colno = c.part14 or d.colno = c.part15 or d.colno = c.part16
            )
    EXECUTE IMMEDIATE 'ALTER TABLE myreport DROP CONSTRAINT (' || tmpvr || ')';
    END FOREACH
END PROCEDURE;
EXECUTE PROCEDURE INFORMIX.REMOVE_FK();
DROP PROCEDURE INFORMIX.REMOVE_FK();