简体   繁体   English

如何将JSON数据插入父子表

[英]How do I insert JSON data into parent child table

I have a MySQL table with the structure below 我有一个具有以下结构的MySQL表

+------------------+------------------+----------------+
|      comp_id     |       name       |     parent     |
|------------------|------------------|----------------+
|         1        |       comp1      |      NULL      |
+------------------+------------------+----------------+
|         2        |       comp2      |       1        |
+------------------+------------------+----------------+
|         3        |       comp3      |       2        |
+------------------+------------------+----------------+
|         4        |       comp4      |       2        |
+------------------+------------------+----------------+

Assuming that no data has been inserted into the table. 假设没有数据插入表中。 In other words, assuming that the table is empty how should i go about the following: 换句话说,假设表为空,我应该如何处理以下内容:

  1. traverse the JSON data below for entry into the table: 遍历下面的JSON数据以输入到表中:
{
  "org_name":"paradise island",
  "daughters" : [
    {
      "org_name": "banana tree",
      "daughters": [
        {"org_name":"Yellow Banana"},
        {"org_name":"Brown Banana"}
      ]
    },
    {
      "org_name": "big banana tree",
      "daughters": [
        {"org_name":"green banana"},
        {"org_name":"yellow banana"},
        {
          "org_name": "Black banana",
          "daughters": [
            {"org_name": "red spider"}
          ]
        }
      ]
    }
  ]
}
  1. what effective SQL query can I write to insert the JSON above into MYSQL database at once. 我可以编写哪种有效的SQL查询将上面的JSON立即插入MYSQL数据库。

I've researched a host of resources on adjacency list model and nested models but none has been exhaustive on how inserts should be done via JSON input 我已经研究了有关邻接表模型和嵌套模型的大量资源,但是没有一个关于如何通过JSON输入完成插入的穷举。

If using uuidv4's as id's would be ok for you, you could just normalize your object with a recursive function, add uuid's as id's and construct the parental relationships. 如果您可以使用uuidv4作为id,则可以使用递归函数将您的对象标准化,添加uuid作为id并构造父母关系。 After That you just bulk insert your data with whatever database client you are using. 之后,您只需使用正在使用的任何数据库客户端批量插入数据即可。

This is an ideal use case for UUIDV4's. 这是UUIDV4的理想用例。 The chance of a collision is very unlikely, so you can consider it production safe. 碰撞的可能性很小,因此您可以认为它生产安全。

You just have to make sure, that your uuid generator is random enough. 您只需要确保uuid生成器足够随机即可。 Bad implementations could lead to much higher probabillities of collisions. 错误的实现可能导致更高的碰撞概率。

I suggest using the uuid package. 我建议使用uuid包。 You can look up it's sources on github. 您可以在github上查找其来源。 It uses nodes crypto library as random data generator which is cryptographically strong according to the documentation 它使用节点加密库作为随机数据生成器,根据文档所述,加密算法在密码学上很强大

Nice to know: MongoDB's ObjectId's are created in a similar way . 很高兴知道:MongoDB的ObjectId是以类似的方式创建的。 They also don't provide 100 percent security against collision. 他们也不提供100%的防撞安全性。 But they are so unlikely that they consider this chance irrelevant. 但是他们是如此不可能,以至于他们认为这个机会是无关紧要的。

EDIT: I assumed your code runs server side in node.js. 编辑:我假设您的代码在node.js中运行服务器端。 Generating uuid's in the browser is not safe, because the crypto api is still experimental and users might try to intentionally cause collisions. 在浏览器中生成uuid并不安全,因为crypto API仍处于试验阶段,用户可能会试图故意造成冲突。

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

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