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.
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.
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.
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.
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.
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.
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].
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With