开发者

SQL Server: What would be the appropriate way to define the relationships (borders) between states?

Suppose you have to build a DB tables that depicts states and their borders between each other. Let's say the States table would look like:

States: Name(PK), Size, etc...

What would be the appropriate way to define the relationships (borders) betw开发者_运维百科een states?

I came up with three alternatives -

  1. Defining a Borders table with primary key combined by two fields: Id(PK), StateName(PK,FK)
  2. Defining a Borders table with StateName1(PK,FK), StateName2(PK,FK)
  3. Defining a Borders table with a concatenated value of two states' names.

Some more information:

  • I am going to query the data as follows: someState.HasBorderWith(State anotherState)
  • I use EF 4.0 with POCO entities.


Option 2 is the standard implementation of such a relationship. I don't understand what you mean w/ option 1, and option 3 isn't an option - it would be a nightmare to query this!


Personally, I would create a table with:

 Id (PK)
 StateName1 (FK)
 StateName2 (FK)

This is basically your second option with an additional Id field (which isn't necessary, but use Id fields on almost every single table out of practice).

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