开发者

Trying to verify understanding of foreign keys SQL Server

开发者 https://www.devze.com 2022-12-27 11:45 出处:网络
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 jus

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 http://drop.io/download/public/uurp1vxej0abpwu7jsee/895663dedff577359b900cda1726f115b24开发者_Python百科be90c/Asset/28368622/v3/large_thumbnail

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?


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.

Trying to verify understanding of foreign keys SQL Server

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号