DB2的系统视图SYSCAT.TABLES详解

可以看到其实SYSCAT模式下的所有表都是视图,包括SYSCAT.TABLES
[db2inst1@t3-dtpoc-dtpoc-web04 ~]$ db2 list tables for schema syscat |grep -i tables
EVENTTABLES                     SYSCAT          V     2023-08-25-13.53.47.441014
TABLES                          SYSCAT          V     2023-08-25-13.53.48.039210
TABLESPACES                     SYSCAT          V     2023-08-25-13.53.48.052082
而SYSIBM模式下的即有表也有视图:
[db2inst1@t3-dtpoc-dtpoc-web04 ~]$ db2 list tables for schema sysibm |grep -i tables
SQLTABLES                       SYSIBM          V     2023-08-25-13.53.51.285112
SYSEVENTTABLES                  SYSIBM          T     2023-08-25-13.53.38.603634
SYSTABLES                       SYSIBM          T     2023-08-25-13.53.38.603634
SYSTABLESPACES                  SYSIBM          T     2023-08-25-13.53.38.603634
TABLES                          SYSIBM          V     2023-08-25-13.53.47.027554
TABLES_S                        SYSIBM          V     2023-08-25-13.53.47.033440


我们查看一下SYSCAT.TABLES视图的定义,发现其实是从sysibm.systables查询的数据
db2 "select VIEWSCHEMA,VIEWNAME,TEXT from  syscat.views where VIEWSCHEMA='SYSCAT' and VIEWNAME='TABLES'"                                                                                               
create or replace view syscat.tables (tabschema, tabname, owner, ownertype, type, status, ....from sysibm.systables t


那我们是不是可以直接从sysibm.systables查询数据呢?能不能增删改呢?
通过测试可以看到可以查但是不能增删改

[db2inst1@t3-dtpoc-dtpoc-web04 ~]$ db2 list tables for all |grep -i course
COURSE                          DB2INST1        T     2023-08-11-09.45.42.899107
[db2inst1@t3-dtpoc-dtpoc-web04 ~]$ db2 "select NAME from sysibm.systables" |grep -i course
COURSE                                                                                                                          
[db2inst1@t3-dtpoc-dtpoc-web04 ~]$ db2 "update sysibm.systables set NAME='COURSE1' where NAME='COURSE' " 
DB21034E  The command was processed as an SQL statement because it was not a 
valid Command Line Processor command.  During SQL processing it returned:
SQL0151N  The column "NAME" cannot be updated.  SQLSTATE=42808
[db2inst1@t3-dtpoc-dtpoc-web04 ~]$ db2 "delete from sysibm.systables where NAME='COURSE' "
DB21034E  The command was processed as an SQL statement because it was not a 
valid Command Line Processor command.  During SQL processing it returned:
SQL0607N  "DELETE" is not defined for system objects.  SQLSTATE=42832
[db2inst1@t3-dtpoc-dtpoc-web04 ~]$ db2 "? sql0151N"


SQL0151N  The column "" cannot be updated.

Explanation: 

The specified column cannot be updated because one of the following was
attempted.

*  The object table is a view, and the specified column is derived from
   a scalar function, expression, keyword, constant, or column of a view
   where that column cannot be updated.

*  The specified column is a non-updateable column of a system catalog,
   or a column explicitly marked as READ ONLY.
   
 当然视图也不能增删改:  
[db2inst1@t3-dtpoc-dtpoc-web04 ~]$  db2 "delete from SYSCAT.TABLES  where TABNAME='COURSE' "       
DB21034E  The command was processed as an SQL statement because it was not a 
valid Command Line Processor command.  During SQL processing it returned:
SQL0607N  "DELETE" is not defined for system objects.  SQLSTATE=42832
 

你可能感兴趣的:(服务器,运维)