我正在使用Informix数据库。我已经在表中添加了一些外键而没有给它们命名,例如。
ALTER TABLE myreport
ADD CONSTRAINT FOREIGN KEY (rid) REFERENCES report(id),
ADD CONSTRAINT FOREIGN KEY (uid) REFERENCES user(id);
现在我要删除uid
表的id
和user
之间的外键。
我可以使用以下命令获取表格上的约束名称:
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
我不知道哪些约束属于uid
和id
user
表之间的外键。
答案 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();