在约束中查找父键

时间:2012-08-08 16:13:27

标签: sql oracle constraints

我收到了异常

ORA-02291:违反了完整性约束(ADS.ADS_JOB_FAMILIES_F03) - 未找到父密钥

我在表中找到了约束插入。

如何查找父键和子键...(列)。

注意:我使用的是Oracle

1 个答案:

答案 0 :(得分:2)

对于父表:

select * from all_constraints 
         where constraint_name in (
               select R_CONSTRAINT_NAME 
                 from all_constraints 
                where constraint_name = 'ADS_JOB_FAMILIES_F03'); 

对于父列:

select * 
  from all_cons_columns 
 where constraint_name in (
       select constraint_name 
         from all_constraints 
        where constraint_name in (
              select R_CONSTRAINT_NAME 
                from all_constraints 
               where constraint_name = 'ADS_JOB_FAMILIES_F03')); 

这并不完全是因为它忽略了约束的所有者。但我认为这对你有用。

编辑:我现在甚至找到了this。哪个会给出这样的查询:

SELECT a.table_name, a.column_name, a.constraint_name, c.owner, 
       -- referenced pk
       c.r_owner, c_pk.table_name r_table_name, c_pk.constraint_name r_pk
  FROM all_cons_columns a
  JOIN all_constraints c ON a.owner = c.owner
                        AND a.constraint_name = c.constraint_name
  JOIN all_constraints c_pk ON c.r_owner = c_pk.owner
                           AND c.r_constraint_name = c_pk.constraint_name
 WHERE a.constraint_name = 'ADS_JOB_FAMILIES_F03';