The use of annotations in Dameng database

When managing a large-scale database, we often need to face a large number of tables and views. At the same time, there may be many fields in the tables and views, which is difficult for people to quickly distinguish and is not conducive to the management of database objects. In addition to giving meaningful and easily distinguishable names to meaningful tables, views and columns when naming, adding comments to database objects is also an effective way to help us manage the database.

Adding a comment in DM can be done with a comment statement – ​​“COMMENT ON <object name> IS <comment string>”. Users can create comments for objects in their own schemas, for the entire table or view, or for individual columns. For example, to add comments to an existing table, the syntax example is as follows:

comment on table TEST.T1 is '这是表注释';

To annotate views, you need to replace the keyword table with view in the statement. For adding comments to the columns in the table, you need to replace the keyword table with column, and then replace the object when adding the table comment with the column name. For example, to add a comment to the ID column in the T1 table in TEST mode, the syntax example is as follows:

comment on column TEST.T1.ID is '这是一个列注释';

Using the DM management tool, you can add comments together when creating a table, as shown in the following example:

By adding annotations, users can add information that needs to be noted for each object, and the modification of annotations is implemented in the same way as when adding annotations.

comment on table TEST.T1 is '这是又一个表注释';

By performing comment operations on the same table in the same way, the comment of the table can be updated. After the comment is updated, we can query it through the system table SYSTABLECOMMENTS.

select * from SYSTABLECOMMENTS where SCHNAME='TEST' and TVNAME='T1';

 From the query results, the table comment of the T1 table in TEST mode has been modified successfully. SYSTABLECOMMENTS contains comments for tables or views in each schema, SCHNAME is the schema name, TVNAME is the name of the table or view, and TABLE_TYPE specifies the object type as a table or view, and the COMMENT$ field is the specific content of the comment.

If the comment content to be updated contains a single quotation mark, you need to add a single quotation mark after the single quotation mark in the content. For example, modify the comment for the T1 table to read: This is a 'table' comment.

comment on table TEST.T1 is '这是一个''表''注释';

select * from SYSTABLECOMMENTS where SCHNAME='TEST' and TVNAME='T1';

For queries on field comments in tables or views, refer to the system table SYSCOLUMNCOMMENTS.

select * from SYSCOLUMNCOMMENTS;

Since there is only one column comment in the current database, it is the comment of the ID column in the T1 table in TEST mode in the result display. Compared with the result of the query table comment, the extra field COLNAME indicates the column name.

The previous update operation for comments actually overwrites the original comment information with the new comments created by the statement. And also in order to delete the comment on the column, you can assign the value of the COMMENT statement to '', so as to achieve the effect of deleting the original comment information. After the comment is removed in this way, the record of this column in the system table SYSCOLUMNCOMMENTS will not disappear. Examples are as follows:

comment on column TEST.T1.ID is '';

select * from SYSCOLUMNCOMMENTS where SCHNAME='TEST' and TVNAME='T1' and COLNAME='ID';

By querying the data dictionary ALL_COL_COMMENTS, you can also find the comment information about the columns in the object with access rights in the current user's environment.

select * from ALL_COL_COMMENTS where OWNER='TEST';

The query results will contain the comments of all the columns that can be accessed, including OWNER is the owner user, TABLE_NAME, SCHEMA_NAME, COLUMN_NAME correspond to the label, schema name and column name respectively, and the COMMENTS column is the comment information. Columns that have not been commented have a COMMENT value of "NULL". For table comments, the corresponding can also be found by querying ALL_COL_COMMENTS.

select * from ALL_COL_COMMENTS where OWNER='TEST';

For some specific scenarios where comments need to be rebuilt, the original comment content can be saved in the form of a statement through SQL statements so that it can be added in batches in the future. For the above-mentioned records containing single quotes in the comment content, the part of the single quotes needs to be taken into account when saving as a statement, so as to facilitate the execution of the statement during reconstruction.

select 'comment on table '||t.SCHNAME||'.'||t.TVNAME||' is '''||replace(t.COMMENT$,'''','''''')||''';' as COMMENTS_BAK from systablecomments t where t.TABLE_TYPE='TABLE' and t.SCHNAME not in ('SYS')

union

select 'comment on view '||v.SCHNAME||'.'||v.TVNAME||' is '''||replace(v.COMMENT$,'''','''''')||''';' from systablecomments v where v.TABLE_TYPE='VIEW' and v.SCHNAME not in ('SYS')

union

select 'comment on column '||c.SCHNAME||'.'||c.TVNAME||'.'||c.COLNAME||' is '''||replace(c.COMMENT$,'''','''''')||''';' from syscolumncomments c where c.SCHNAME not in ('SYS');

In this way, the comments that need to be saved can be saved as SQL statements, and can be directly executed when reconstruction is required.

The above is a little sharing about various aspects of annotations in DM. Flexible and sufficient use of annotations can better manage the database in the face of a large number of complex object structures.

Guess you like

Origin blog.csdn.net/qq_35273918/article/details/130409770