简体   繁体   中英

Key and Foreign Key Relations

Table 1 I have a table housing all of my dealers and roadshows. I am attempting to add the ability to add an event at these locations. This table has location_id .

To do this I created another table.

Table 2 In this new table I have an id, and an 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.

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 .

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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