在数据库中存储数组 [英] Storing arrays in the database

查看:122
本文介绍了在数据库中存储数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道在数据库中存储数组是否是一个好的做法?我倾向于使用 json_encode 而不是 serialize ,但只是想知道这是否是一个好主意。

解决方案

不,这是一个可怕的做法。请不要在关系数据库中插入CSV,JSON, serialize()或任何类型的序列化数据。反规范化几乎总是一个坏主意 - 除非你真的知道你在做什么,否则不要这样做,否则你会开始问
问题,比如:这个,...



这样做会损失或严重阻碍您的能力:




  • 使用 JOIN

  • 查找或修改特定元素

  • 强制引用完整性

  • 从索引使用中受益

  • 也浪费空间



这可能听起来很狡猾,但是看到人们这样做是我的宠爱之一 - 特别是考虑到SO上提出的大量问题,如果他们做正确的方式,就可以避免。 / p>

这里是正确的方式在RDBMS中执行一对多和多对多关系。


I'm wondering if it is actually good practise to store Arrays in the database ? I tend to use json_encode rather than serialize, but was just wondering if it is a good idea. If not, then I can make some small changes and just implode the array with a comma.

解决方案

No, it's a terrible practice. Please refrain from inserting CSV, JSON, serialize() or ANY kind of serialized data in a relational database. Denormalization is almost always a bad idea - don't do it unless you really know what you are doing, or you'll start asking questions like: this, this, this, this, ...

Doing that, you lose or it severely hinders your ability to:

  • Use JOINs.
  • Find or modify a particular element
  • Enforce referential integrity
  • Benefit from index usage
  • And it also wastes space

It may sound pedantic, but seeing people do this is one of my pet peeves - especially in light of the plethora of questions asked on SO that would be avoided if they did the right way.

Here's the right way to do one-to-many and many-to-many relationships in an RDBMS.

这篇关于在数据库中存储数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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