How to view all the Metadata of columns of a table in oracle database?
Asked Answered
E

2

13

I want to know the Query that allows us to view all the columns that are defined for a table in oracle database.

Elaboration:

Table Name: Some_Table have 10 columns.

I want to know how I can retrieve the all column names, their data type, and any constraints that are defined for any column.

Ellenaellender answered 19/11, 2012 at 6:43 Comment(0)
O
27

I want to know how I can retrieve the all column names, their data type, and any constraints that are defined for any column.

To do that you can query(depending on privileges granted to you) [user|all|dba]_tab_columns, [user|all|dba]_cons_columns, [user|all|dba]_constraints views.

Here is a quick example:

 select decode( t.table_name
              , lag(t.table_name, 1) over(order by t.table_name)
              , null
             , t.table_name ) as table_name -- <- just to eliminate 
      , t.column_name                       -- repeated tab_name    
      , t.data_type
      , cc.constraint_name
      , uc.constraint_type
   from user_tab_columns t
        left join user_cons_columns cc
          on (cc.table_name = t.table_name and
              cc.column_name = t.column_name)
        left join user_constraints uc
          on (t.table_name = uc.table_name and
              uc.constraint_name = cc.constraint_name )
 where t.table_name in ('EMPLOYEES', 'DEPARTMENTS');

Result:

TABLE_NAME    COLUMN_NAME       DATA_TYPE      CONSTRAINT_NAME   CONSTRAINT_TYPE
------------- ----------------- -------------- -------------------------------
DEPARTMENTS   LOCATION_ID       NUMBER         DEPT_LOC_FK       R
              DEPARTMENT_ID     NUMBER         DEPT_ID_PK        P
              DEPARTMENT_NAME   VARCHAR2       DEPT_NAME_NN      C
              MANAGER_ID        NUMBER         DEPT_MGR_FK       R
EMPLOYEES     SALARY            NUMBER         EMP_SALARY_MIN    C
              PHONE_NUMBER      VARCHAR2                            
              EMPLOYEE_ID       NUMBER         EMP_EMP_ID_PK     P
              DEPARTMENT_ID     NUMBER         EMP_DEPT_FK       R
              JOB_ID            VARCHAR2       EMP_JOB_FK        R
              MANAGER_ID        NUMBER         EMP_MANAGER_FK    R
              COMMISSION_PCT    NUMBER                              
              FIRST_NAME        VARCHAR2                            
              JOB_ID            VARCHAR2       EMP_JOB_NN        C
              HIRE_DATE         DATE           EMP_HIRE_DATE_NN  C
              EMAIL             VARCHAR2       EMP_EMAIL_NN      C
              LAST_NAME         VARCHAR2       EMP_LAST_NAME_NN  C
              EMAIL             VARCHAR2       EMP_EMAIL_UK      U

17 rows selected

Also to retrieve a complete specification(if needed) of a table, you can use dbms_metadata package and get_ddl function of that package:

select dbms_metadata.get_ddl('TABLE', 'EMPLOYEES') as table_ddl
  from dual;

 table_ddl
 --------------------------------------------------------------------------------

  CREATE TABLE "HR"."EMPLOYEES"
   ("EMPLOYEE_ID" NUMBER(6,0),
    "FIRST_NAME" VARCHAR2(20),
    "LAST_NAME" VARCHAR2(25) CONSTRAINT "EMP_LAST_NAME_NN" NOT NULL ENABLE,
    "EMAIL" VARCHAR2(25) CONSTRAINT "EMP_EMAIL_NN" NOT NULL ENABLE,
    "PHONE_NUMBER" VARCHAR2(20),
    "HIRE_DATE" DATE CONSTRAINT "EMP_HIRE_DATE_NN" NOT NULL ENABLE,
    "JOB_ID" VARCHAR2(10) CONSTRAINT "EMP_JOB_NN" NOT NULL ENABLE,
    "SALARY" NUMBER(8,2),
    "COMMISSION_PCT" NUMBER(2,2),
    "MANAGER_ID" NUMBER(6,0),
    "DEPARTMENT_ID" NUMBER(4,0),
     CONSTRAINT "EMP_SALARY_MIN" CHECK (salary > 0) ENABLE,
     CONSTRAINT "EMP_EMAIL_UK" UNIQUE ("EMAIL")

   -- ... other attributes

   )
Outlier answered 19/11, 2012 at 7:6 Comment(5)
Hi Nicholas Krasnov, I tried your queries but I am getting error from oracle for both of your methods. When I am running the get_ddl I am getting ERROR: ORA-31603: object "XATable_1" of type TABLE not found in schema "XATRANS" ORA-06512: at "SYS.DBMS_METADATA", line 5088 ORA-06512: at "SYS.DBMS_METADATA", line 7589 ORA-06512: at line 1Favor
And while running the query I am getting this at line no 13 ORA-00904: "UC"."CONSTRAINTS_NAME": invalid identifierFavor
break on table_name would perform better in sql*plus environment ;)Doti
@NicholasKrasnov you would probably want to test it )Doti
@Aura Make sure that the table XATable_1 exists in the schema XATRANS. Here is sqlfiddle examples: query, package. 2) uc.constraint_name(singular) not plural(UC.CONSTRAINTS_NAME) as it is in your comment.Outlier
F
0

select * from all_tab_columns where table_name='YOUR_TABLE_NAME_IN_CAPS';

-- NOTE: -- all_tab_columns is a SYSTEM defined Meta Data Table. -- table_name is also a SYSTEM Command. -- Don't forget to write your table name in caps as it's case-sensitive.

Follower answered 3/2, 2023 at 14:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.