views:

49

answers:

1

So I'm working on just a learning project to expose myself to doing some things I do not get to do at work. I'm just making a simple bug and case tracking app (I know there are a million this is just to work with some tools I don't get to). So I was designing my database and realized I've never actually used Foreign Keys before in any of my projects, I've used them before but never actually setting up a column as a FK. So I've designed my database as follows, which I think is close to correct (at least for the initial layout). Database Image

However When I try to add the FK's to the linking Tables I get an error saying, "The tables present in the relationship must have the same number of columns". I'm doing this by in SQLSMS by going to the Keys 'folder' and adding a FK. Is there something that I am doing wrong here, I don't understand why the tables would have to have the same number of columns for me to add a FK relationship between the tables?

A: 

When defining a foreign key relationship there are four things you need to specify:

  • The referring table (foreign key table)
  • The columns from the referring table that will form the foreign key.
  • The referred table (primary key table)
  • The columns from the referred table that form the key.

I'm guessing that you have selected one of the above incorrectly - probably you have forgotten that you need to state the column involved in the relationship twice - once for each table.

Here's a screenshot of how it should look - note that the column is specified twice. In my example the columns have slightly different names to demonstrate that they don't always need to have the same name, but in your case the column names will be the same.

Mark Byers
Okay wow I didn't even see that box under there, thanks!
msarchet
yea I got it to not throw me the error but should that be showing up under my Keys 'folder' for my the table I added the key to?
msarchet
@msarchet: Are you sure you have saved all your changes? Making a modification to a table doesn't actually do anything unless you save the table. Even then, the GUI doesn't always show the changes. There is no automatic refresh when the schema changes.
Mark Byers
That what it was I didn't realize I wasn't saving the actual key I created it properly and hit Ctrl+S but not when I was over the main screen. Thanks for the help.
msarchet