简体   繁体   English

关于使用外键进行数据库完整性的建议

[英]Suggestion for Database Integrity with Foreign Keys

I try to create a database design to suit my needs. 我尝试创建适合我需要的数据库设计。

I would really appreciate any of your recommendation for my following table design: 对于以下表格设计,我非常感谢您的任何建议:

在此处输入图片说明

I have two components: Categories and Content . 我有两个组成部分: CategoriesContent

Whenever I create a new item for any of those two components, a new route item should be created also, with relations like shown on visual diagram. 每当我为这两个组件中的任何一个创建新项目时,也应该创建一个新的路由项目,其关系类似于可视化图中所示。

Red and Blue highlights are identical with their colors. 红色和蓝色突出显示的颜色相同。

Is it good practice to have the following columns in those tables like ci_routes.refid , ci_categories.slug and ci_content.slug ? 在这些表中包含ci_routes.refidci_categories.slugci_content.slug这样的表是否是一种好习惯?

==== UPDATED ==== ====更新====

The business logic for my app works like this: 我的应用程序的业务逻辑如下所示:

  • I create a category item with html form 我用html表单创建一个类别项目
  • On form submit, app will create a new route to return a newly created ci_routes.id 在提交表单时,应用程序将创建一条新路线以返回新创建的ci_routes.id
  • Category will be created and filled with all info including ci_categories.rid . 将创建类别并填充所有信息,包括ci_categories.rid
  • Then it updates yet again the ci_routes with the all information where the ci_routes.slug is identical to ci_categories.slug and ci_routes.route will be based on configured setting something like $path/$category_id where $path = 'category' 然后它将再次使用所有信息更新ci_routes ,其中ci_routes.slugci_categories.slugci_routes.route相同,将基于已配置的设置,例如$path/$category_id ,其中$path = 'category'

Whenever I decide to change $path to something like $path = 'node' , all my routes should be purged and recreated based on new settings. 每当我决定将$path更改$path $path = 'node' ,我的所有路由都应根据新设置清除并重新创建。

Pls correct me If I made some logical mistake somewhere in my logic 请纠正我,如果我在逻辑中的某个地方犯了一些逻辑错误

Is it good practice to have the following columns in those tables ci_routes.refid 在这些表ci_routes.refid中包含以下列是否是一种好习惯

No. In the current design ci_routes.refid will be a column referencing two tables, you can not have a foreign key to more than a table on a column in RDBMS, so you have to handle it in your application logic, that would be a buggy, hard work. 否。在当前设计中ci_routes.refid将是引用两个表的列,您不能在RDBMS的列上拥有超过一个表的外键,因此必须在应用程序逻辑中进行处理,这将是越野车,努力工作。 Also you will loose data integrity in database level, that is not appreciated. 另外,您将失去数据库级别的数据完整性,这是不被赞赏的。

Solution 1 : 解决方案1
Have two separate columns in ci_routes to reference ci_categories and ci_content like ci_routes.content_id and ci_routes.categories_id ci_routes有两个单独的列来引用ci_categoriesci_content例如ci_routes.content_idci_routes.categories_id

Solution 2 : 解决方案2
Use inheritance by having a base table for components and categories, containing shared properties of these two entities, having the base table you can refer it inside ci_routes as the foreign key. 通过具有组件和类别的基表来使用继承 ,该基表包含这两个实体的共享属性,并具有基表,您可以在ci_routes中将其引用为外键。

在此处输入图片说明

Is it good practice to have the following columns in those tables like ci_categories.slug and ci_content.slug ? 在这些表中包含ci_categories.slug和ci_content.slug之类的以下列是一种好习惯吗?

Answering the question needs more information about your business logic. 回答问题需要有关您的业务逻辑的更多信息。
BTW it seams you are going to de-normalize your model by redundant slug column. 顺便说一句,它表明您将通过冗余的slug柱对模型进行归一化。 If you are going to do this just for performance reason then think about when a components slug column is updating, you will need to update dependent rout row(s). 如果您仅出于性能原因而执行此操作,请考虑何时更新组件段子列,则需要更新相关的路由行。 I won't do that until having specific evidences. 在没有具体证据之前,我不会这样做。

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

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