简体   繁体   English

json列vs多列

[英]json column vs multiple columns

i don't even know if calling it serialized column is right, but i'm going to explain myself, for example, i have a table for users, i want to store the users phone numbers(cellphone, home, office, etc), so, i was thinkin' to make a column for each number type, but at the same time came to my head an idea, what if i save a json string in a single column, so, i will never have a column that probably will never be used and i can turn that string into a php array when reading the data from database, but i would like to hear the goods and bads of this practice, maybe it is just a bad idea, but first i want to know what other people have to say about 我甚至不知道是否将其称为序列化列是正确的,但我将解释自己,例如,我有一个用户表,我想存储用户的电话号码(手机,家庭,办公室等)所以,我想为每个数字类型创建一个列,但同时我想到了一个想法,如果我在一个列中保存一个json字符串,那么,我将永远不会有一个列可能将永远不会被使用,我可以在从数据库中读取数据时将该字符串转换为php数组,但我想听听这种做法的商品和坏处,也许这只是一个坏主意,但首先我想知道是什么其他人不得不说

thanks 谢谢

Short Answer, Multiple columns. 简答,多列。

Long Answer: 答案很长:

For the love of all that is holy in the world please do not store mutiple data sets in a single text column 为了爱世界上所有圣洁的人,请不要在单个文本列中存储多个数据集

I am assuming you will have a table that will either be 我假设你将有一张桌子

+------------------------------+      +----------------------+
| User |  cell | office | home |  OR  | User | JSON String   |
+------------------------------+      +----------------------+

First I will say both these solutions are not the best solution but if you were to pick the from the two the first is best. 首先,我会说这两种解决方案都不是最好的解决方案,但如果你要从两者中选择第一种解决方案是最好的。 There are a couple reasons mainly though the ability to modify and query specifically is really important. 主要有几个原因,但具体修改和查询的能力非常重要。 Think about the algrothim to modify the second option. 想想algrothim修改第二个选项。

SELECT `JSON` FROM `table` WHERE `User` = ?

Then you have to do a search and replace in either your server side or client side language

Finally you have to reinsert the JSON string

This solution totals 2 queries and a search and replace algorithm. 此解决方案总计2个查询和搜索和替换算法。 No Good! 不好!

Now think about the first solution. 现在想想第一个解决方案。

SELECT * FROM `table` WHERE `User` = ?

Then you can do a simple JSON encode to send it down

To modify you only need one Query.

UPDATE `table` SET `cell` = ? WHERE `User` = ?

to update more than one its again a simple single query 

UPDATE `table` SET `cell` = ?, `home` = ? WHERE `User` = ?

This is clearly better but it is not best 这显然更好,但并不是最好的

There is a third solution Say you want a user to be able to insert an infinite number of phone numbers. 还有第三种解决方案假设您希望用户能够插入无限数量的电话号码。

Lets use a relation table for that so now you have two tables. 让我们使用关系表,所以现在有两个表。

              +-------------------------------------+
+---------+   |      Phone                          | 
| Users   |   +-------------------------------------+ 
+---------+   | user_name| phone_number | type      |
| U_name  |   +-------------------------------------+
+---------+

Now you can query all the phone numbers of a user with something like this 现在,您可以使用类似的内容查询用户的所有电话号码

Now you can query the table via a join 现在,您可以通过连接查询表

SELECT Users. SELECT用户。 , phone. , 电话。 FROM Phone, Users WHERE phone.user_name = ? 来自电话,用户WHERE phone.user_name =? AND Users.U_name = ? AND Users.U_name =?

Inserts are just as easy and type checking is easy too. 插入也很简单,类型检查也很容易。

Remember this is a simple example but SQL really provides a ton of power to your data-structure you should use it rather than avoiding it 请记住,这是一个简单的示例,但SQL确实为您的数据结构提供了大量的功能,您应该使用它而不是避免使用它

If you work with json, there are more elegant ways than MySQL. 如果你使用json,那么有比MySQL更优雅的方法。 Would recommend to use either another Database working better with json, like mongoDB or a wrapper for SQL like Persevere, http://www.persvr.org/Documentation (see "Perstore") 建议使用另一个与json更好地工作的数据库,如mongoDB或像Persevere这样的SQL包装器, http ://www.persvr.org/Documentation(参见“Perstore”)

I would only do this with non-essential data, for example, the user's favorite color, favorite type of marsupial (obviously 'non-essential' is for you to decide). 我只会用非基本数据来做这件事,例如,用户最喜欢的颜色,最喜欢的有袋动物类型(显然“非必要”是供您决定的)。 The problem with doing this for essential data (phone number, username, email, first name, last name, etc) is that you limit yourself to what you can accomplish with the database. 对基本数据(电话号码,用户名,电子邮件,名字,姓氏等)执行此操作的问题在于您将自己限制为可以使用数据库完成的任务。 These include indexing fields, using ORDER BY clauses, or even searching for a specific piece of data. 这些包括索引字段,使用ORDER BY子句,甚至搜索特定的数据。 If later on you realize you need to perform any of these tasks it's going to be a major headache. 如果以后你意识到你需要执行任何这些任务,那将是一个令人头痛的问题。

Your best best in this situation is using a relational table for 1 to many objects - ex UserPhoneNumbers . 在这种情况下,您最好的方法是使用1到多个对象的关系表 - 例如UserPhoneNumbers It would have 3 columns: user_id , phone_number , and type . 它将有3列: user_idphone_numbertype The user_id lets you link the rows in this table to the appropriate User table row, the phone_number is self explanatory, and the type could be 'home', 'cell', 'office', etc. This lets you still perform the tasks I mentioned above, and it also has the added benefit of not wasting space on empty columns, as you only add rows to this table as you need to. user_id允许您将此表中的行链接到相应的User表行, phone_number是自解释的, type可以是'home','cell','office'等。这使您仍然可以执行任务I如上所述,它还具有不浪费空列空间的额外好处,因为您只需要在此表中添加行。

I don't know how familiar you are with MySQL, but if you haven't heard of database normalization and query JOINs, now is a good time to start reading up on them :) 我不知道你对MySQL有多熟悉,但如果你还没有听说过数据库规范化和查询JOIN,现在是开始阅读它们的好时机:)

Hope this helps. 希望这可以帮助。

I'm not sure what the advantages of this approach would be. 我不确定这种方法的优点是什么。 You say "so, i will never have a column that probably will never be used..." What I think you meant was (in your system) that sometimes a user may not have a value for each type of phone number available, and that being the case, why store records with empty columns? 你说“所以,我永远不会有一个可能永远不会被使用的专栏...”我认为你的意思是(在你的系统中)有时用户可能没有可用的每种电话号码的价值,以及既然如此,为什么要用空列存储记录?

Storing records with some empty columns is not necessarily bad. 使用一些空列存储记录不一定是坏事。 However, if you wanted to normalize your database, you could have a separate table for user_phonenumber , and create a 1:many relationship between user and user_phonenumber records. 但是,如果要对数据库进行规范化,则可以为user_phonenumber创建单独的表,并在useruser_phonenumber记录之间创建1:many关系。 The user_phonenumber table would basically have four columns: user_phonenumber表基本上有四列:

  • id (primary key) id(主键)
  • userid (foreign key to user table) userid(用户表的外键)
  • type (eg cellphone, home, office, etc.) 类型(例如手机,家庭,办公室等)
  • value (the phone number) 价值(电话号码)

Constraints would be that id is a primary key, userid is a foreign key for user.id, and type would be an enum (of all possible phone number types). 约束是id是主键,userid是user.id的外键,type是enum(所有可能的电话号码类型)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM