开发者

Is it alright to create foreign keys that are not really foreign keys in a database?

开发者 https://www.devze.com 2023-04-04 03:39 出处:网络
I have this mock database: users: userid companies: company_id user_id franchises: franchise_id company_id stores:

I have this mock database:

users:

  • userid

companies:

  • company_id
  • user_id

franchises:

  • franchise_id
  • company_id

stores:

  • store_id
  • franchise_id

These are all primary keys and foreign keys.

What I want to know if it is bad practice to do things like put user_id in the franchises table.

That way I can do report开发者_JAVA百科ing without all the relations and also in case something "gets lost" I can find out where it goes. They aren't foreign keys defined in the database but they look like foreign keys. Or should I make them foreign keys?

Thank you for your input.

edit: I also thought that denormalizing like I was thinking could possibly help speed things up, not having to use all the joins.


What you are talking about is a planned data denormalization. You might be better off creating views for reporting which access the tables. That way you don't have to alter your tables and their integrity (or store redundancy), but you can still use the associations in reporting.


I see what you are doing. To me, what you have currently is perfectly fine; you don't need to add an extra field 'user_id' to franchise. Since a user has a relationship to a company, and a company has a relationship to a franchise; a user does have a connection to the franchise.

You may want to create a View to show everything instead of adding an extra field to a table.

0

精彩评论

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

关注公众号