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