简体   繁体   中英

How to reference the actual value in a one to many relation

I have two tables, pirates and bounties .

Pirate Bounties
id id
name value
. pirate_id

One pirate can have a lot of bounties, but one bounty can have only one pirate. The issue is, I need to know what is the actual bounty, since I need to keep track of all previous bounties.

My solution was to create a cyclical reference with a deferent constraint FK , so my tables would be like this:

Pirate Bounties
id id
name value
bounty pirate_id

Or I could add one column to check if that bounty is the actual one:

Pirate Bounties
id id
name value
. pirate_id
. is_actual

What would be the best approach? Any suggestions?

I would create a table that matches a pirate ID and a bounty ID. This way you can change the rules over time (one pirate with several bounties or one bounty for several pirates, your choice). You gain more flexibility over time.

Especially useful if a bounty contains a lot of data that you don't want to duplicate when another pirate takes the same bounty aftewards. You will only create a row in the linkage table with two IDs.

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