PHP / MySQL的 - 存储数据库阵列 [英] PHP/MySQL - Storing array in database

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

问题描述

我正在一个PHP应用程序要求的各种设置被存储在数据库中。客户经常询问如果某些东西可以添加或更改/删除,已导致与表的设计问题。基本上,我有很多,如果各种设置进行了特定记录启用它只是指示的布尔字段。

I'm working on a PHP app which requires various settings to be stored in a database. The client often asks if certain things can be added or changed/removed, which has been causing problems with the table design. Basically, I had a lot of boolean fields which simply indicated if various settings were enabled for a particular record.

为了避免与表瞎搞任何更多的,我正在考虑将数据存储作为一个序列化数组。我已阅读,这被认为是不好的做法,但我认为这是使用这种方法正当情况。

In order to avoid messing around with the table any more, I'm considering storing the data as a serialized array. I have read that this is considered bad practice, but I think this is a justified case for using such an approach.

有没有真正的理由,以避免这样做呢?

Is there any real reason to avoid doing this?

任何意见AP preciated。

Any advice appreciated.

感谢。

推荐答案

真正的原因是标准化的,你会打破的第一normalform做它的。

The real reason is normalisation, and you will break the first normalform by doing it.

不过,也有不少案件中,违反正常的形式可以考虑。有多少个字段,你处理的,并都布尔值?

However, there are many cases in which a breach of the normal forms could be considered. How many fields are you dealing with and are they all booleans?

连载存储在数据库中的字符串将有以下缺点(等等)数组:

Storing an array serialized as a string in your database will have the following disadvantages (among others):


  • 当你需要更新你的设置,您必须首先从数据库中提取的当前设置,反序列化数组,改变数组,序列化阵列和更新数据在表中。

  • 在搜索时,你就不能只问数据库中的给定用户(或一组用户)已给定的设置禁用或启用,是否因此你不会有搜索的任何机会。

相反,你应该真正考虑你需要从您的其他表一个一对多的关系记录创建另一个表的选项。因此,你会不会有30个空字段,而是你可以有不同于默认偏离每个选项行(注意,该选项有一些缺点藏汉,例如,如果您更改默认的)。

Instead, you should really consider the option of creating another table with the records you need as a one-to-many relation from your other table. Thus you won't have 30 empty fields, but instead you can just have a row for each option that deviates from the default (note that this option has some disadvantages aswell, for example if you change the default).

在和:我觉得你应该避免序列化阵列和将它们放入数据库,至少如果你关心只是一点点关于上述缺点

In sum: I think you should avoid serializing arrays and putting them into the databases, at least if you care just a tiny bit about the aforementioned disadvantages.

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

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