Oracle comment新增註釋

lhrbest發表於2019-04-26

Oracle comment新增註釋


   Oracle COMMENT 語句可以給一個列、表、檢視或快照新增一個最多 2K 位元組的註釋。註釋被儲存在資料字典中,並且可以通過資料字典檢視 DBA_COL_COMMENTS (列的註釋)和 DBA_TAB_COMMENTS (表的註釋)檢視 COMMENTS 列。 COMMENT 語句的語法:

COMMENT ON TABLE tb | COLUMN tb.cols IS 'text';

其中, tb 是表的名字, cols 是表中列的名字, text 是註釋的文字。可以用設定註釋為空串( '' )的辦法從資料庫中刪除一個註釋,例如:

COMMENT ON TABLE employees IS '';
COMMENT ON TABLE HR.employees IS 'Employee Information';
COMMENT ON COLUMN HR.employees.FIRST_NAME IS 'Employee FIRST_NAME';

對於 SYS 使用者下的表,只有擁有 ALTER 許可權的普通使用者才可以對其進行 COMMENT 操作。對於普通使用者下的表,擁有“ COMMENT ANY TABLE ”或 ALTER 許可權的普通使用者都可以執行 COMMENT 操作。示例如下:

SYS@test18c> create table t as select * from dual;
 
Table created.
 
SYS@test18c> create user lhr33 identified by lhr;
 
User created.
 
SYS@test18c> grant create session to lhr33;
 
Grant succeeded.
 
SYS@test18c> GRANT SELECT ON sys.t to lhr33;
 
Grant succeeded.
 
SYS@test18c> conn lhr33/lhr
Connected.
LHR33@test18c> 
LHR33@test18c> select * from sys.t;
 
DU
--
X
 
LHR33@test18c> comment on column sys.t.dummy is 'aa';
comment on column sys.t.dummy is 'aa'
                      *
ERROR at line 1:
ORA-01031: insufficient privileges
 
 
LHR33@test18c> comment on table sys.t is 'aa';
comment on table sys.t is 'aa'
                     *
ERROR at line 1:
ORA-01031: insufficient privileges
 
 
LHR33@test18c> conn / as sysdba
Connected.
SYS@test18c> grant alter on sys.t to lhr33;
 
Grant succeeded.
 
SYS@test18c> conn lhr33/lhr
Connected.
 
LHR33@test18c> 
LHR33@test18c> comment on table sys.t is 'aa';
 
Comment created.
 
LHR33@test18c> comment on column sys.t.dummy is 'aa';
 
Comment created.
 
LHR33@test18c> conn / as sysdba
Connected.
SYS@test18c> revoke alter on sys.t  from lhr33;
 
Revoke succeeded.
 
SYS@test18c> conn lhr33/lhr
Connected.
LHR33@test18c> comment on column sys.t.dummy is 'aa';
comment on column sys.t.dummy is 'aa'
                      *
ERROR at line 1:
ORA-01031: insufficient privileges
 
 
LHR33@test18c> conn / as sysdba
Connected.
SYS@test18c> grant COMMENT ANY TABLE   to lhr33;
 
Grant succeeded.
 
SYS@test18c> conn lhr33/lhr
Connected.
LHR33@test18c> comment on column sys.t.dummy is 'aa';
comment on column sys.t.dummy is 'aa'
                      *
ERROR at line 1:
ORA-01031: insufficient privileges
 
 
LHR33@test18c> comment on table sys.t is 'aa';
comment on table sys.t is 'aa'
                     *
ERROR at line 1:
ORA-01031: insufficient privileges
 
 
LHR33@test18c> conn / as sysdba
Connected.
SYS@test18c> grant alter on sys.t to lhr33;
 
Grant succeeded.
 
SYS@test18c> conn lhr33/lhr
Connected.
LHR33@test18c> comment on table sys.t is 'aa';
 
Comment created.

對於普通使用者下的表,擁有 COMMENT ANY TABLE ”或 ALTER 許可權的使用者都可以執行 COMMENT 操作:

LHR33@test18c> conn lhr/lhr
Connected.
LHR@test18c> create table bb as select * from dual;
 
Table created.
 
LHR@test18c> conn lhr33/lhr
Connected.
LHR33@test18c>  comment on table lhr.bb is 'bb';
 
Comment created.
 
LHR33@test18c> select * from lhr.bb;
select * from lhr.bb
                  *
ERROR at line 1:
ORA-01031: insufficient privileges
 
 
LHR33@test18c> conn / as sysdba
Connected.
SYS@test18c> revoke COMMENT ANY TABLE from lhr33;
 
