SQL Server Management Studio - 添加外键令人困惑?

发布于 2024-09-27 05:59:46 字数 504 浏览 6 评论 0原文

我总是发现在 Management Studio 中向主表添加外键很令人困惑。

假设我有一个

Table1 
{
    ID int, -- Primary Key
    Table2ID int, -- Refers to Table2's ID 
}

Table2 
{
    ID int, -- Primary Key
    SomeData nvarchar(50)
}

我正在通过右键单击 -> 向 Table1 添加外键关系->表和列规范。我将“主”弹出窗口设置为 Table2、ID,将“外键表”设置为 Table1、Table2ID。

我的问题:

  1. 不应该列出表2 “外键表”和表1 主键?我的理解是 错误的?

  2. 当我保存时,我收到一条警报“下表将保存到您的数据库中。”它显示了两个表。我真的不明白这个。我只改变了表1。为什么显示第二个表?

I always find it confusing to add foreign keys to primary table in Management Studio.

Lets say I have a

Table1 
{
    ID int, -- Primary Key
    Table2ID int, -- Refers to Table2's ID 
}

Table2 
{
    ID int, -- Primary Key
    SomeData nvarchar(50)
}

I am adding a foreign key to Table1 by Right Click -> Relationships -> Table and column specification. I am setting "Primary" popups to Table2, ID and "Foreign Key Table" to Table1, Table2ID.

My questions:

  1. Shouldn't Table2 be listed for
    "Foreign Key Table" and Table1 for
    Primary Key? Is my understanding
    wrong?

  2. When I save I get an alert "The following tables will be saved to your database." and it shows both tables. I really don't get this. I only changed Table1. Why is the second table shown?

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

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

发布评论

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

评论(5

许久 2024-10-04 05:59:46
  • 单击表旁边的展开符号。
  • 右键单击“Keys”文件夹并选择“新建外键”。
  • (或者,打开表后,您可以单击工具条上的“关系”按钮)
  • 单击“表和列规范”行上的“...”按钮以打开编辑器。
  • 左侧的下拉列表将是您要添加的表,静态文本字段将列出您要添加到的表。
  • 使用下拉列表指定约束,并确保两侧具有相同的列数。
  • Click the expand symbol next to the table.
  • Right click on the "Keys" folder and select "New Foreign Key."
  • (Alternatively, you can click the Relationships button on the toolstrip when you have the table open)
  • Click the "..." button on the "Tables and Columns Specifications" row to open the editor.
  • The drop down on the left will be the table you're adding from, and the static text field will list the table you're adding to.
  • Use the dropdowns to specify your constraints, and be sure both sides have the same number of columns.
注定孤独终老 2024-10-04 05:59:46

为什么不直接使用等效的 T-SQL 语句?对我来说似乎更容易,也更容易混淆:

ALTER TABLE dbo.Table1
  ADD CONSTRAINT FK_Table1_Table2
    FOREIGN KEY(Table2ID) REFERENCES dbo.Table2(ID)

当我读到这篇文章时,我相信这立即清楚了所涉及的两个表以及它们是如何连接的(Table1.Table2ID --(references)--> ; Table2.ID

如果您想留在 SSMS 设计器世界中:您还可以创建所涉及表的数据库图表,然后只需拖放您的 Table2IDTable1 列到 Table2 并将其拖放到那里的 ID 列 - 这会以图形方式告诉 SSMS 您想要做什么,您只需需要检查您的选择,然后在弹出的对话框中单击“确定”。

Why don't you just use the equivalent T-SQL statements?? Seems much easier and less confusing to me:

ALTER TABLE dbo.Table1
  ADD CONSTRAINT FK_Table1_Table2
    FOREIGN KEY(Table2ID) REFERENCES dbo.Table2(ID)

When I read this, I believe this is immediately clear what two tables are involved, and how they are connected (Table1.Table2ID --(references)--> Table2.ID)

If you want to stay in the SSMS designer world: you could also create a database diagram of your tables involved, and then just drag&drop your Table2ID column from Table1 over to Table2 and drop it onto the ID column there - this would graphically tell SSMS what you want to do, and you just need to review your choices and click OK on the dialog that pops up.

嗳卜坏 2024-10-04 05:59:46

1.不应该为“外键表”列出Table2,为“外键表”列出Table1吗?
主键?我的理解是
错了?

我相信你的理解是错误的。 Table2 是您正在引用其主键的表。因此它列在主键下。 Table1 是将具有外键(对另一个表的主键的引用)的表;因此它列在“外键表”下。

至于为什么保存两个表,即使后来列出外键属于 Table1:我相信这是因为外键约束了两个表。他们都必须“了解”约束,因此他们都需要被保存。

1.Shouldn't Table2 be listed for "Foreign Key Table" and Table1 for
Primary Key? Is my understanding
wrong?

I believe your understanding is wrong. Table2 is the table whose primary key you are referencing. Therefore it's listed under Primary Key. Table1 is the table that will have the foreign key (the reference to the primary key of another table); therefore it's listed under "Foreign Key Table".

As far as why both tables are saved, even though the foreign key is listed afterward as belonging to Table1: I believe it's because the foreign key constrains both tables. They both have to "know" about the constraint, so they both need to be saved.

遗失的美好 2024-10-04 05:59:46

按照这种方式在表中创建外键。

  1. 右键单击该列,然后单击关系

输入图像描述此处

  1. 单击... 中的特定于表和列

在此处输入图像描述

  1. 选择主键表和键左侧,然后选择要映射到右侧的当前表格键。

输入图像描述此处

  1. 单击特定于表和列中的向下箭头以确认外键映射。

输入图像描述这里

  1. 单击关闭按钮,现在外键已创建。

Follow this way to create a foreign key in your table.

  1. Right-click the column and click Relationship.

enter image description here

  1. Click the Tables And Column Specific in the ...

enter image description here

  1. Select the primary key table and key on the left side and select the current table key you want to map on the right side.

enter image description here

  1. click the down arrow in Tables And Column Specific to confirm foreign key mapping.

enter image description here

  1. Click the close button and now a foreign key is created.
醉酒的小男人 2024-10-04 05:59:46
ALTER TABLE dbo.Table1
  ADD CONSTRAINT FK_Table1_Table2
    FOREIGN KEY(Table2ID) REFERENCES dbo.Table2(ID)
ALTER TABLE dbo.Table1
  ADD CONSTRAINT FK_Table1_Table2
    FOREIGN KEY(Table2ID) REFERENCES dbo.Table2(ID)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文