mysql - 从 JSON 多维数组到 SQL

标签 mysql sql json

我有这个 JSON 结构,需要将其传递到数据库。但我不知道我所做的结构是否是最好的,代码有一些重复,我认为这是不正确的,我认为这是没有必要的。

JSON:

{
    "info": [
        {
            "atriz": "Sandra Bullock",
            "atriz-id": "162",
            "atriz-slug": "sandra-bullock",
            "carreira": {
                "id": "264",
                "inicio": "08/10/91",
                "final": "16/08/18",
                "videos": [
                    {
                        "id": "2930500",
                        "titulo": "Lorem ipsum habitant commodo cubilia eget blandit",
                        "desc": "Suscipit augue dictum ultrices ultricies aliquam mattis nostra taciti sagittis",
                        "code": "Sb6y1oTwd",
                        "source": "http://URL",
                        "download": true,
                        "link": "http://URL",
                        "duration": null,
                        "post_status": "pending",
                        "category": "drama",
                        "upload": {
                            "send": true,
                            "id": "118840448",
                            "url": "http://URL",
                            "status": "finished"
                        }
                    },
                    {
                        "id": "2930499",
                        "titulo": "Lorem ipsum habitant commodo cubilia eget blandit",
                        "desc": "Suscipit augue dictum ultrices ultricies aliquam mattis nostra taciti sagittis",
                        "code": "R2G0GhTwF",
                        "source": "http://URL",
                        "download": true,
                        "link": "http://URL",
                        "duration": null,
                        "post_status": "pending",
                        "category": "acao",
                        "upload": {
                            "send": true,
                            "id": "118840554",
                            "url": "http://URL",
                            "status": "finished"
                        }
                    }
                ]
            }
        },
        {
            "atriz": "Jennifer Lawrence",
            "atriz-id": "207",
            "atriz-slug": "jennifer-lawrence",
            "carreira": {
                "id": "263",
                "inicio": "02/01/88",
                "final": "09/08/18",
                "videos": [
                    {
                        "id": "2930443",
                        "titulo": "Lorem ipsum habitant commodo cubilia eget blandit",
                        "desc": "Suscipit augue dictum ultrices ultricies aliquam mattis nostra taciti sagittis",
                        "code": "DNJNYHWh",
                        "source": "http://URL",
                        "download": true,
                        "link": "http://URL",
                        "duration": null,
                        "post_status": "pending",
                        "category": "drama",
                        "upload": {
                            "send": true,
                            "id": "118844113",
                            "url": "http://URL",
                            "status": "finished"
                        }
                    },
                    {
                        "id": "2930442",
                        "titulo": "Lorem ipsum habitant commodo cubilia eget blandit",
                        "desc": "Suscipit augue dictum ultrices ultricies aliquam mattis nostra taciti sagittis",
                        "code": "OqieXJHwh",
                        "source": "http://URL",
                        "download": true,
                        "link": "http://URL",
                        "duration": null,
                        "post_status": "pending",
                        "category": "comedia",
                        "upload": {
                            "send": true,
                            "id": "118844112",
                            "url": "http://URL",
                            "status": "finished"
                        }
                    }
                ]
            }
        }
    ]
}

我对 SQL 不太了解,所以我使用了在线转换,它返回了我这个:

SQL:

