简体   繁体   English

在sql中,存储几个有序向量/列表的最佳方法是什么?

[英]In sql, what is the best way to store several ordered vectors/lists?

I have a few integer lists of different length of which I want to preserve the order. 我有一些不同长度的整数列表,我想保留它的顺序。 I will be using them as alternative to each other, never 2 at the same time. 我将使用它们作为彼此的替代,而不是同时使用它们。 The number of lists might grow in the future, although I expect it never to reach a value like 50 or so. 列表的数量可能会在未来增长,但我预计它永远不会达到50左右这样的值。 I might want to insert one value within this list. 我可能想在此列表中插入一个值。 These lists are relatively seldom modified, and using a manual editor like MS SQL Server Management Studio for this purpose is fine. 这些列表相对很少被修改,并且使用像MS SQL Server Management Studio这样的手动编辑器就可以了。 For what I can see in this moment, these lists will be rarely used to directly make queries, there will be some C# in between. 对于我目前可以看到的内容,这些列表很少用于直接进行查询,中间会有一些C#。

For storing one ordered list, a linked (or double-linked) list seems appropriate. 对于存储一个有序列表,链接(或双链接)列表似乎是合适的。 But if I have to store several ordered lists, it seems to me that I will have to add one table for each one of them. 但是,如果我必须存储几个有序列表,在我看来,我将不得不为每个列表添加一个表。 The same is valid if I use an indexed list. 如果我使用索引列表,则相同。 On the other hand, I could also store all these lists in one table transforming them in strings (one string per list) with values comma separated, that I would then parse in my C# program. 另一方面,我还可以将所有这些列表存储在一个表中,将它们转换为字符串(每个列表一个字符串),并以逗号分隔,然后我将在我的C#程序中进行解析。

In sql, what is the best way to store several ordered vectors/lists in sql? 在sql中,在sql中存储几个有序向量/列表的最佳方法是什么?

Relational databases like SQL Server typically don't have "arrays" or "lists" - if you need to store more than one value - they have tables for that. 像SQL Server这样的关系数据库通常没有“数组”或“列表” - 如果需要存储多个值 - 它们都有表格 And no - it's in no way cumbersome for a database like SQL Server to have even thousands of tables - if you really must..... After all - handling tables is the core competency of a relational database ... it should be good at it! 并且不 - 对于像SQL Server这样的数据库来说,甚至数千个表都没有麻烦 - 如果你真的必须......毕竟 - 处理表是关系数据库的核心竞争力 ......它应该是好的在它! (and SQL Server is - and so are many other mature RDBMS systems, too). (和SQL Server一样 - 以及许多其他成熟的RDBMS系统也是如此)。

And I would strongly recommend not to use comma-separated strings - those first of all violate even the first normal form of database design, and as soon as you do need to join those values against something else - you're in trouble. 强烈建议不要使用以逗号分隔的字符串 - 这些字符串首先违反了第一种正常形式的数据库设计,并且只要你需要将这些值加入其他内容 - 你就会遇到麻烦。 Don't do this - there's absolutely no need for this, and you'll just make your developer life miserable sometime in the future - if you have the opportunity to avoid it - don't do it! 不要这样做 - 绝对没有必要这样做,你将在未来的某个时候让你的开发人员生活痛苦 - 如果你有机会避免它 - 不要这样做!

A kind of this? 一种这样的?

ListId   ItemOrder ItemValue
1        1         10
1        4         7
1        2         5
2        1         55
1        7         23
2        4         15

Select ItemValue FROM [Table] WHERE ListId = 1 Order By ItemOrder

Here Each list has an ID (you can use a clustered index here) and the order is given by the field ItemOrder 这里每个列表都有一个ID(你可以在这里使用聚集索引),顺序由字段ItemOrder给出

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

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