SQL 信息架构 - 读取复合外键

发布于 2024-10-31 20:00:26 字数 1573 浏览 0 评论 0原文

我正在开发一个通用应用程序,该应用程序使用一组预先选择的数据库表(及其关系)来生成用于管理这些表中的数据的用户界面。基本上,我只是从信息模式中查找内容,获取所有主键和外键,并基于此生成输入字段和指向其他对话框的链接。因为它应该是一个通用的解决方案,所以它应该适用于任何彼此之间具有有意义关系的表集。

我遇到的事情之一是外键,特别是在一种约束下的复合外键。例如,如果我有以下表格:

Company

CompanyID, CompanyName, CompanyID
--companyID is the primary key which identifies the company. 

Division

DivisionID, CompanyID, DivisionName, DivisionID+CompanyID
--DivisionID+CompanyID is a composite primary key for a division, because it's a one-to-many relationship and division is DEPENDENT on Company. 

Team

TeamID, DivisionID, CompanyID, TeamName, TeamID+DivisionID+CompanyID
--same as above - a Team is dependent on Division, which has a composite primary key.

现在,根据所有定义,此数据库模型是一个有效的模型(并且SQL Server 允许这样做)-但我遇到了一个问题。

例如,在信息模式中,DivisionID 和 CompanyID 都被“分配”到相同的 CONSTRAINT。因此,当我加入正确的表时,我遇到了问题。无法知道一个表中的哪一列是另一个表中的哪一列。在上面的示例中,列名称是相同的(CompanyID 是与公司 ID 相关的每个列的名称 - 无论是在 COMPANY 表中还是在 TEAM 表中,等等......)但是,没有任何规则说列的名称必须相同,所以我不知道如何真正让程序知道哪一列是哪一列。

TABLE1    COLUMN1        CONSTRAINT         TABLE2     COLUMN2
TEAM      CompID      TEAM_HAS_DIVISION    DIVISION   CompanyID
TEAM      DivID       TEAM_HAS_DIVISION    DIVISION   DivisionID

有没有办法让计算机知道表 TEAM 中的 CompID 引用 CompanyID,而不是 DIVISION 表中的 DivisionID?

我无法从 INFORMATION_SCHEMA 视图中的数据中找到任何方法来执行此操作。

是的,人类可以很容易地找出 CompID=CompanyID 等...但正如我之前提到的 - 我正在尝试制定一个通用的解决方案,不需要人查看这个并做出决定和错误:= )

I'm working on a generic application that uses a set of pre-selected database tables (and it's relationships) to generate a user interface for managing data in those tables. Basicaly, I'm just looking up stuff from the information schema, getting all the primary and foreign keys and generating input fields and links to other dialogs based on that. Because it's supposed to be a generic solution, it should work with any set of tables that do have a meaningful relationship among each other.

One of the things I ran into are foreign keys, in particular - COMPOSITE foreign keys under one constraint. For example, if I have the following tables:

Company

CompanyID, CompanyName, CompanyID
--companyID is the primary key which identifies the company. 

Division

DivisionID, CompanyID, DivisionName, DivisionID+CompanyID
--DivisionID+CompanyID is a composite primary key for a division, because it's a one-to-many relationship and division is DEPENDENT on Company. 

Team

TeamID, DivisionID, CompanyID, TeamName, TeamID+DivisionID+CompanyID
--same as above - a Team is dependent on Division, which has a composite primary key.

Now, this database model is - by all definitions - a valid model (and SQL Server allows it) - but I've come across a certain problem.

For example, in the information schema, both DivisionID and CompanyID are 'assigned' to the same CONSTRAINT. So, when I join the right tables, I get a problem. There's no way of knowing which column in one table is which column in the other. In the above example, the column names are the same (CompanyID is the name of every column that relates to the ID of the company - be it in the COMPANY table or the TEAM table, etc...) but, there's no rule that says the name of the columns must be the same, so I am at a loss on how to actually make the program know which column is which.

TABLE1    COLUMN1        CONSTRAINT         TABLE2     COLUMN2
TEAM      CompID      TEAM_HAS_DIVISION    DIVISION   CompanyID
TEAM      DivID       TEAM_HAS_DIVISION    DIVISION   DivisionID

Is there a way for the computer to know that CompID in Table TEAM references CompanyID, and not DivisionID in the DIVISION table?

I couldn't find any way to do this from the data in INFORMATION_SCHEMA views.

Yes, a human can easily figure out that CompID=CompanyID, etc... but as I mentioned previously - I'm trying to make a generic solution that wouldn't require a person to look at this and make decisions and mistakes :=)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

南冥有猫 2024-11-07 20:00:26

您必须按列顺序将它们匹配。

  • REFERENTIAL_CONSTRAINTS 告诉您 TEAM_HAS_DIVISION 约束将 PK_Division 作为您的主键
  • KEY_COLUMN_USAGE 告诉您 TEAM_HAS_DIVISION 按顺序具有 DivID 和 CompID,由 ORDINAL_POSITION 列指定
  • KEY_COLUMN_USAGE 还告诉您 PK_Division 按顺序具有 DivisionID 和 CompanyID,由 ORDINAL_POSITION 列指定

这就是方法你知道哪个指的是哪个。

You have to match them up by column order.

  • REFERENTIAL_CONSTRAINTS tells you that TEAM_HAS_DIVISION constraint has PK_Division as your primary key
  • KEY_COLUMN_USAGE tells you that TEAM_HAS_DIVISION has DivID and CompID in that order, specified by ORDINAL_POSITION column
  • KEY_COLUMN_USAGE also tells you that PK_Division has DivisionID and CompanyID in that order, specified by ORDINAL_POSITION column

This is how you know which one refers to which.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文