Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
233 views
in Technique[技术] by (71.8m points)

php - Storing arrays in the database

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

*Although some SQL databases have built-in support for JSON, it's often better to restructure your data so that you don't need this


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...