CREATE CLUSTER TABLE

语法说明

集群表是指在系统租户下的系统库 mo_catalog 创建的表,该表会在其它租户下同时生效。系统租户下可对该表进行 DDL 与 DML 操作,其它租户只能进行查询或基于该表创建视图。

本篇文档将讲述如何在 MatrixOne 数据库建立集群表。

语法结构

  1. > CREATE CLUSTER TABLE [IF NOT EXISTS] tbl_name
  2. (create_definition,...)
  3. [table_options]
  4. [partition_options]

使用说明

  • 创建集群表仅限于 sys 租户管理员角色才能创建。

  • sys 租户的集群表包含所有数据,其它租户下可能只会看到部分数据。

  • 在集群表中,account_id 字段是自动生成的,表示在插入或 LOAD DATA 时指定数据的可见租户的 id,每条数据只能选择一个可见租户,若您想多个租户都能查看该数据,需要多次插入指定不同租户 id,该字段数据在其它租户中查询不会返回。

  • 集群表不能为外表或临时表,在所有租户下的表结构完全相同。

示例

  1. --创建两个租户 test1 test2
  2. mysql> create account test1 admin_name = 'root' identified by '111' open comment 'tenant_test';
  3. Query OK, 0 rows affected (0.44 sec)
  4. mysql> create account test2 admin_name = 'root' identified by '111' open comment 'tenant_test';
  5. Query OK, 0 rows affected (0.51 sec)
  6. --在 sys 租户下创建集群表
  7. mysql> use mo_catalog;
  8. Database changed
  9. mysql> drop table if exists t1;
  10. Query OK, 0 rows affected (0.00 sec)
  11. mysql> create cluster table t1(a int);
  12. Query OK, 0 rows affected (0.01 sec)
  13. --查看租户 id
  14. mysql> select * from mo_account;
  15. +------------+--------------+--------+---------------------+----------------+---------+----------------+
  16. | account_id | account_name | status | created_time | comments | version | suspended_time |
  17. +------------+--------------+--------+---------------------+----------------+---------+----------------+
  18. | 0 | sys | open | 2024-01-11 08:56:57 | system account | 1 | NULL |
  19. | 6 | test1 | open | 2024-01-15 03:15:40 | tenant_test | 7 | NULL |
  20. | 7 | test2 | open | 2024-01-15 03:15:48 | tenant_test | 8 | NULL |
  21. +------------+--------------+--------+---------------------+----------------+---------+----------------+
  22. 3 rows in set (0.01 sec)
  23. --在集群表 t1 中插入数据只对 test1 租户可见
  24. mysql> insert into t1 values(1,6),(2,6),(3,6);
  25. Query OK, 3 rows affected (0.01 sec)
  26. --在 sys 租户中查看 t1 的数据,可看到包含`account_id`字段在内的所有数据
  27. mysql> select * from t1;
  28. +------+------------+
  29. | a | account_id |
  30. +------+------------+
  31. | 1 | 6 |
  32. | 2 | 6 |
  33. | 3 | 6 |
  34. +------+------------+
  35. 3 rows in set (0.00 sec)
  36. --在 test1 租户中查看 t1 的数据,可以看到非`account_id`字段的数据
  37. mysql> select * from t1;
  38. +------+
  39. | a |
  40. +------+
  41. | 1 |
  42. | 2 |
  43. | 3 |
  44. +------+
  45. 3 rows in set (0.01 sec)
  46. --在 test2 租户中查看 t1 的数据,不会看到有数据
  47. mysql> select * from t1;
  48. Empty set (0.01 sec)
  49. --在 test1 租户中创建基于 t1 的视图
  50. mysql> create view t1_view as select * from mo_catalog.t1;
  51. Query OK, 0 rows affected (0.01 sec)
  52. mysql> select * from t1_view;
  53. +------+
  54. | a |
  55. +------+
  56. | 1 |
  57. | 2 |
  58. | 3 |
  59. +------+
  60. 3 rows in set (0.00 sec)
  61. --在 test2 租户中创建基于 t1 的视图
  62. mysql> create view t1_view as select * from mo_catalog.t1;
  63. Query OK, 0 rows affected (0.01 sec)
  64. mysql> select * from t1_view;
  65. Empty set (0.01 sec)