2

I need an Oracle DB query where I can get the Primary keys, Data type and the length of an Attribute in BYTES.

Currently I am doing:

SELECT cols.table_name, cols.column_name
FROM all_constraints cons, all_cons_columns cols
WHERE cons.constraint_type = 'P' 
  AND cons.constraint_name = cols.constraint_name
  AND cons.owner like 'DBP%';

where I am getting the table name, column name which is a primary key. Now I need the data type and the length of the column in BYTES.

1 Answer 1

1

Join with all_tab_columns to get the data type and (maximum) length in bytes.

select
    cols.table_name,
    cols.column_name,
    tab_cols.data_type,
    tab_cols.data_length
from all_constraints cons
join all_cons_columns cols
    on cons.owner = cols.owner
    and cons.constraint_name = cols.constraint_name
join all_tab_columns tab_cols
    on cols.owner = tab_cols.owner
    and cols.table_name = tab_cols.table_name
    and cols.column_name = tab_cols.column_name
where cons.constraint_type = 'P'
    and cons.owner like 'DBP%';

You didn't specify but I assume you want the maximum length. If you want the actual used size for the column you could use AVG_COL_LENGTH and multiply it by NUM_ROWS in ALL_TABLES.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.