存储“列表”列表的最正确方法是什么?在SQL数据库中? [英] What is the most correct way to store a "list" in a SQL Database?

查看:423
本文介绍了存储“列表”列表的最正确方法是什么?在SQL数据库中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我读了很多关于如何将多个值存储到一个列中是一个坏主意,并且违反了数据规范化的第一条规则(令人惊讶的是,它不是不谈论数据规范化),所以我需要一些帮助。

So, I've read a lot about how stashing multiple values into one column is a bad idea and violates the first rule of data normalisation (which, surprisingly, is not "Do Not Talk About Data Normalisation") so I need some help.

目前,我正在为自己工作的地方设计一个ASP .NET网页。我想根据个人所属的Active Directory组在网页上显示数据。想到的第一种方法是创建一个表,该表实质上包含一个包含AD组的列,第二个列包含属于该列表的计算机列表。

At the moment I'm designing an ASP .NET webpage for the place I work for. I want to display data on a web page depending on what Active Directory groups the person belongs to. The first way of doing this that comes to mind is to have a table with, essentially, a column containing the AD group and the second column containing what list of computers belong to that list.

我了解到,这对关系数据库表现出极大的漠视,那么有什么更好的方法呢?我想通过SQL表控制此访问,因此我可以从这些表中添加/删除并相应地更改最终用户访问。

I've learnt that this is showing great disregard for relational databases, so what is a better way to do it? I want to control this access by SQL tables, so I can add/remove from these tables and change end users access accordingly.

感谢您的帮助! :)

编辑:确切地描述我想做的是:

To describe exactly what I want to do is this:

我们有一个特定的组需要检查的计算机数量,但是这些计算机实际上很难到达位置。我所属的组织已为这些计算机启用了远程控制,但是它们并不能提供远程控制密码(可理解)。

We have a certain group of computers that need to be checked up on, however these computers are in physically difficult to reach locations. The organisation I belong to has remote control enabled for these computers, however they're not in the business of giving out the remote control password (understandable).

添加的层复杂性在于,根据您的身份,我们的客户应该只能看到特定的计算机组(即他们所在区域拥有的计算机组)。因此,如果A组中有Thomas,B组中有Jones,那么如果您属于这两个组,则只会看到一个条目。但是,如果您同时属于这两个组,则应该在其中都看到Thomas和Jones计算机。

The added layer of complexity is that, depending on who you are, our clients should only be able to see a certain group of computers (that is, the group of computers that their area owns). So, if Group A has Thomas in it, and Group B has Jones in it, if you belong to either group then you would just see one entry. However, if you belong to both groups you should see both Thomas and Jones computers in it.

原因我认为将数据存储在SQL单元中的原因是因为将它们存储在表中(在我看来)需要为每个新的计算机组创建一个新表。我不想为每个新组都创建SQL表,我宁愿只是在SQL表中的某个地方添加一行。

The reason why I think that storing this data in a SQL cell is the way to go is because, to store them in tables would require (in my mind) a new table for each new "group" of computers. I don't want to crank out SQL tables for every new group, I'd much rather just have an added row in a SQL table somewhere.

推荐答案

在SQL Server中,您基本上有三个选择:

You basically have three options in SQL Server:


  • 将值存储在单个列中。

  • 将值存储在联结表中。

  • 将值存储为XML(或其他某种结构化数据格式)。

(其他数据库还有其他选项,例如数组,嵌套表和JSON )。

(Other databases have other options, such as arrays, nested tables, and JSON.)

在几乎所有情况下,使用联结表都是正确的方法。为什么?以下是一些原因:

In almost all cases, using a junction table is the correct approach. Why? Here are some reasons:


  • SQL Server具有(相对)糟糕的字符串操作,因此只需确保唯一列表确实很简单,

  • 连接表允许您存储许多其他信息(何时添加了计算机?计算机的完整描述是什么?等等。) >
  • 使用联结表,您想要的大多数查询都非常简单(一个例外是获取逗号分隔的列表,a-这只是违反直觉的,而不是硬的)。 >
  • 所有类型都是本地存储的。

  • 连接表允许您对列表的元素强制执行约束(包括检查和外键)。

尽管定界列表几乎永远都不是正确的解决方案,但可以考虑一下可能有用的情况:

Although a delimited list is almost never the right solution, it is possible to think of cases where it might be useful:


  • 列表不变,列表的显示非常重要。

  • 空间使用是问题(alas,非规范化通常导致页面减少)。

  • 查询并不能真正访问列表中的元素,而只能访问整个列表。

在某些情况下,XML也是一个合理的选择。在最新版本的SQL Server中,这可以变得非常有效。但是,这会导致读取和解析XML的开销-重复消除之类的事情仍然不明显。

XML is also a reasonable choice under some circumstances. In the most recent versions of SQL Server, this can be made pretty efficient. However, it incurs the overhead of reading and parsing XML -- and things like duplicate elimination are still not obvious.

因此,您确实有选择。在几乎所有情况下,联结表都是正确的方法。

So, you do have options. In almost all cases, the junction table is the right approach.

这篇关于存储“列表”列表的最正确方法是什么?在SQL数据库中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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