Revoke succeeded.
 
SYS@test18c> conn lhr33/lhr
Connected.
LHR33@test18c>  comment on table lhr.bb is 'bb';
 comment on table lhr.bb is 'bb'
                      *
ERROR at line 1:
ORA-00942: table or view does not exist
 
 
LHR33@test18c> conn / as sysdba
Connected.
SYS@test18c> grant select on lhr.bb to lhr33;
 
Grant succeeded.
 
SYS@test18c> conn lhr33/lhr
Connected.
LHR33@test18c>  comment on table lhr.bb is 'bb';
 comment on table lhr.bb is 'bb'
                      *
ERROR at line 1:
ORA-01031: insufficient privileges
 
LHR33@test18c> conn lhr/lhr
Connected.
LHR@test18c> grant alter on lhr.bb to lhr33;
 
Grant succeeded.
 
LHR@test18c> conn lhr33/lhr
Connected.
LHR33@test18c>  comment on table lhr.bb is 'bb';
 
Comment created.

檢視 DBA_COL_COMMENTS DBA_TAB_COMMENTS 在做開發時非常實用,舉例如下:

create table SCOTT.G_PROD_USER_CONF
(
  func_type     VARCHAR2(20) not null,
  func_sub_type VARCHAR2(20) not null,
  userid        VARCHAR2(20) not null,
  username      VARCHAR2(50) not null,
  sendtype      VARCHAR2(20) not null,
  email_address VARCHAR2(500)
);
-- Add comments to the table 
comment on table SCOTT.G_PROD_USER_CONF is '系統功能人員配置';
-- Add comments to the columns 
comment on column SCOTT.G_PROD_USER_CONF.func_type  is '功能型別 ';
comment on column SCOTT.G_PROD_USER_CONF.func_sub_type  is '功能子型別 1=收件人 2=抄送人 3=密送人 4=發件人';
comment on column SCOTT.G_PROD_USER_CONF.userid is '員工工號';
comment on column SCOTT.G_PROD_USER_CONF.username is '員工姓名';
comment on column SCOTT.G_PROD_USER_CONF.sendtype is '傳送型別1:簡訊2:郵件3:通知公告';
comment on column SCOTT.G_PROD_USER_CONF.email_address is '電子郵箱';
-- Grant/Revoke object privileges 
grant select, insert, update, delete, references, alter, index on SCOTT.G_PROD_USER_CONF to PUBLIC;
通過檢視可以查詢出一些有用的SQL語句:
SELECT * FROM DBA_TAB_COMMENTS D WHERE D.TABLE_NAME = 'G_PROD_USER_CONF';
SELECT 'A.' || D.COLUMN_NAME || ',',
       '--' || D.COMMENTS,
       'P_' || DTC.COLUMN_NAME || ' ' || DTC.DATA_TYPE || ',',
       'P_' || DTC.COLUMN_NAME || ' ' || DTC.DATA_TYPE || ',' || ' --' ||
       D.COMMENTS 入參,
       'A.' || D.COLUMN_NAME || ' ' || D.COLUMN_NAME || ',' || '--' ||
       D.COMMENTS 查詢,
       'P_' || D.COLUMN_NAME || ',' || '--' || D.COMMENTS 插入,
       'A.' || D.COLUMN_NAME || '=' || 'P_' || DTC.COLUMN_NAME || ', --' ||
       D.COMMENTS 更新,
       D.COLUMN_NAME || ', --' || D.COMMENTS,
       D.COMMENTS,
       DECODE(DTC.DATA_TYPE, 'DATE', 'DATE', '') DATA_TYPE,
       '--' || D.COMMENTS || CHR(10) ||
       ' v_sql := v_sql || fun_sqlparam(p_' || D.COLUMN_NAME ||
       ', '' and A.' || D.COLUMN_NAME || (CASE
         WHEN DTC.DATA_TYPE = 'VARCHAR2' THEN
          Q'[ = ''{0}'' '); ]'
           ELSE
            Q'[ = {0} ');]'
       END) WHERE條件
  FROM DBA_COL_COMMENTS D, DBA_TAB_COLS DTC
 WHERE D.TABLE_NAME = DTC.TABLE_NAME
   AND D.COLUMN_NAME = DTC.COLUMN_NAME
   AND D.OWNER = DTC.OWNER
   AND D.TABLE_NAME = 'G_PROD_USER_CONF'
   AND D.OWNER = 'SCOTT'
 ORDER BY DTC.COLUMN_ID;

返回結果,只列舉部分:







COMMENT

Purpose

Use the COMMENT statement to add to the data dictionary a comment about a table or table column, view, materialized view, operator, indextype, mining model, or edition.

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

