Tuesday, June 10, 2008

Comments in Oracle

You can comments to any SQL statements or any schema objects.

A)Comments Within SQL Statements
----------------------------------------

Comments within SQL statements do not affect the statement execution. The only exception is use of hints. With use hints the SQL statements is affected. In fact comment is used to make your application easier for you to read and maintain.

Within SQL statements you can include comment in two ways.
1)With /* and */
--------------------------

Format is,
Begin with slash (/) and an asterisk (*) together (/*) + comment Text + End with an asetrisk (*) and a slash (/) together (*/).

With this format text can span multiple lines.

The opening and terminating characters need not be separated from the text by a space or a line break.

Example:
-----------
SQL> select second_col /*This is column
2 Name */ from test /*This is Table Name*/ ;


2)with --
-----------------

The text that begin with two hyphens (--) is considered as comments. The comments written in this way can't span multiple lines. End the comment with a line break.

Example:
------------
SQL> select second_col -- THIS IS COMMENT
2 FROM TEST;


B)Comments on Schema Objects
---------------------------------

You can associate a comment with a table, view, materialized view, or column using the COMMENT command. Comments associated with schema objects are stored in the data dictionary.

You can view the comments on a particular table or column by querying the data dictionary views USER_TAB_COMMENTS, DBA_TAB_COMMENTS, or ALL_TAB_COMMENTS or USER_COL_COMMENTS, DBA_COL_COMMENTS, or ALL_COL_COMMENTS.

You can view the comments on a particular operator by querying the data dictionary views USER_OPERATOR_COMMENTS, DBA_OPERATOR_COMMENTS, or ALL_OPERATOR_COMMENTS.

You can view the comments on a particular indextype by querying the data dictionary views USER_INDEXTYPE_COMMENTS, DBA_INDEXTYPE_COMMENTS, or ALL_INDEXTYPE_COMMENTS.

You can view the comments on a particular materialized view by querying the data dictionary views USER_MVIEW_COMMENTS, DBA_MVIEW_COMMENTS, or ALL_MVIEW_COMMENTS.

To drop a comment from the database, set it to the empty string ' '.

Example:
--------------

I have comment on table test and on its columns by,
SQL> COMMENT ON TABLE ARJU.TEST IS 'This is test Table';
Comment created.

SQL> COMMENT ON COLUMN ARJU.TEST.SECOND_COL IS 'This is the only one column';

Comment created.

SQL> column COMMENTS format a30
SQL> col owner format a10
SQL> col table_name format a10
SQL> select OWNER,TABLE_NAME,COMMENTS from dba_tab_comments where table_name='TEST' and owner='ARJU';


OWNER TABLE_NAME COMMENTS
---------- ---------- ------------------------------
ARJU TEST This is test Table

SQL> select OWNER,TABLE_NAME,COMMENTS from dba_col_comments where table_name='TEST' and owner='ARJU';

OWNER TABLE_NAME COMMENTS
---------- ---------- ------------------------------
ARJU TEST This is the only one column

To drop comment,
SQL> COMMENT ON COLUMN ARJU.TEST.SECOND_COL IS '';
Comment created.

SQL> select OWNER,TABLE_NAME,COMMENTS from dba_col_comments where table_name='TEST' and owner='ARJU';
OWNER TABLE_NAME COMMENTS
---------- ---------- ------------------------------
ARJU TEST

No comments:

Post a Comment