2013-03-20 132 views
6

在假設情況下,我是沒有表創建權限的用戶。我想知道表中的某列是否有唯一約束。是否有可能在DICTIONARY?我會怎麼做呢?查找列是否有唯一約束

回答

5

這裏給出的兩個答案錯過了在列上強制唯一性的一種方法:通過創建唯一索引(沒有在列上定義唯一約束)。如果您不熟悉此選項,請參閱這兩個鏈接(one,two)。

這種檢查應該進行另外獨特的約束檢查:

select count(*) from 
USER_IND_COLUMNS cols 
where cols.table_name='YOUR_TABLE_NAME' 
and cols.COLUMN_NAME='YOUR_COLUMN'; 

要檢查唯一約束使用已經提供的方法:

select count(*) cnt 
from user_constraints uc 
where uc.table_name='YOUR_TABLE_NAME' 
and uc.constraint_type='U'; 

另外,您還可以查看ALL_CONSTRAINTSALL_IND_COLUMNS視圖。

5

唯一約束,你可以這樣做:

select cons.constraint_type, 
     all_cols.owner, all_cols.constraint_name, 
     all_cols.table_name, 
     all_cols.column_name, 
     all_cols.position 
    from all_cons_columns col 
     inner join all_cons_columns all_cols 
       on col.owner = all_cols.owner 
       and col.constraint_name = all_cols.constraint_name 
     inner join all_constraints cons 
       on col.owner = cons.owner 
       and col.constraint_name = cons.constraint_name 
where col.owner = 'SCHEMA' 
    and col.table_name = 'FOO' 
    and col.column_name = 'ID' 
    and cons.constraint_type in ('U', 'P') 
order by owner, constraint_name, position; 

設置感興趣的所有者,表和列,它會告訴你,覆蓋柱

注意所有的約束,這將不顯示列上存在唯一索引的所有情況(因爲其可能具有唯一索引而沒有約束存在)。

例如:

SQL> create table foo(id number, id2 number, constraint foo_con unique(id, id2), constraint foo_con2 unique(id)); 

Table created. 

現在列出了覆蓋id所有約束:

SQL> col column_name format a20 
SQL> col constraint_name format a20 
SQL> col table_name format a15 
SQL> select cons.constraint_type, 
    2   all_cols.owner, all_cols.constraint_name, 
    3   all_cols.table_name, 
    4   all_cols.column_name, 
    5   all_cols.position 
    6 from all_cons_columns col 
    7   inner join all_cons_columns all_cols 
    8     on col.owner = all_cols.owner 
    9    and col.constraint_name = all_cols.constraint_name 
10   inner join all_constraints cons 
11     on col.owner = cons.owner 
12    and col.constraint_name = cons.constraint_name 
13 where col.owner = user 
14  and col.table_name = 'FOO' 
15  and col.column_name = 'ID' 
16  and cons.constraint_type in ('U', 'P') 
17 order by owner, constraint_name, position; 

C OWNER       CONSTRAINT_NAME  TABLE_NAME  COLUMN_NAME   POSITION 
- ------------------------------ -------------------- --------------- -------------------- ---------- 
U DTD_TRADE      FOO_CON    FOO    ID       1 
U DTD_TRADE      FOO_CON    FOO    ID2       2 
U DTD_TRADE      FOO_CON2    FOO    ID       1 
+0

此答案不檢查唯一索引 - 請參閱我的答案以獲取詳細信息;)。 – SebastianH 2015-02-26 10:52:18

3
select count(*) cnt 
from user_constraints 
where table_name=your_table_name 
and constraint_type='U'; 

如果count = 0,則沒有UNIQUE約束別人有你的桌子上UNIQUE約束。

+0

此答案不檢查唯一索引 - 請參閱我的答案瞭解詳細信息;) – SebastianH 2015-02-26 10:52:46

0

這是我剛剛嘗試的一個查詢。它列出了每個唯一性約束,通過強制它的索引標識,列是唯一的:

select x.index_name, c.column_name, c.column_position 
from USER_INDEXES x join USER_IND_COLUMNS c 
    on x.index_name = c.index_name and x.table_name = c.table_name 
    left join USER_CONSTRAINTS uc 
    on x.index_name = uc.index_name and x.table_name = uc.table_name 
where x.status = 'VALID' and 
     (x.uniqueness = 'UNIQUE' or 
     uc.constraint_type = 'U' and uc.status = 'ENABLED' and uc.validated = 'VALIDATED') 
     and x.table_name='<your table name_in_caps>' 
order by x.index_name, c.column_position; 

這似乎對主鍵,唯一索引工作,並添加唯一性約束。