oracle Get details of current user table field etc. SQL

  • 2021-12-12 10:14:30
  • OfStack

Take a note for reference only


SELECT 
d.TABLE_NAME tbName,// Table name 
COALESCE(t.COMMENTS, ' ') tbDesc, // Table annotation 
a.COLUMN_NAME columnName, // Field name 
a.DATA_TYPE columnType, // Field type 
a.DATA_LENGTH width, // Field length 
a.DATA_SCALE precision,// Field decimal place 

decode(a.NULLABLE,'Y','0','1') notNull,// Whether null is allowed 
COALESCE(m.COMMENTS, ' ') comments, // Field comments 
decode(k.uniqueness,'UNIQUE','1','0') uniques, // Whether only 1
COALESCE(k.index_name, ' ') indexName,// If it is an index, the index name 
decode(k.key,'Y','1','0') masterKey// Whether primary key 
FROM
user_tab_columns a
INNER JOIN user_tables d on a.TABLE_NAME=d.TABLE_NAME
LEFT JOIN user_tab_comments t ON t.TABLE_NAME=d.TABLE_NAME
LEFT JOIN user_col_comments m ON m.COLUMN_NAME=a.COLUMN_NAME AND m.TABLE_NAME=d.TABLE_NAME
LEFT JOIN
(
SELECT e.index_name,u.TABLE_NAME,u.COLUMN_NAME,e.uniqueness,decode(p.constraint_name,NULL,'N','Y') key
from user_indexes e INNER JOIN user_ind_columns u ON e.index_name=u.index_name
LEFT JOIN ( select constraint_name from user_constraints where constraint_type='P' ) p ON e.index_name=p.constraint_name
) k ON k.TABLE_NAME=a.TABLE_NAME and k.COLUMN_NAME=a.COLUMN_NAME
ORDER BY tbName

Remarks: user_ begins with the current user, all_ begins with all users, and dba_ begins with system tables


Related articles: