5

我们有一个名为 asamembr 的表,其中有两个字段:cust_code 和 mbrcode。

还有另一个表成员消息与外键具有相同的字段,但是当我使用以下查询来创建约束时:

alter table 'informix'.messageclubmembership add constraint foreign key 
            (membership_number, member_code)
            references 'informix'.asamembr
            (cust_code, mbr_code) 
            on delete cascade 
            constraint fk_messageclubm926;

我收到此错误:

 Cannot find unique constraint or primary key on referenced table (informix.asamembr)

你能告诉如何在两个字段cust_code和mbr_code上查询表asamembr上是否存在主键吗?

4

2 回答 2

6

首先查找 PK 的索引名称(pk_idx 列)

select c.constrname, c.constrtype as tp , c.idxname as pk_idx , t2.tabname, c2.idxname
from sysconstraints c, systables t, outer (sysreferences r, systables t2, sysconstraints c2)
where t.tabname = "asamembr"
  and t.tabid = c.tabid
  and r.constrid = c.constrid
  and t2.tabid = r.ptabid
  and c2.constrid = r.constrid

其中 constrtype :

constrtype CHAR(1) 标识约束类型的代码:
C = 检查约束
N = 非 NULL
P = 主键
R = 引用
T = 表
U = 唯一

然后,检查索引列(查找与 PK 约束相同的索引名称):

   select unique
        t.tabname
      , i.idxname
      , i.idxtype
      , (select c.colname from syscolumns c where c.tabid = i.tabid and c.colno = i.part1 )
      , (select c.colname from syscolumns c where c.tabid = i.tabid and c.colno = i.part2 )
      , (select c.colname from syscolumns c where c.tabid = i.tabid and c.colno = i.part3 )
      , (select c.colname from syscolumns c where c.tabid = i.tabid and c.colno = i.part4 )
      , (select c.colname from syscolumns c where c.tabid = i.tabid and c.colno = i.part5 )
      , (select c.colname from syscolumns c where c.tabid = i.tabid and c.colno = i.part6 )
      , (select c.colname from syscolumns c where c.tabid = i.tabid and c.colno = i.part7 )
      , (select c.colname from syscolumns c where c.tabid = i.tabid and c.colno = i.part8 )
      , (select c.colname from syscolumns c where c.tabid = i.tabid and c.colno = i.part9 )
      , (select c.colname from syscolumns c where c.tabid = i.tabid and c.colno = i.part10)
      , (select c.colname from syscolumns c where c.tabid = i.tabid and c.colno = i.part11)
      , (select c.colname from syscolumns c where c.tabid = i.tabid and c.colno = i.part12)
      , (select c.colname from syscolumns c where c.tabid = i.tabid and c.colno = i.part13)
      , (select c.colname from syscolumns c where c.tabid = i.tabid and c.colno = i.part14)
      , (select c.colname from syscolumns c where c.tabid = i.tabid and c.colno = i.part15)
      , (select c.colname from syscolumns c where c.tabid = i.tabid and c.colno = i.part16)
      from sysindexes i , systables t
      where i.tabid = t.tabid
        and t.tabname = "asamembr";

其中 idxtype:

idxtype CHAR(1) 索引类型:
U = 唯一
D = 允许重复
G = 非位图通用
g = 通用位图
u = 唯一,位图
d = 非唯一,位图

Informix 在线手册中搜索“sysconstraints”或“sysindexes”

于 2013-10-06T01:59:14.083 回答
2

要查看表 asamembr 的表模式,您可以在命令行中使用 dbschema:

dbschema –d yourdbname –t asamembr

有关一些示例,请参见此处

或者你可以去那里dbaccess yourdbname > Table > Info > asamembr 查看表格信息,但我更喜欢使用 dbschema,因为它会在一个地方显示所有内容。

关于您的错误,引用的列(或使用多列约束格式时的列集,这是您的情况)必须具有唯一或主键约束。在您的情况下,情况似乎并非如此。在此处查看更多信息

于 2013-10-05T11:48:56.217 回答