Oracle – How to Find All Unique Columns That Are Allowed Null

oracle

I am trying to write a query that displays all unique columns that are allowed to be null in Oracle.

The information I need is in these 3 tables:

  • ALL_TAB_COLUMNS
  • ALL_IND_COLUMNS
  • ALL_INDEXES

I am having difficulty joining these tables to display the information I'm looking for. Any help would be appreciated.

Looking for output such as:

OWNER, COLUMN_NAME, INDEX, UNIQUENESS, NULLABLE

WHERE NULLABLE='Y' AND UNIQUENESS='UNIQUE'

Best Answer

Try this one:

SELECT ind.OWNER AS INDEX_OWNER, ind.INDEX_NAME,
    tab.OWNER AS TABLE_OWNER, tab.table_name,
    tab.column_Name,
    UNIQUENESS, NULLABLE
FROM ALL_INDEXES ind
    JOIN ALL_IND_COLUMNS col 
        ON ind.OWNER = INDEX_OWNER 
            AND ind.INDEX_NAME = col.INDEX_NAME
    JOIN ALL_TAB_COLUMNS tab 
        ON tab.owner = col.table_owner 
            AND tab.table_name = col.table_name 
            AND tab.column_Name = col.column_Name
WHERE NULLABLE='Y' AND UNIQUENESS='UNIQUE';