是存储在MySQL中的数据为JSON是个好主意?

编程入门 行业动态 更新时间:2024-10-24 20:18:38
本文介绍了是存储在MySQL中的数据为JSON是个好主意?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个场景,有这样的关系表一节有很多元素,让我想到了去为的has_many关系模型像一个部分有许多元素。

我从我们的客户一些建议,说要救一个列,而不是多个表中的数据为JSON。

我有点糊涂了哪些方法,我应该走了。

建议,欢迎。

这是模型正在提议

如:部分的has_many元素

表:部分

ID类型 1页

表:元素

ID类型内容section_id 1文字< P> lipsum lipsum< / P> 1 2图像/images/test.png 1

客户的建议是,

ID类型的内容  第1页{:文本=> &其中p为H.; lipsum&所述; / P>中,图象=> /images/test.png}

解决方案

在一个单一的数据库字段存储的数据作为JSON通常是一个坏主意。这意味着你将无法操纵/使用SQL查询数据 - 哪一种失败将数据存储在数据库中的点

我建议你部分表和表之间存在一个一对多的关系为文本和图片。这可以通过 belongs_to的和的的has_many 协会的Rails

不管怎么说,创建新表将只有当你有多个文本和图像的一个部分是有帮助的。如果你有你的每一个部分的单一文本和单个图像(作为例子提出),你可能会被罚款有一个部分表 ID ,键入,文本和图像属性。

I got a scenario to have relational tables like a "section has many elements", so I thought of going for has_many relation model like a section has many elements.

I got some suggestions from our client saying to save the data as JSON inside a column instead of multiple tables.

I am bit confused which approach I should go with.

Suggestions are welcome.

This is the model am proposing

eg : section has_many elements

Table : sections

id type 1 page

Table : elements

id type content section_id 1 text <p>lipsum lipsum</p> 1 2 image /images/test.png 1

Client proposal is,

id type content 1 page {:text => "<p>lipsum</p>", image => "/images/test.png"}

解决方案

Storing data as a JSON in a single database field is generally a bad idea. It means you will not be able to manipulate/query the data using SQL - which kind of defeats the point of storing the data in a database.

I'd recommend a one-to-many relationship between your section table and tables for texts and images. That can be achieved through belongs_to and has_many associations in Rails.

Anyways, creating new tables will only be helpful if you have multiple texts and images for a single section. If you have a single text and a single image for each of your sections (as the example proposes), you'll probably be fine with a single sections table with id, type, text, and image attributes.

更多推荐

是存储在MySQL中的数据为JSON是个好主意?

本文发布于:2023-10-21 17:24:48,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1514869.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:是个   好主意   数据   MySQL   JSON

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!