Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 737
hughesdan Avatar asked Feb 20 '11 17:02

hughesdan


People also ask

Where can I find FK in SQL Server?

Using SQL Server Management Studio Open the Table Designer for the table containing the foreign key you want to view, right-click in the Table Designer, and choose Relationships from the shortcut menu. In the Foreign Key Relationships dialog box, select the relationship with properties you want to view.

How do I add a foreign key to an existing table in access?

Select one or more tables or queries and then click Add. After you have finished adding tables and queries to the Relationships document tab, click Close. Drag a field (typically the primary key) from one table to the common field (the foreign key) in the other table.

Can not add foreign key constraint?

The usual cause are generally a mismatch in the type of the column of the primary table and the foreign table. It can also be a mismatch in the Engine type of two tables i.e. MyISAM or InnoDB. Datatype both columns should have same datatype. int(11) on one table and smallint(5) on another will cause problem.

How do you find the primary key and foreign key relationship in DB2?

The foreign key of a DB2 table can be found using SYSIBM. SYSFOREIGNKEYS table and SYSIBM. SYSRELS table. The SYSFOREIGNKEYS is a DB2 system table which contains one row for every column of every foreign key.


2 Answers

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.

like image 165
CSiegle Avatar answered Dec 23 '22 06:12

CSiegle


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].

like image 24
Shirley Avatar answered Dec 23 '22 08:12

Shirley