简体   繁体   English

关键和外键关系

[英]Key and Foreign Key Relations

Table 1 I have a table housing all of my dealers and roadshows. 表1我有一张桌子,上面放着我所有的经销商和路演。 I am attempting to add the ability to add an event at these locations. 我正在尝试添加在这些位置添加事件的功能。 This table has location_id . 该表具有location_id

To do this I created another table. 为此,我创建了另一个表。

Table 2 In this new table I have an id, and an event_location_id . 表2在这个新表中,我有一个ID和一个event_location_id

Which way do I make the relationship? 我该如何建立关系?

I want to be able to daisy chain from Table1.Table2.count() in asp.net MVC. 我希望能够从asp.net MVC中的Table1.Table2.count()菊花链。

In which table do I make the foreign key relationship? 我在哪个表中建立外键关系?

Assuming that location_id is the primary key of Table 1 you would want to add a foreign key to Table 2 on the event_location_id that references location_id of Table 1 . 假设location_id表1的主键,你会想要一个外键添加到表2event_location_id引用location_id 表1中。

Having said that, a table that houses dealers and roadshows doesn't sound like it's been designed correctly. 话虽如此,容纳经销商路演的桌子听起来好像设计不正确。 It seems like it would make more sense to have a dealers table, a roadshows table, a relationship between them, and events that hang off of one or the other. 似乎有一个经销商表,一个路演表,它们之间的关系以及彼此悬而未决的事件会更有意义。

In a one-to-many relationship, the primary key is held in the 'one' table, and the foreign key is held in the 'many' table. 在一对多关系中,主键保存在“一个”表中,外键保存在“许多”表中。 So if a location can have many events, you would have a column in the Event table called LocationId which would map to the location_id primary key of the Location table. 因此,如果一个位置可以有许多事件,则Event表中将有一个名为LocationId的列,该列将映射到Location表的location_id主键。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM