SQL Server Management Studio - adding foreign key confusing?

Viewed 94303

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?

5 Answers

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