系统周期时态表和FOREIGN KEY ...删除级联

时间:2019-03-10 20:31:33

标签: ibm-midrange db2-400

当我尝试向system-period temporal table添加FOREIGN KEY ... ON DELETE CASCADE约束时遇到错误-在我的示例中STATE表下方。

我可以让应用程序层进行清理,但是我更希望让数据库来处理自己的内部事务。

父表:

0001.00 -- *************************************************************************************************
0002.00 -- Create the table.                                                                                
0003.00 -- *************************************************************************************************
0004.00 CREATE OR REPLACE TABLE country (                                                                   
0005.00   iso2   CHAR(2) NOT NULL,                                                                          
0006.00   iso3   CHAR(3) NOT NULL,                                                                          
0007.00   isonum CHAR(3) NOT NULL,                                                                          
0008.00   name   VARCHAR(45) NOT NULL,                                                                      
0009.00   sys_start                                                                                         
0010.00     TIMESTAMP(12) NOT NULL GENERATED ALWAYS AS ROW BEGIN IMPLICITLY HIDDEN,                         
0011.00   sys_end                                                                                           
0012.00     TIMESTAMP(12) NOT NULL GENERATED ALWAYS AS ROW END IMPLICITLY HIDDEN,                           
0013.00   sys_ts                                                                                            
0014.00     TIMESTAMP(12) NOT NULL GENERATED ALWAYS AS TRANSACTION START ID                                 
0015.00     IMPLICITLY HIDDEN,                                                                              
0016.00   PERIOD SYSTEM_TIME(sys_start, sys_end),                                                           
0017.00   PRIMARY KEY(iso2)                                                                                 
0018.00 ) RCDFMT rcountry;                                                                                  
0019.00                                                                                                     
0020.00 COMMIT;                                                                                             
0021.00                                                                                                     
0022.00 -- *************************************************************************************************
0023.00 -- Create a history table to track all changes.                                                     
0024.00 -- *************************************************************************************************
0025.00 CREATE OR REPLACE TABLE countryh LIKE country;                                                      
0026.00                                                                                                     
0027.00 COMMIT;                                                                                             
0028.00                                                                                                     
0029.00 -- *************************************************************************************************
0030.00 -- Associate our history table with the main table and tell the system to add a row to the history  
0031.00 -- table when a row is deleted so we'll have a record of the deletion.                              
0032.00 -- *************************************************************************************************
0033.00 ALTER TABLE country ADD VERSIONING USE HISTORY TABLE countryh                                       
0034.00   ON DELETE ADD EXTRA ROW;                                                                          
0035.00                                                                                                     
0036.00 COMMIT;                                                                                             

子表:

0001.00 -- *************************************************************************************************
0002.00 -- Create the table.                                                                                
0003.00 -- ****************************************************************************|********************
0004.00 CREATE OR REPLACE TABLE state (                                                                     
0005.00   id BIGINT GENERATED ALWAYS AS IDENTITY (START WITH 1 CYCLE),                                      
0006.00   cntry_id CHAR(2) NOT NULL,                                                                        
0007.00   code     VARCHAR(3) NOT NULL,                                                                     
0008.00   name     VARCHAR(35) NOT NULL,                                                                    
0009.00   sys_start                                                                                         
0010.00     TIMESTAMP(12) NOT NULL GENERATED ALWAYS AS ROW BEGIN IMPLICITLY HIDDEN,                         
0011.00   sys_end                                                                                           
0012.00     TIMESTAMP(12) NOT NULL GENERATED ALWAYS AS ROW END IMPLICITLY HIDDEN,                           
0013.00   sys_ts                                                                                            
0014.00     TIMESTAMP(12) NOT NULL GENERATED ALWAYS AS TRANSACTION START ID                                 
0015.00     IMPLICITLY HIDDEN,                                                                              
0016.00   PERIOD SYSTEM_TIME(sys_start, sys_end),                                                           
0017.00   PRIMARY KEY(id),                                                                                  
0018.00   UNIQUE (cntry_id, code)                                                                           
0019.00 ) RCDFMT rstate;                                                                                    
0020.00                                                                                                     
0021.00 COMMIT;                                                                                             
0022.00                                                                                                     
0023.00 -- *************************************************************************************************
0024.00 -- Create a history table to track all changes.                                                     
0025.00 -- *************************************************************************************************
0026.00 CREATE OR REPLACE TABLE stateh LIKE state;                                                          
0027.00                                                                                                     
0028.00 COMMIT;                                                                                             
0029.00                                                                                                     
0030.00 -- *************************************************************************************************
0031.00 -- Associate our history table with the main table and tell the system to add a row to the history  
0032.00 -- table when a row is deleted so we'll have a record of the deletion.                              
0033.00 -- *************************************************************************************************
0034.00 ALTER TABLE state ADD VERSIONING USE HISTORY TABLE stateh                                           
0035.00   ON DELETE ADD EXTRA ROW;                                                                          
0036.00                                                                                                     
0037.00 COMMIT;                                                                                             
0038.00                                                                                                     
0039.00 ALTER TABLE state ADD FOREIGN KEY(cntry_id) REFERENCES country(iso2)                                
0040.00   ON DELETE CASCADE;
0041.00                     
0042.00 COMMIT;             

我的编译列表中显示的error是:

MSG ID  SEV  RECORD  TEXT
SQ20525  30      39  Position 1 Operation on table STATE in HILLB1 not
                     allowed.                                         

我的工作日志中显示的错误是:

Constraint is not valid.

当我执行F1提示以获取更多信息时,我得到:

Message . . . . :   Constraint is not valid.                                 
Cause . . . . . :   Constraint *N cannot be added for file STATE in library  
  HILLB1 for TYPE value *N. For a referential constraint (TYPE *REFCST), the 
  parent file *N in library *N has a delete rule of *N and update rule of *N.
  The constraint was not added because of errors. The reason code is 6. The  
  reason codes and their meanings are as follows:                            
    06 - The file is a system-period temporal table and the referential    
  constraint cannot use the delete rule of *CASCADE, *SETDFT, and *SETNULL.

很明显Db2 for i不喜欢我要尝试做的事情。

我还尝试将FOREIGN KEY定义放入CREATE OR REPLACE TABLE语句中,并得到相同的错误(尽管在第34行)。

当父表删除行时,真的没有办法使从属表(即系统周期的时态表)删除相关行吗?

0 个答案:

没有答案