PHP / MySQL的 - 存储阵列中的数据为JSON,不好的做法? [英] PHP/MySQL - Storing array data as JSON, bad practice?

查看:99
本文介绍了PHP / MySQL的 - 存储阵列中的数据为JSON,不好的做法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在想,是否不存储阵列在一个MySQL文本字段JSON字符串是很好的做法。

I was wondering whether or not storing an array as a JSON string in a mysql text field is good practice.

我创建的发票,其允许用户对产品无限数量添加到发票。当提交表单时,它需要所有空白的项目等,但我通常会留下2-5个项目因。每个项目都有一个SKU,价格,名称和说明。

I am creating an invoice which allows the user to add an unlimited number of products to the invoice. When the form is submitted, it takes out all the blank items and such, but I will typically be left with 2-5 items depending. Each item has a sku, price, name, and description.

我对这种情况的选项是:(1)创建一个新产品的表格,添加每个项目作为一个新行,带发票表链接它,并访问数据时调用两个表。 (2)所有的产品数据存储在发票表中的单个JSON文本字段,然后我没有创建或访问另一个表。

My options for this situation are (1) to create a new product table, add each item as a new row, link it with the invoice table, and call both tables when accessing data. Or (2) store all the product data as a single JSON text field in the invoice table, and then I am not creating or accessing another table.

由于我是pretty刚性与MySQL编程,我的感觉是在MySQL中使用JSON就会授人皱起了眉头。我对吗?有人可以阐明这一些轻?

Since I am pretty rigid with MySQL programming, I get the feeling that using JSON in MySQL would be frowned upon. Am I right? Can someone shed some light on this?

推荐答案

如果你需要的仅仅是商品 - 那么它是不是一个不好的做法

If all you need is just to store - then it is not a bad practice.

但是,如果你需要执行任何种类的加工,整理或类似的东西 - 你需要它正常化

But if you need to perform any sort of processing, sorting or something similar - you need to normalize it.

这篇关于PHP / MySQL的 - 存储阵列中的数据为JSON,不好的做法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