云数据库 GAUSSDB-账本数据库概述:操作步骤

时间:2025-06-03 09:25:16

操作步骤

  1. 创建防篡改模式。

    例如,创建防篡改模式ledgernsp。
    1
    gaussdb=# CREATE SCHEMA ledgernsp WITH BLOCKCHAIN;
    

    如果需要创建防篡改模式或更改普通模式为防篡改模式,则需设置enable_ledger参数为on。enable_ledger默认参数为off。

  2. 在防篡改模式下创建防篡改用户表。

    例如,创建防篡改用户表ledgernsp.usertable。

    gaussdb=# CREATE TABLE ledgernsp.usertable(id int, name text);

    查看防篡改用户表结构及其对应的用户历史表结构。

    gaussdb=# \d+ ledgernsp.usertable;
    gaussdb=# \d+ blockchain.ledgernsp_usertable_hist;

    执行结果如下:

    gaussdb=# \d+ ledgernsp.usertable;
                         Table "ledgernsp.usertable"
     Column     |  Type   | Modifiers | Storage  | Stats target | Description
    --------+---------+-----------+----------+--------------+-------------
     id         | integer |           | plain    |              |
     name       | text    |           | extended |              |
     hash_69dd43   | hash16  |           | plain    |              |
    Has OIDs: no
    Distribute By: HASH(id)
    Location Nodes: ALL DATANODES
    Options: orientation=row, compression=no
    History table name: ledgernsp_usertable_hist
    
    gaussdb=# \d+ blockchain.ledgernsp_usertable_hist;
                 Table "blockchain.ledgernsp_usertable_hist"
      Column  |  Type  | Modifiers | Storage | Stats target | Description
    ----------+--------+-----------+---------+--------------+-------------
     rec_num  | bigint |           | plain   |              |
     hash_ins | hash16 |           | plain   |              |
     hash_del | hash16 |           | plain   |              |
     pre_hash | hash32 |           | plain   |              |
    Indexes:
        "gs_hist_69dd43_index" PRIMARY KEY, btree (rec_num int4_ops) TABLESPACE pg_default
    Has OIDs: no
    Distribute By: HASH(rec_num)
    Location Nodes: ALL DATANODES
    Options: internal_mask=263
    1. 防篡改模式下仅行存表为防篡改表,临时表、外表、unlog表及非行存表均无防篡改属性。
    2. 防篡改表在创建时会自动增加一个用于校验的系统列,所以防篡改表单表最大列数为1599。

  3. 修改防篡改用户表数据。

    例如,对防篡改用户表执行INSERT、UPDATE、DELETE操作。

    gaussdb=# INSERT INTO ledgernsp.usertable VALUES(1, 'alex'), (2, 'bob'), (3, 'peter');
    INSERT 0 3
    gaussdb=# SELECT *, hash_69dd43 FROM ledgernsp.usertable ORDER BY id;
     id | name  |       hash_69dd43
    ----+-------+------------------
      1 | alex  | 1f2e543c580cb8c5
      2 | bob   | 8fcd74a8a6a4b484
      3 | peter | f51b4b1b12d0354b
    (3 rows)
    
    gaussdb=# UPDATE ledgernsp.usertable SET name = 'bob2' WHERE id = 2;
    UPDATE 1
    gaussdb=# SELECT *, hash_69dd43 FROM ledgernsp.usertable ORDER BY id;
     id | name  |       hash_69dd43
    ----+-------+------------------
      1 | alex  | 1f2e543c580cb8c5
      2 | bob2  | 437761affbb7c605
      3 | peter | f51b4b1b12d0354b
    (3 rows)
    
    gaussdb=# DELETE FROM ledgernsp.usertable WHERE id = 3;
    DELETE 1
    gaussdb=# SELECT *, hash_69dd43 FROM ledgernsp.usertable ORDER BY id;
     id | name |       hash_69dd43
    ----+------+------------------
      1 | alex | 1f2e543c580cb8c5
      2 | bob2 | 437761affbb7c605
    (2 rows)

  4. 删除表和模式。

    若要执行其他账本数据库章节的示例,请在执行完之后再执行当前步骤,否则请直接执行当前步骤。

    gaussdb=# DROP TABLE ledgernsp.usertable;
    DROP TABLE
    gaussdb=# DROP SCHEMA ledgernsp;
    DROP SCHEMA

support.huaweicloud.com/fg-gaussdb-dist-v8/gaussdb-18-0021.html
提示

您即将访问非华为云网站,请注意账号财产安全