php 将 JSON 存储在 MySQL 数据库中?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15395287/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 09:09:24  来源:igfitidea点击:

Storing JSON in MySQL database?

phpmysqljson

提问by user1947561

Is it acceptable to store JSON data in a MySQL table row? I need to store arrays in a mysql database. The problem is, i don't know how many columns i will need for each user. So I thought to store JSON in one row named arrayfor exemple. Is this the best way?

将 JSON 数据存储在 MySQL 表行中是否可以接受?我需要将数组存储在 mysql 数据库中。问题是,我不知道每个用户需要多少列。所以我想将 JSON 存储在一行中,例如命名array。这是最好的方法吗?

Edit:

编辑:

Also, I am using textas table column type.

另外,我使用text作为表列类型。

回答by Gustav

Yes, it's a very good idea to use mysql as a key-value store, in fact facebook does for some uses.

是的,使用 mysql 作为键值存储是一个很好的主意,事实上 facebook 确实有一些用途。

CREATE TABLE `json` (
    `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
    `data` blob NOT NULL,
    PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

The above table-structure will get you far. It's very easy to shard or cluster.

上面的表结构会让你走得更远。分片或集群非常容易。

Edit: The point here is to use PHP, Ruby etc to handle the json/data. You do SELECT ..., do your edits, then INSERT ... ON DUPLICATE KEY UPDATE ....

编辑:这里的重点是使用 PHP、Ruby 等来处理 json/data。你做SELECT ...,做你的编辑,然后INSERT ... ON DUPLICATE KEY UPDATE ...

回答by jonixj

Storing more than one piece of data in a relational database field is generally wrong. It is possible to think of cases where it would be acceptable, but not for storing an entire user.

在关系数据库字段中存储多个数据通常是错误的。可以考虑可以接受但不能存储整个用户的情况。

If a using a relational database structure is not sufficient you may want to look at a NoSQL database inestead of MySQL.

如果使用关系数据库结构还不够,您可能需要查看 NoSQL 数据库而不是 MySQL。

回答by Apollo SOFTWARE

It's not a problem. mySQL can already handle most of the brackets and funky characters in their text field.

这不是问题。mySQL 已经可以处理其文本字段中的大多数括号和时髦字符。

回答by sedat sevgili

I think it is not. What about four months later you decide add a new attribute to your entity or remove some attributes from your entity? How will you parse your old json contents? If you dont know how many columns you will need in your table, you should think in a different way and maybe create a dynamic structure like using user_column table

我认为不是。四个月后,您决定向实体添加新属性还是从实体中删除一些属性呢?您将如何解析旧的 json 内容?如果您不知道您的表中需要多少列,您应该换一种方式思考,并可能创建一个动态结构,例如使用 user_column 表