CREATE TABLE IF NOT EXISTS infos (
    `info_atriz` VARCHAR(17) CHARACTER SET utf8,
    `info_atriz_id` INT,
    `info_atriz_slug` VARCHAR(17) CHARACTER SET utf8,
    `info_carreira_id` INT,
    `info_carreira_inicio` DATETIME,
    `info_carreira_final` DATETIME,
    `info_carreira_videos_id` INT,
    `info_carreira_videos_titulo` VARCHAR(49) CHARACTER SET utf8,
    `info_carreira_videos_desc` VARCHAR(78) CHARACTER SET utf8,
    `info_carreira_videos_code` VARCHAR(9) CHARACTER SET utf8,
    `info_carreira_videos_source` VARCHAR(10) CHARACTER SET utf8,
    `info_carreira_videos_download` VARCHAR(4) CHARACTER SET utf8,
    `info_carreira_videos_link` VARCHAR(10) CHARACTER SET utf8,
    `info_carreira_videos_duration` INT,
    `info_carreira_videos_post_status` VARCHAR(7) CHARACTER SET utf8,
    `info_carreira_videos_category` VARCHAR(7) CHARACTER SET utf8,
    `info_carreira_videos_upload_send` VARCHAR(4) CHARACTER SET utf8,
    `info_carreira_videos_upload_id` INT,
    `info_carreira_videos_upload_url` VARCHAR(10) CHARACTER SET utf8,
    `info_carreira_videos_upload_status` VARCHAR(8) CHARACTER SET utf8
);
INSERT INTO infos VALUES
    ('Sandra Bullock',162,'sandra-bullock',264,'1991-10-08 00:00:00','2018-08-16 00:00:00',2930500,'Lorem ipsum habitant commodo cubilia eget blandit','Suscipit augue dictum ultrices ultricies aliquam mattis nostra taciti sagittis','Sb6y1oTwd','http://URL','True','http://URL',NULL,'pending','drama','True',118840448,'http://URL','finished'),
    ('Sandra Bullock',162,'sandra-bullock',264,'1991-10-08 00:00:00','2018-08-16 00:00:00',2930499,'Lorem ipsum habitant commodo cubilia eget blandit','Suscipit augue dictum ultrices ultricies aliquam mattis nostra taciti sagittis','R2G0GhTwF','http://URL','True','http://URL',NULL,'pending','acao','True',118840554,'http://URL','finished'),
    ('Jennifer Lawrence',207,'jennifer-lawrence',263,'1988-01-02 00:00:00','2018-08-09 00:00:00',2930443,'Lorem ipsum habitant commodo cubilia eget blandit','Suscipit augue dictum ultrices ultricies aliquam mattis nostra taciti sagittis','DNJNYHWh','http://URL','True','http://URL',NULL,'pending','drama','True',118844113,'http://URL','finished'),
    ('Jennifer Lawrence',207,'jennifer-lawrence',263,'1988-01-02 00:00:00','2018-08-09 00:00:00',2930442,'Lorem ipsum habitant commodo cubilia eget blandit','Suscipit augue dictum ultrices ultricies aliquam mattis nostra taciti sagittis','OqieXJHwh','http://URL','True','http://URL',NULL,'pending','comedia','True',118844112,'http://URL','finished');

这真的是数据库的最佳结构吗?这些值中的重复例如 ('Sandra Bullock', 162, 'sandra-bullock', 264, '1991-10-08 00:00:00', '2018-08-16 00:00:00 ')没有这个怎么优化?

最佳答案

答案取决于您想要实现的目标。此外,了解您将如何进一步使用这些数据也很重要。

  1. 结构不是很好。对于唯一的 JSON 项目,您将拥有多个记录(它们的数量可能很大 - 取决于 JSON)。我不确定以这种方式检索数据是否方便。 优先使用 relational model :

Generally, each table/relation represents one "entity type" (such as customer or product). The rows represent instances of that type of entity (such as "Lee" or "chair") and the columns representing values attributed to that instance (such as address or price).

所以,在你的情况下,我会使用多个表。
-- 视频,包含 {id、titulo、desc 等列}
-- carreira 将相关“atriz”记录的 id 作为单独的列和其他列 {id, inicio, Final}
-- carreira_videos - 存储 carreira-video 关系的单独表。 rable 可能仅包含两列:carreira_id 和 video_id。例如,示例 JSON 中的“Sandra Bullock”项目有带有两个视频的 carreira - 因此,在 carreira_videos 表中您将有两条记录:

carreira_id | video_id
264 | 264 2930500
264 | 264 2930499

-- atrizs 包含 {atriz, atriz-id, atriz-slug} 列和与 carreira_id 相关的单独列。

  1. 实际上,您的示例中存在大量数据重复,这是正确的。如果您的 JSON 在“info”节点中包含不止两个项目,那么就会有更多的数据重复项。如果您将使用关系模型(因为它在基于 SQL 的数据库中是首选)和如上所述的最佳模式,您将避免重复。

  2. 在进一步学习之前,您需要学习关系数据库和 SQL 的基础知识。如果您不理解它们,那么创建优化设计就不是真正的。
    https://www.w3schools.com/sql/
    https://searchdatamanagement.techtarget.com/definition/relational-database

  3. 实际上,如果您想存储 JSON 本身,那么您可能不需要基于 SQL 的数据库。您应该查看 NoSQL 方法和数据库 https://en.wikipedia.org/wiki/NoSQL
    https://www.quackit.com/json/tutorial/list_of_json_databases.cfm

一切都取决于您的目标。

关于mysql - 从 JSON 多维数组到 SQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52182393/

相关文章:

mysql - "Error Code: 1111. Invalid use of group function"带有 JSON 函数

php - 可以使用 JSON 将数据存储在 mysql 数据库中吗?

java - 请问java中这个自动Mysql DB连接可能有什么问题

mysql - 一个表中的两列是否可以具有指向另一个表中同一列的外键?

mysql group_concat 用于左表和右表

sql - 预加载与连接获取相同吗?

json - 将大型多嵌套 json 解析为 scala 案例类

mysql - Magento - 尽管在 local.xml 中设置了远程 IP,但仍尝试通过 unix 套接字进行连接

php 要求($文件) : failed to open stream

javascript - 如何使用 sequelize 或 sql 聚合来自不同表的总和?