DB2从9.5以后引入了隐藏列,就是你用select * from table的方式查询不到这个列,但是可以显式的指定列名来看该列的值。
CREATE TABLE CUSTOMER
(
CUSTNO INTEGER NOT NULL,
CUST_NAME VARCHAR(50),
CUST_INFOCHANGE TIMESTAMP NOT NULL IMPLICITLY HIDDEN GENERATED BY DEFAULT FOR EACH ROW ON UPDATE AS ROW CHANGE TIMESTAMP
)
插入数据的时候,不用指定隐藏列
INSERT INTO CUSTOMER VALUES(1,'111')
INSERT INTO CUSTOMER VALUES(2,'222')
INSERT INTO CUSTOMER VALUES(3,'333')
INSERT INTO CUSTOMER VALUES(4,'444')
隐藏列的值在这里是插入的时间戳,如果你更新一行,那么这个时间戳也是相应改变的。
describe 表是可以看到这个隐藏列的
db2 => describe table customer
Data type Column
Column name schema Data type name Length Scale Nulls
------------------------------- --------- ------------------- ---------- ----- ------
CUSTNO SYSIBM INTEGER 4 0 No
CUST_NAME SYSIBM VARCHAR 50 0 Yes
CUST_INFOCHANGE SYSIBM TIMESTAMP 10 6 No
3 record(s) selected.
可以显式的查看隐藏列
db2 => select CUSTNO,CUST_NAME,CUST_INFOCHANGE from customer
CUSTNO CUST_NAME CUST_INFOCHANGE
----------- -------------------------------------------------- --------------------------
1 111 2015-11-26-07.41.52.610475
2 222 2015-11-26-07.41.52.623278
3 333 2015-11-26-07.41.52.898431
4 444 2015-11-26-07.41.53.674208
4 record(s) selected.
但是使用select *就看不到这个列,这就是为什么叫隐藏列的原因吧,因为这种情况下看不到
db2 => select * from customer
CUSTNO CUST_NAME
----------- --------------------------------------------------
1 111
2 222
3 333
4 444
4 record(s) selected.
导出导入数据实验
export to customer.del of del modified by implicitlyhiddeninclude select * from customer
load from customer.del of del modified by implicitlyhiddeninclude identityoverride replace into customer nonrecoverable
但是这里有个问题,还是需要继续查一下,如果在定义此列的时候使用的是ALWAYS的方式
CUST_INFOCHANGE TIMESTAMP NOT NULL IMPLICITLY HIDDEN GENERATED ALWAYS FOR EACH ROW ON UPDATE AS ROW CHANGE TIMESTAMP
在执行load命令的时候会出现问题。
db2 => load from customer.del of del modified by implicitlyhiddeninclude identityoverride replace into customer nonrecoverable
SQL3551W The table contains at least one GENERATED ALWAYS column that the
utility will override.
SQL3109N The utility is beginning to load data from file
"/home/db2inst1/customer.del".
SQL3500W The utility is beginning the "LOAD" phase at time "11/26/2015
07:39:05.270055".
SQL3519W Begin Load Consistency Point. Input record count = "0".
SQL3520W Load Consistency Point was successful.
SQL3550W The field value in row "F0-1" and column "3" is not NULL, but the
target column has been defined as GENERATED ALWAYS.
SQL3185W The previous error occurred while processing data from row "F0-1" of
the input file.
SQL3550W The field value in row "F0-2" and column "3" is not NULL, but the
target column has been defined as GENERATED ALWAYS.
SQL3185W The previous error occurred while processing data from row "F0-2" of
the input file.
SQL3550W The field value in row "F0-3" and column "3" is not NULL, but the
target column has been defined as GENERATED ALWAYS.
SQL3185W The previous error occurred while processing data from row "F0-3" of
the input file.
SQL3550W The field value in row "F0-4" and column "3" is not NULL, but the
target column has been defined as GENERATED ALWAYS.
SQL3185W The previous error occurred while processing data from row "F0-4" of
the input file.
SQL3227W Record token "F0-1" refers to user record number "1".
SQL3227W Record token "F0-2" refers to user record number "2".
SQL3227W Record token "F0-3" refers to user record number "3".
SQL3227W Record token "F0-4" refers to user record number "4".
SQL3110N The utility has completed processing. "4" rows were read from the
input file.
SQL3519W Begin Load Consistency Point. Input record count = "4".
SQL3520W Load Consistency Point was successful.
SQL3515W The utility has finished the "LOAD" phase at time "11/26/2015
07:39:05.317399".
SQL3107W At least one warning message was encountered during LOAD processing.
Number of rows read = 4
Number of rows skipped = 0
Number of rows loaded = 0
Number of rows rejected = 4
Number of rows deleted = 0
Number of rows committed = 4