Oracle数据库21c 不可变表(immutable table)实验

Posted dingdingfish

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Oracle数据库21c 不可变表(immutable table)实验相关的知识,希望对你有一定的参考价值。

本实验参考Oracle 21c Learning Database New Features文档Practice: Immutable Tables

 CREATE IMMUTABLE TABLE imm_ledger
  (
    tran_id NUMBER,
    tran_source  VARCHAR2(50),
    tran_destination VARCHAR2(50),
    amount NUMBER
  )
  NO DROP UNTIL 20 DAYS IDLE
  NO DELETE UNTIL 30 DAYS AFTER INSERT;

SELECT row_retention "Row Retention Period", row_retention_locked "Row Retention Locked?", table_inactivity_retention "Table Retention Period"
 FROM user_immutable_tables 
 WHERE table_name LIKE 'IMM_LEDGER';

Row Retention Period Row Table Retention Period
-------------------- --- ----------------------
                  30 NO                      20

desc imm_ledger
 Name                                      Null?    Type
 ----------------------------------------- -------- ----------------------------
 TRAN_ID                                            NUMBER
 TRAN_SOURCE                                        VARCHAR2(50)
 TRAN_DESTINATION                                   VARCHAR2(50)
 AMOUNT                                             NUMBER

COL "Data Length" FORMAT 9999
COL "Column Name" FORMAT A24
COL "Data Type" FORMAT A28
SET LINESIZE 200
SELECT internal_column_id "Column ID", SUBSTR(column_name,1,30) "Column Name", SUBSTR(data_type,1,30) "Data Type",  data_length "Data Type Length"
FROM user_tab_cols
WHERE table_name LIKE 'IMM_LEDGER'
ORDER BY internal_column_id;

 Column ID Column Name              Data Type                    Data Type Length
---------- ------------------------ ---------------------------- ----------------
         1 TRAN_ID                  NUMBER                                     22
         2 TRAN_SOURCE              VARCHAR2                                   50
         3 TRAN_DESTINATION         VARCHAR2                                   50
         4 AMOUNT                   NUMBER                                     22
         5 ORABCTAB_INST_ID$        NUMBER                                     22
         6 ORABCTAB_CHAIN_ID$       NUMBER                                     22
         7 ORABCTAB_SEQ_NUM$        NUMBER                                     22
         8 ORABCTAB_CREATION_TIME$  TIMESTAMP(6) WITH TIME ZONE                13
         9 ORABCTAB_USER_NUMBER$    NUMBER                                     22
        10 ORABCTAB_HASH$           RAW                                      2000
        11 ORABCTAB_SIGNATURE$      RAW                                      2000
        12 ORABCTAB_SIGNATURE_ALG$  NUMBER                                     22
        13 ORABCTAB_SIGNATURE_CERT$ RAW                                        16
        14 ORABCTAB_SPARE$          RAW                                      2000

14 rows selected.

INSERT INTO imm_ledger VALUES(100,'John','Jane',540);
INSERT INTO imm_ledger VALUES(110,'Mary','Tom',658);
INSERT INTO imm_ledger VALUES(120,'Sheila','Paul',48);
commit;

COL From format a10
COL To format a10
COL "Creation Timestamp" FORMAT A40
SELECT ORABCTAB_CREATION_TIME$ "Creation Timestamp", tran_id "Transaction ID", SUBSTR(tran_source,1,15) "From" , SUBSTR(tran_destination,1,15) "To", amount
FROM imm_ledger;

Creation Timestamp                       Transaction ID From       To             AMOUNT
---------------------------------------- -------------- ---------- ---------- ----------
26-DEC-21 01.00.41.710856 PM +00:00                 100 John       Jane              540
26-DEC-21 01.00.41.713717 PM +00:00                 110 Mary       Tom               658
26-DEC-21 01.00.41.715375 PM +00:00                 120 Sheila     Paul               48

ALTER TABLE imm_ledger NO DROP UNTIL 30 DAYS IDLE;

ALTER TABLE imm_ledger NO DELETE UNTIL 18 DAYS AFTER INSERT;
ERROR at line 1:
ORA-05732: retention value cannot be lowered

ALTER TABLE imm_ledger NO DELETE UNTIL 45 DAYS AFTER INSERT;
Table altered.

-- 过了保留期后就可以删除所有行
SET SERVEROUTPUT ON;
DECLARE
   number_rows NUMBER;
BEGIN
   DBMS_IMMUTABLE_TABLE.DELETE_EXPIRED_ROWS('AUDITOR','IMM_LEDGER',NULL, NUMBER_ROWS);
   DBMS_OUTPUT.PUT_LINE('Number of rows deleted = '|| NUMBER_ROWS);
END;
/

-- 数据清空后才能删除
DROP TABLE imm_ledger;
DROP TABLE imm_ledger
           *
ERROR at line 1:
ORA-05723: drop blockchain or immutable table IMM_LEDGER not allowed

参考

以上是关于Oracle数据库21c 不可变表(immutable table)实验的主要内容,如果未能解决你的问题,请参考以下文章

Oracle Database 21c 分析函数增强

Oracle Database 21c 分析函数增强

[译] Oracle Database 21c 中的空间管理增强功能

[译] Oracle Database 21c 中的数据泵(expdpimpdp)增强功能

Vagrant 快速生成Oracle 21c 数据库实验环境

Oracle LiveLabs实验:Configure network environment for Oracle Database 21c