Oracle zipper table

Table of contents

-- Prepare a zipper list

 -- 2. Synchronize all data to TEST_TARGET in the zipper table

 --3. The data in the source table has changed 

--4. Synchronize the newly added and modified data to the zipper table -- the process of opening the chain -- determine the data of the source table and the target table, and insert different data

--5. Modify the failure time and status in the zipper table (change the original open chain time to the current time) -- closed chain


-- Zipper table 
-- a table that reflects historical changes, maintains the historical status of data, and the latest status of data

--Tables involved in zipper table
1. Source table (table in business database)
2. Zipper table

-- It is often used in data warehouses to maintain relationships between fact tables and dimension tables

--Implementation process of zipper table.
1. Prepare a source table and a zipper table
2. Synchronize all data to the zipper table
3. Data in the source table changes
4. Synchronize new and modified data to the zipper table
5 . Modify the expiration time and status of the zipper list.

-- Prepare a zipper list
CREATE TABLE TEST_SOURCE  -- 源表
(
S_ID NUMBER,
S_NAME VARCHAR2(10),
S_SAL NUMBER,
CREATE_AT DATE,
UPDATE_AT DATE
);
CREATE TABLE TEST_TARGET --目标表
(
S_ID NUMBER,
S_NAME VARCHAR2(10),
S_SAL NUMBER,
CREATE_AT DATE,
UPDATE_AT DATE,
START_AT DATE,  -- to_date(to_char(sysdate-1,'yyyymmdd'),'yyyymmdd')
END_AT DATE,    -- to_date('9999-12-31','yyyy-mm-dd')
T_ACTIVE VARCHAR2(10) -- '有效'
);
INSERT INTO TEST_SOURCE VALUES(1,'黄征',6000,to_date(to_char(sysdate-1,'yyyymmdd'),'yyyymmdd'),to_date(to_char(sysdate-1,'yyyymmdd'),'yyyymmdd'));
INSERT INTO TEST_SOURCE VALUES(2,'徐峥',7000,to_date(to_char(sysdate-1,'yyyymmdd'),'yyyymmdd'),to_date(to_char(sysdate-1,'yyyymmdd'),'yyyymmdd'));

select * from test_source; 

 -- 2. Synchronize all data to TEST_TARGET in the zipper table
 INSERT INTO TEST_TARGET
 SELECT 
 S.S_ID,
 S.S_NAME,
 S.S_SAL,
 S.CREATE_AT,
 S.UPDATE_AT,
 TO_DATE(TO_CHAR(SYSDATE-1,'yyyymmdd'),'yyyymmdd'),
 TO_DATE('9999-12-31','yyyy-mm-dd'),
 '有效'
 FROM TEST_SOURCE S 

 SELECT * FROM TEST_TARGET; 

 --3. The data in the source table has changed 
 INSERT INTO TEST_SOURCE VALUES (3,'黄海波',8000,to_date(to_char(sysdate,'yyyymmdd'),'yyyymmdd'),to_date(to_char(sysdate,'yyyymmdd'),'yyyymmdd'));
 
 UPDATE TEST_SOURCE S SET S.S_SAL=S.S_SAL+900, S.UPDATE_AT=TO_DATE(TO_CHAR(SYSDATE,'YYYYMMDD'),'YYYYMMDD') WHERE S.S_ID=1;
--4. Synchronize the newly added and modified data to the zipper table -- the process of opening the chain
 -- determine the data of the source table and the target table, and insert different data
 INSERT INTO TEST_TARGET 
 SELECT 
 S.S_ID,
 S.S_NAME,
 S.S_SAL,
 S.CREATE_AT,
 S.UPDATE_AT,
 TO_DATE(TO_CHAR(SYSDATE-1,'yyyymmdd'),'yyyymmdd'),
 TO_DATE('9999-12-31','yyyy-mm-dd'),
 '有效'
 FROM TEST_SOURCE S
 WHERE NOT EXISTS(
 SELECT 1 
 FROM TEST_TARGET G
WHERE S.S_ID=G.S_ID
 AND G.S_NAME=S.S_NAME 
 AND G.S_SAL=S.S_SAL
 AND G.CREATE_AT =S.CREATE_AT
 AND G.UPDATE_AT=S.UPDATE_AT
 AND G.END_AT=TO_DATE('9999-12-31','yyyy-mm-dd')
 );

 SELECT * FROM TEST_TARGET;

--5. Modify the failure time and status in the zipper table (change the original open chain time to the current time) -- closed chain
UPDATE TEST_TARGET T SET T.END_AT=TO_DATE(TO_CHAR (SYSDATE,'YYYYMMDD'),'YYYYMMDD'),
 T.T_ACTIVE='失效'
 WHERE EXISTS
 (
 select 1
  from TEST_SOURCE s
 WHERE t.s_id=s.s_id
 AND (T.S_NAME <> S.S_NAME OR T.S_SAL<> S.S_SAL OR T.UPDATE_AT <> S.UPDATE_AT)
 );

 SELECT * FROM TEST_TARGET;

Guess you like

Origin blog.csdn.net/weixin_57024726/article/details/133069689