Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Oracle Create Table AS and table comments and column comments

Tags:

sql

oracle

Is it possible to create another table as CREATE TABLE AS and also preserve columns' comments ?


CREATE TABLE TABLE1_COPY AS SELECT * FROM TABLE1;

The previous statement does not include columns' comments. Therefore TABLE1_COPY is left without columns' comments. Is using USER_COL_COMMENTS the only way to reproduce the same comments on my newly created table too?

like image 530
reforrer Avatar asked Aug 02 '11 09:08

reforrer


People also ask

What is comment on column in Oracle?

Purpose. Use the COMMENT statement to add a comment about a table, view, materialized view, or column into the data dictionary. To drop a comment from the database, set it to the empty string ' '.

What is comment on column?

Adds, revises, or removes a projection column comment. You can only add comments to projection columns, not to table columns. Each object can have one comment. Comments are stored in the system table COMMENTS .


1 Answers

As for DMBS_METADATA.GET_DDL it doesn't seem to genereate COMMENT ON COLUMN statements unless I am missing some properties.

One method is to use dbms_metadata.get_dependent_ddl in combination with dbms_metadata.get_ddl

Here is an example created using SQL plus:

SQL> set long 1000000

SQL> create table t (x number);

Table created.

SQL> comment on column T.X IS 'this is the column comment';

Comment created.

SQL> comment on table T IS 'this is the table comment';

Comment created.

SQL> SELECT dbms_metadata.get_ddl( 'TABLE', 'T' ) || ' ' ||
  2         dbms_metadata.get_dependent_ddl( 'COMMENT', 'T', USER ) the_ddl
  3  FROM dual
  4  /

THE_DDL
--------------------------------------------------------------------------------

  CREATE TABLE "SCOTT"."T"
   (    "X" NUMBER
   ) SEGMENT CREATION DEFERRED
  PCTFREE 10 PCTUSED 40 INITRANS 1 MAXTRANS 255 NOCOMPRESS LOGGING
  TABLESPACE "USERS"

   COMMENT ON COLUMN "SCOTT"."T"."X" IS 'this is the column comment'

   COMMENT ON TABLE "SCOTT"."T"  IS 'this is the table comment'
like image 143
Ian Carpenter Avatar answered Oct 08 '22 11:10

Ian Carpenter