2012-08-08 57 views
1

我得到一個異常查找約束父鍵

ORA-02291:完整性約束(ADS.ADS_JOB_FAMILIES_F03)違反 - 父鍵沒有找到

我找到了約束表中插入上午。

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

注:我使用的是Oracle

+0

您是否試圖確定主鍵約束的名稱?主鍵約束定義的列(s)?或者找不到的父鍵的值?或者你想找到別的東西嗎? – 2012-08-08 16:25:21

+0

只是想要一個查詢,它會提取我父鍵列和外鍵列。 – ashwinsakthi 2012-08-09 14:43:54

回答

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'; 
+1

感謝Hol..this正是我想要的查詢。 – ashwinsakthi 2012-08-10 06:48:37