【问题标题】:Query to check if primary key exists on the table in informix在informix中查询表上是否存在主键
【发布时间】:2013-10-05 09:34:03
【问题描述】:

我们有一个名为 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)

请问如何查询主键是否存在于表 asamembr 的两个字段 cust_code 和 mbr_code 上?

【问题讨论】:

    标签: sql informix


    【解决方案1】:

    首先查找 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 CHAR(1) 标识约束类型的代码:
    C = 检查约束
    N = 非空
    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 online manuals 搜索“sysconstraints”或“sysindexes”

    【讨论】:

    • +1:我认为第一个查询可以从条件AND c.constrtype IN ('U', 'P') 中受益,因为目标是找到主键或唯一约束(将目标表指定为表名)。如果存在主键或唯一约束,那么您可能需要查找索引,如图所示。请注意,系统生成的索引名称有前导空格。
    • 请注意...我上面显示的查询是从我拥有的一些脚本中复制而来的,因此它们显示了您需要的更多内容,例如过滤后的表中某些 FK 的参考表... . 和其他约束,可以像乔纳森所说的那样过滤。
    【解决方案2】:

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

    dbschema –d yourdbname –t asamembr
    

    有关示例,请参阅here

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-07-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多