基本上,问题是我是应该在每个使用分层数据的表中使用相同的列,还是应该让一个包含这些列的表来处理所有类型数据的分层数据。
我的数据库存储不同类型的分层数据:页面、问题等。下面给出的是questions数据库。
CREATE TABLE `hp_questions` (
`client_id` int(4) unsigned NOT NULL,
`id` int(4) unsigned NOT NULL AUTO_INCREMENT,
`root_id` int(4) unsigned NOT NULL,
`parent_id` int(4) unsigned NOT NULL,
`depth` int(4) unsigned NOT NULL DEFAULT '0',
`position` int(4) unsigned NOT NULL DEFAULT '0',
`absolute_position` int(4) unsigned NOT NULL DEFAULT '0',
`name` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`slug` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`uri` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`body` longtext COLLATE utf8_unicode_ci,
`last_modified_by_id` int(4) unsigned DEFAULT NULL,
`last_modified_on` int(4) unsigned DEFAULT NULL,
`language` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `client_id` (`client_id`)
) ENGINE=InnoDB AUTO_INCREMENT=738 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;现在,root_id, parent_id, depth, position, absolute_position仅用于分层数据。相同的列有pages、filesystem、templates和permissions表。我想知道是否将它们放到一个表中并添加指示数据类型的额外列type会更正确?
发布于 2011-06-15 04:05:17
“正确”的答案在很大程度上取决于你试图做什么以及数据是如何呈现的,但我会说是的,你的建议是有意义的。如果层次结构是数据组织的核心,那么将其分离出来是有意义的,因为它可以规范化您的数据。
这样做将要求您在代码中添加额外的逻辑,以确保您根据所查看的内容链接到正确的问题/页面/文件系统/等表。
https://stackoverflow.com/questions/6349199
复制相似问题