See Also:

Prerequisites

The object about which you are adding a comment must be in your own schema or:

  • To add a comment to a table, view, or materialized view, you must have COMMENT ANY TABLE system privilege.

  • To add a comment to an indextype, you must have the CREATE ANY INDEXTYPE system privilege.

  • To add a comment to an operator, you must have the CREATE ANY OPERATOR system privilege.

  • To add a comment to an edition, you must have the CREATE ANY EDITION system privilege, granted either directly or through a role.

Syntax

comment ::=

Oracle comment新增註釋



Semantics

COLUMN Clause

Specify the name of the column of a table, view, or materialized view to be commented. If you omit schema , then Oracle Database assumes the table, view, or materialized view is in your own schema.

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 .

EDITION Clause

Specify the name of an existing edition to be commented.

You can query the data dictionary view ALL_EDITION_COMMENTS to view comments associated with editions that are accessible to the current user. You can query DBA_EDITION_COMMENTS to view comments associated with all editions in the database.

TABLE Clause

Specify the schema and name of the table or materialized view to be commented. If you omit schema , then Oracle Database assumes the table or materialized view is in your own schema.

Note:

In earlier releases, you could use this clause to create a comment on a materialized view. You should now use the COMMENT ON MATERIALIZED VIEW clause for materialized views.

INDEXTYPE Clause

Specify the name of the indextype to be commented. If you omit schema , then Oracle Database assumes the indextype is in your own schema.

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 .

MATERIALIZED VIEW Clause

Specify the name of the materialized view to be commented. If you omit schema , then Oracle Database assumes the materialized view is in your own schema.

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 .

MINING MODEL

Specify the name of the mining model to be commented. You must have the COMMENT ANY MINING MODEL system privilege to specify this clause.

OPERATOR Clause

Specify the name of the operator to be commented. If you omit schema , then Oracle Database assumes the operator is in your own schema.

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 .

IS ' string '

Specify the text of the comment. Refer to "Text Literals" for a syntax description of 'string' .

Example

Creating Comments: Example  To insert an explanatory remark on the job_id column of the employees table, you might issue the following statement:

COMMENT ON COLUMN employees.job_id 
   IS 'abbreviated job title';

To drop this comment from the database, issue the following statement:

COMMENT ON COLUMN employees.job_id IS ' ';






About Me

........................................................................................................................

● 本文作者:小麥苗,部分內容整理自網路,若有侵權請聯絡小麥苗刪除

● 本文在itpub( http://blog.itpub.net/26736162 )、部落格園( http://www.cnblogs.com/lhrbest )和個人weixin公眾號( xiaomaimiaolhr )上有同步更新

● 本文itpub地址: http://blog.itpub.net/26736162

● 本文部落格園地址: http://www.cnblogs.com/lhrbest

● 本文pdf版、個人簡介及小麥苗雲盤地址: http://blog.itpub.net/26736162/viewspace-1624453/

● 資料庫筆試面試題庫及解答: http://blog.itpub.net/26736162/viewspace-2134706/

● DBA寶典今日頭條號地址: http://www.toutiao.com/c/user/6401772890/#mid=1564638659405826

........................................................................................................................

● QQ群號: 230161599 (滿) 、618766405

● weixin群:可加我weixin,我拉大家進群,非誠勿擾

● 聯絡我請加QQ好友 646634621 ,註明新增緣由

● 於 2019-04-01 06:00 ~ 2019-04-30 24:00 在魔都完成

● 最新修改時間:2019-04-01 06:00 ~ 2019-04-30 24:00

● 文章內容來源於小麥苗的學習筆記,部分整理自網路,若有侵權或不當之處還請諒解

● 版權所有,歡迎分享本文,轉載請保留出處

........................................................................................................................

小麥苗的微店 https://weidian.com/s/793741433?wfr=c&ifr=shopdetail

小麥苗出版的資料庫類叢書 http://blog.itpub.net/26736162/viewspace-2142121/

小麥苗OCP、OCM、高可用網路班 http://blog.itpub.net/26736162/viewspace-2148098/

小麥苗騰訊課堂主頁 https://lhr.ke.qq.com/

........................................................................................................................

使用 weixin客戶端 掃描下面的二維碼來關注小麥苗的weixin公眾號( xiaomaimiaolhr )及QQ群(DBA寶典)、新增小麥苗weixin, 學習最實用的資料庫技術。

........................................................................................................................

歡迎與我聯絡

 

 



來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/26736162/viewspace-2642615/,如需轉載,請註明出處,否則將追究法律責任。

相關文章