简体   繁体   English

是否应该对所有表都使用用户的外键?

[英]Should I use the foreign key of user for all tables?

I have four tables so far. 到目前为止,我有四张桌子。 They are named: 它们被命名为:

user, gang, gang_business, gang_members, gang_weapons 用户,帮派,帮派业务,帮派成员,帮派武器

I am trying to figure out if I should use the primary key of either the "user" or "gang" table as foreign keys for the "gang_business", "gang_members", and "gang_weapons" table. 我试图弄清楚是否应该使用“ user”或“ gang”表的主键作为“ gang_business”,“ gang_members”和“ gang_weapons”表的外键。 The "gang" table has the user's primary key as a foreign key. “帮派”表将用户的主键作为外键。 I am trying to figure out which is the best route to take. 我正在尝试找出哪条最佳路线。 Please give me your best advice and feedback. 请给我您最好的建议和反馈。 Thanks. 谢谢。

  • user table: (user_id, user_name, user_email, user_password) 用户表:(用户ID,用户名,用户电子邮件,用户密码)
  • gang table: (gang_id, user_id, gang_name, gang_color, gang_idelogy) 帮派表:(帮派ID,用户ID,帮派名称,帮派颜色,帮派意识形态)
  • gang_business:(business_id, user_id and/or gang_id, bar, restaurant, gang_business:(business_id,user_id和/或gang_id,酒吧,餐厅,
    casino, etc) 赌场等)

It depends on the relations within the users and the gangs tables. 这取决于用户和帮派表之间的关系。

I would do it with this tables: 我会用这张桌子做的:

user: (id, name, email, password) 用户:(ID,名称,电子邮件,密码)

gang: (id, name, color, idelogy) 帮派:(身份证,姓名,肤色,意识形态)

gang_members : (user_id, gang_id) gang_members:(user_id,gang_id)

user_weapons: (user_id, weapon_id) user_weapons :(用户ID,武器ID)

gang_business: (gang_id, business_id) gang_business:(gang_id,business_id)

weapons: (id, name, ...) 武器:(编号,名称,...)

business: (id, name, ...) 业务:(编号,名称,...)

I would only prefix columns when they are foreign keys. 当它们是外键时,我只会在它们前面加上前缀。

I suppose weapons belong to users and business belongs to gangs. 我想武器属于用户,企业属于帮派。

If only gang-sters can be a member of gang_business and gang_members, then you should use the PK of the gang table. 如果只有帮派会员可以成为gang_business和gang_members的成员,则应使用帮派表的PK。 You should not use the user PK because that is the super group, of which the gang table is a sub group of. 您不应该使用用户PK,因为那是超级组,帮派表是该超级组的子组。

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

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