I am wondering if it's really good practise to keep Arrays within the database ? I am inclined to use
json_encode instead of
serialize, but only agreed to be wondering whether it may be beneficial. Otherwise, i quickly could make some small changes and merely implode the array having a comma.
No, it is a terrible practice. Please refrain from placing CSV, JSON,
serialize() or ANY type of serialized data inside a relational database. Denormalization is nearly always an awful idea - do not do it unless of course you actually know your work. Or you'll start asking
questions like: this, this, this, this, ...
Doing that, you lose or it seriously hinders what you can do to:
- Find or modify a specific elememt
- Enforce referential integrity
- Take advantage of index usage
- And in addition it wastes space
It might seem pedantic, but seeing people do that is just one of my pet peeves - especially considering all the different questions requested on To ensure that could be prevented when they did the proper way.
Here's the proper way to complete one-to-many and several-to-many associations within an RDBMS.
Is dependent in your usage pattern. If you are gonna need to access more compact servings of the array (e.g. to be used inside a where clause or similar), then it is an awful idea - you lose all the advantages of storing data inside a relational database by looking into making the information not-relatable. You'll finish track of major overhead removing that small bit of data again and again and also over again.
However, if you are simply using the database like a data store and not have to slice that saved array apart - just place and retrieve, plus there is most likely not a problem whatsoever, apart from maybe waste of space, like a serialized/json'd format is commonly "wordy" and occupy more space compared to raw data itself does.