本文介绍了如何将列表存储在数据库表的列中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 因此,根据 Mehrdad对相关问题的回答,I 得到它,正确的数据库表列不存储列表。相反,您应该创建一个有效保存所述列表元素的另一个表,然后直接或通过连接表链接到该表。但是,我想创建的列表类型将由唯一的项目组成(与链接的问题水果示例不同)。此外,我的列表中的项目被明确排序 - 这意味着如果我将元素存储在另一个表中,我必须在每次访问它们时对它们进行排序。最后,列表基本上是原子的,因为任何时候我希望访问列表,我想要访问整个列表,而不是只是一个它 - 所以看起来很愚蠢的必须发出一个数据库查询来收集一些列表。So, per Mehrdad's answer to a related question, I get it that a "proper" database table column doesn't store a list. Rather, you should create another table that effectively holds the elements of said list and then link to it directly or through a junction table. However, the type of list I want to create will be composed of unique items (unlike the linked question's fruit example). Furthermore, the items in my list are explicitly sorted - which means that if I stored the elements in another table, I'd have to sort them every time I accessed them. Finally, the list is basically atomic in that any time I wish to access the list, I will want to access the entire list rather than just a piece of it - so it seems silly to have to issue a database query to gather together pieces of the list. AKX的解决方案(上面链接)是将列表序列化并存储在二进制列中。但这也似乎不方便,因为这意味着我必须担心序列化和反序列化。AKX's solution (linked above) is to serialize the list and store it in a binary column. But this also seems inconvenient because it means that I have to worry about serialization and deserialization.有没有更好的解决方案?如果 没有更好的解决方案,那为什么?似乎这个问题应该不时出现。Is there any better solution? If there is no better solution, then why? It seems that this problem should come up from time to time. ...只是一些更多的信息让你知道我来自哪里。一旦我刚刚开始理解SQL和数据库,我就被转向LINQ to SQL,所以现在我有点被宠坏了,因为我希望处理我的编程对象模型,而不必考虑对象被查询或存储在数据库中。... just a little more info to let you know where I'm coming from. As soon as I had just begun understanding SQL and databases in general, I was turned on to LINQ to SQL, and so now I'm a little spoiled because I expect to deal with my programming object model without having to think about how the objects are queried or stored in the database.感谢所有! John更新:所以在我得到的第一个答案中,我看到你可以去CSV / XML路由,但不要!所以现在我正在寻找为什么的解释。给我一些很好的参考。UPDATE: So in the first flurry of answers I'm getting, I see "you can go the CSV/XML route... but DON'T!". So now I'm looking for explanations of why. Point me to some good references.另外,给你一个更好的了解我要做的事情:在我的数据库中,我有一个功能表,它将有一个列表(x,y)对。 (该表还将有其他信息对我们的讨论没有任何影响。)我将永远不需要看到(x,y)对的列表的一部分。相反,我会把它们全部放在屏幕上。我将允许用户拖动节点来偶尔更改值,或者添加更多的值到图上。Also, to give you a better idea of what I'm up to: In my database I have a Function table that will have a list of (x,y) pairs. (The table will also have other information that is of no consequence for our discussion.) I will never need to see part of the list of (x,y) pairs. Rather, I will take all of them and plot them on the screen. I will allow the user to drag the nodes around to change the values occasionally or add more values to the plot.推荐答案不,没有更好的方式将一系列项目存储在单个列中。关系数据库专门设计用于存储每行/列组合中的一个值。为了存储多个值,您必须将您的列表序列化为单个值进行存储,然后在检索时对其进行反序列化。没有其他方法可以做你正在谈论的事情(因为你所说的是一个坏的主意,一般应该不会被完成)。No, there is no "better" way to store a sequence of items in a single column. Relational databases are designed specifically to store one value per row/column combination. In order to store more than one value, you must serialize your list into a single value for storage, then deserialize it upon retrieval. There is no other way to do what you're talking about (because what you're talking about is a bad idea that should, in general, never be done).我明白你认为创建另一个表来存储该列表是愚蠢的,但这正是关系数据库所做的。您正在争取一场艰苦的战斗,违反了关系数据库设计的最基本原则之一,没有任何理由。由于您声明您只是在学习SQL,因此我强烈建议您避免这种想法,并遵守更多经验丰富的SQL开发人员向您推荐的做法。I understand that you think it's silly to create another table to store that list, but this is exactly what relational databases do. You're fighting an uphill battle and violating one of the most basic principles of relational database design for no good reason. Since you state that you're just learning SQL, I would strongly advise you to avoid this idea and stick with the practices recommended to you by more seasoned SQL developers.您违反的原则称为第一正常格式 ,这是数据库规范化的第一步。The principle you're violating is called first normal form, which is the first step in database normalization.过分简化事情的风险,数据库规范化是根据数据定义数据库的过程,因此您可以对其编写合理且一致的查询,并能轻松维护。规范化旨在限制数据中的逻辑不一致和腐败,并且有很多级别。关于数据库规范化的维基百科文章其实相当不错。At the risk of oversimplifying things, database normalization is the process of defining your database based upon what the data is, so that you can write sensible, consistent queries against it and be able to maintain it easily. Normalization is designed to limit logical inconsistencies and corruption in your data, and there are a lot of levels to it. The Wikipedia article on database normalization is actually pretty good.基本上,规范化的第一条规则(或形式)表示您的表必须表示一个关系。这意味着:Basically, the first rule (or form) of normalization states that your table must represent a relation. This means that: 您必须能够区分一行与任何其他行(换句话说,您的表必须具有可以作为主键,这也意味着不应该重复行。 数据的任何排序必须由数据定义,而不是由物理行的排序(SQL是基于一个集合的想法,这意味着您应该依赖的 排序是您在查询中显式定义的) 每行/列交集必须包含一个,只能包含一个值You must be able to differentiate one row from any other row (in other words, you table must have something that can serve as a primary key. This also means that no row should be duplicated.Any ordering of the data must be defined by the data, not by the physical ordering of the rows (SQL is based upon the idea of a set, meaning that the only ordering you should rely on is that which you explicitly define in your query)Every row/column intersection must contain one and only one value最后一点显然是这里的突出点,SQL旨在为您存储您的套件,而不是为您提供一个桶,您可以自己存储一套,是的,可以做,不,世界赢了但是,您已经瘫痪了自己,了解SQL以及随之而来的最佳做法,立即跳入使用ORM。 LINQ to SQL是太棒了,就像绘图计算器一样。但是,同样的情况下,他们应该不会替代知道他们使用的流程实际工作的方式。The last point is obviously the salient point here. SQL is designed to store your sets for you, not to provide you with a "bucket" for you to store a set yourself. Yes, it's possible to do. No, the world won't end. You have, however, already crippled yourself in understanding SQL and the best practices that go along with it by immediately jumping into using an ORM. LINQ to SQL is fantastic, just like graphing calculators are. In the same vein, however, they should not be used as a substitute for knowing how the processes they employ actually work.您的列表可能现在完全是原子的,这个项目可能不会改变。但是,您会习惯于在其他项目中做类似的事情,最终(可能很快)会遇到一个现在适合您的快速易于列表的列表方法完全不合适。为您正在尝试存储的内容创建正确的表没有太多额外的工作,当数据库设计出现时,您不会被其他SQL开发人员所嘲笑。此外,LINQ to SQL将会看到您的关系,并为您的列表自动提供正确的面向对象界面。为什么你会放弃ORM提供给您的便利,以便您可以执行非标准和不明智的数据库破解?Your list may be entirely "atomic" now, and that may not change for this project. But you will, however, get into the habit of doing similar things in other projects, and you'll eventually (likely quickly) run into a scenario where you're now fitting your quick-n-easy list-in-a-column approach where it is wholly inappropriate. There is not much additional work in creating the correct table for what you're trying to store, and you won't be derided by other SQL developers when they see your database design. Besides, LINQ to SQL is going to see your relation and give you the proper object-oriented interface to your list automatically. Why would you give up the convenience offered to you by the ORM so that you can perform nonstandard and ill-advised database hackery? 这篇关于如何将列表存储在数据库表的列中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-24 18:44
查看更多