[英]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 . 我有两个组成部分: Categories和Content 。
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.refid
, ci_categories.slug
和ci_content.slug
这样的表是否是一种好习惯?
==== UPDATED ==== ====更新====
The business logic for my app works like this: 我的应用程序的业务逻辑如下所示:
ci_routes.id
ci_routes.id
ci_categories.rid
. ci_categories.rid
。 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.slug
与ci_categories.slug
和ci_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_categories
和ci_content
例如ci_routes.content_id
和ci_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.