简体   繁体   English

MySQL 8:通过 DDL 创建 Collections

[英]MySQL 8: Create Collections via DDL

I'd like to be able to create MySQL Document Store Collections via simple SQL DDL statements rather than using the X-Protocol clients.我希望能够通过简单的 SQL DDL 语句而不是使用 X 协议客户端来创建 MySQL 文档存储 Collections。

Is there any way to do so?有什么办法吗?

Edit: I'll try and clarify the question.编辑:我会尝试澄清这个问题。

Collections are tables using JSON datatypes and functions. Collections 是使用 JSON 数据类型和函数的表。 That much is clear.这很清楚。 I would like know how I can create a Collection without using the X-Protocol calls and make sure that the aforementioned collection is picked up as an actual Collection.我想知道如何在不使用 X-Protocol 调用的情况下创建一个集合,并确保将上述集合作为一个实际的集合。

Judging from MySQL workbench, collection tables have a _id blob PK with an expression, a doc JSON column and a few other elements I do not recall at the moment (might be indexes, etc).从 MySQL 工作台来看,收集表有一个带有表达式的 _id blob PK、一个文档 JSON 列和一些我目前不记得的其他元素(可能是索引等)。

I have no means to tell via the Workbench whatever additional schema/metadata information is required for a table to be considered a Document Store Collection, or if the mere presence of an _id and doc columns are enough.我无法通过 Workbench 告诉我们需要什么额外的模式/元数据信息才能将表视为文档存储集合,或者仅存在 _id 和 doc 列是否足够。

I hope this clears things up.我希望这能解决问题。

All "x-api" instructions are directly mapped to sql syntax.所有“x-api”指令都直接映射到 sql 语法。 When you eg run db.createCollection('my_collection') , MySQL will literally just execute例如,当您运行db.createCollection('my_collection')时, MySQL 只会执行

CREATE TABLE `my_collection` (
  `doc` json DEFAULT NULL,
  `_id` varbinary(32) GENERATED ALWAYS AS
     (json_unquote(json_extract(`doc`,_utf8mb4'$._id'))) STORED NOT NULL,
  `_json_schema` json GENERATED ALWAYS AS (_utf8mb4'{"type":"object"}') VIRTUAL,
  PRIMARY KEY (`_id`),
  CONSTRAINT `$val_strict` CHECK (json_schema_valid(`_json_schema`,`doc`))
      NOT ENFORCED
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci

You can run the corresponding sql statements yourself if you follow that format.如果您遵循该格式,您可以自己运行相应的 sql 语句。 The doc and _id (with their type and the given expression) are required, the _json_schema is optional, the check too (and only added since MySQL 8.0.17). doc_id (及其类型和给定表达式)是必需的, _json_schema是可选的,检查也是(并且仅在 MySQL 8.0.17 之后添加)。 Since MySQL 8, no additional columns are allowed, except generated columns that use JSON_EXTRACT on doc and which are supposed to be used in an index, see below (although they don't actually have to be used in an index).从 MySQL 8 开始,不允许使用其他列,除了生成的列在doc上使用JSON_EXTRACT并且应该在索引中使用,请参见下文(尽管它们实际上不必在索引中使用)。

Any table that looks like that - doc and _id with their correct type/expression and no other columns except an optional _json_schema and generated JSON_EXTRACT(doc, -columns - will be found with getCollections() .任何看起来像这样的表 - doc_id具有正确的类型/表达式,除了可选的_json_schema和生成的JSON_EXTRACT(doc, -columns - 之外没有其他列 - 将使用getCollections()找到。

To add an index, the corresponding syntax for要添加索引,对应的语法为

my_collection.createIndex("age", {fields: [{field: "$.age", type: "int"}]})

would be将会

ALTER TABLE `test`.`my_collection` ADD COLUMN `$ix_i_somename` int
      GENERATED ALWAYS AS (JSON_EXTRACT(doc, '$.age')) VIRTUAL, 
   ADD INDEX `age` (`$ix_i_somename`)

Obviously,明显地,

db.dropCollection('my_collection')

simply translates to简单地转化为

DROP TABLE `my_collection`

Similarly, all CRUD operations on documents have a corresponding sql DML syntax (that will actually be executed when you use them via x-api).类似地,文档上的所有 CRUD 操作都有相应的 sql DML 语法(当您通过 x-api 使用它们时将实际执行)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM