Why does my newly created FK relationship not appear in object explorer?
Asked Answered
S

2

11

I just added a table "PackageItems" to an existing SQL Server 2008 R2 database. I'm working in SQL Server Management Studio. After creating the new table I created a relationship between that table and another table called "Package". The relationship FK_PackageItems_Package makes the primary key of the Package table (PackageID) the foreign key in my new PackageItems table. Both tables have a common column called PackageID (int, not null).

My question is the following. The primary and foreign keys for every other table in the database are clearly denoted in the Object Explorer panel in SSMS. I see gold or gray keys beside the columns and I also see a PK or FK in the parentheses beside. But my newly created relationship (FK_PackageItems_Package) is not represented in this way. Did I do something wrong?

Note, to create the FK relationships I entered the design view for PackageItems, clicked the Relationships icon, clicked Add, selected the "..." button next to "Tables and Columns Specification". Then in the window that appeared I chose Package as my primary key table and PackageID. And I chose PackageItems and PackageID for my foreign key table and column.

Hopefully this is not too dumb of a question. Thanks in advance for taking a look.

Suspect answered 20/2, 2011 at 17:2 Comment(2)
Most nodes in the treeview have a "refresh" context-menu item; did you try that? Without it, SSMS doesn't even notice the new database it just created itself.Tearjerker
Thank you! That solved it. I'll accept your answer as soon as I locate the link to do so.Suspect
R
21

When you open the Create Foreign Key dialog window, you'll notice that the GUI adds the table design window. After configuring the keys and close the window, you must also save the table design.

Rootstock answered 22/7, 2016 at 17:38 Comment(1)
I can't believe how poor the UI design is for this operation. There is absolutely no indication that this is required. Also the three dots that allow selection of table columns for the foreign key are only visible when the "Tables and Columns Specification" row is highlighted. It should not be necessary to ask Stack Overflow how to perform a simple operation for which a dialog exists.Selfwill
S
4

There might be an issue when using design view to create a foreign key. If I try to create a foreign key with the GUI view, it doesn't get created. If I create it with a script, it does.

To verify if the foreign key has been created or not, you can use sp_help [TableName].

Shock answered 3/3, 2015 at 1:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.