2014-01-27 59 views
0

需要從沒有主鍵的模式獲取表名列表。我嘗試了下面的查詢來獲取它,但它會列出除主鍵以外的所有其他鍵。沒有主鍵的表的列表

SELECT a.constraint_name,a.table_name 
FROM ALL_CONS_COLUMNS A 
JOIN ALL_CONSTRAINTS C 
ON A.CONSTRAINT_NAME = C.CONSTRAINT_NAME 
WHERE 
C.CONSTRAINT_TYPE not in('P') 
and a.owner ='my_schema'; 

回答

2

如果你只希望這對當前用戶,最好是用戶user_xxx意見,而不是all_xxx意見。

以下應該做你想要什麼:

select ut.table_name 
from user_tables ut 
where not exists (select 1 
        from user_constraints ac 
        where ac.table_name = ut.table_name 
        and ac.constraint_type = 'P' 
       ); 

如果你確實需要這對於不同的用戶,那麼你可以使用下面的。

select at.* 
from all_tables at 
where not exists (select 1 
        from all_constraints ac 
        where ac.owner = at.owner 
        and ac.table_name = at.table_name 
        and ac.constraint_type = 'P' 
       ) 
and at.owner = 'MY_SCHEMA'; 

不要忘了Oracle是大小寫敏感的用戶名稱存儲在大寫,所以a.owner ='my_schema'將最有可能不會返回任何東西。

0

嘗試這樣,

SELECT table_name 
FROM all_tables A 
WHERE table_name NOT IN 
    (
    SELECT table_name 
    FROM all_constraints WHERE constraint_type ='P' 
    ) 
AND a.owner = 'my_schema'; 
0

另一種方式:

select owner,table_name 
from all_tables 
where owner = 'my_schema' 
MINUS 
select owner,table_name 
from all_constraints 
where owner = 'my_schema' 
and constraint_type = 'P'