数据库设计问题

时间:2009-12-17 16:21:46

标签: mysql foreign-key-relationship

我正在尝试设计一个数据库,但我需要一些关系的帮助。我的桌子设计是否正确?

这是数据库的想法..

用户将提交一个howto,每个howto将有一个或多个步骤(一对多)。每个步骤可以有与(另一个到多个)相关的随机图片。所以我在想这个:

CREATE TABLE `HowtoStepImage`  
  `id` int(10) unsigned NOT NULL auto_increment,  
  `user_id` int(10) unsigned NOT NULL,  
  `howto_id` varchar(25) NOT NULL,  
  `step_id` varchar(25) NOT NULL,  
  `img_id` int(10) unsigned NOT NULL,  
  PRIMARY KEY  (`id`),  
  KEY `hsi_k_1` (`howto_id`),  
  CONSTRAINT `hsi_ibfk_1` FOREIGN KEY (`user_id`) REFERENCES `user` (`id`),  
  CONSTRAINT `hsi_ibfk_2` FOREIGN KEY (`step_id`) REFERENCES `HowtoStep` (`step_id`),  
  CONSTRAINT `hsi_ibfk_3` FOREIGN KEY (`img_id`) REFERENCES `StepImage` (`id`)  
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

table HowtoStep  
step_id, title, content, created  
primary key (step_id)

table StepImage  
img_id, filename, created


CREATE TABLE `UserHowtoComment` (  
  `id` int(10) unsigned NOT NULL auto_increment,  
  `howto_id` varchar(25) NOT NULL,  
  `user_id` int(10) unsigned NOT NULL,  
  `comment` varchar(500) NOT NULL,  
  `created` timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP,  
  PRIMARY KEY  (`id`),  
  KEY `UserHowtoComment_ibfk_1` (`howto_id`),  
  KEY `UserHowtoComment_ibfk_2` (`user_id`),  
  CONSTRAINT `UserHowtoComment_ibfk_1` FOREIGN KEY (`howto_id`) REFERENCES `HowtoStepImage` (`howto_id`),  
  CONSTRAINT `UserHowtoComment_ibfk_2` FOREIGN KEY (`user_id`) REFERENCES `user` (`id`)  
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

然而,我在创建表时遇到错误,我确信这是由于我的数据库设计。这是什么mysql> SHOW ENGINE INNODB状态;所示:

091217  9:59:59 Error in foreign key constraint of table UserhowtoComment:
 FOREIGN KEY (`howto_id`) REFERENCES `howtoStepImage` (`howto_id`),
  CONSTRAINT `UserHowtoComment_ibfk_2` FOREIGN KEY (`user_id`) REFERENCES `user` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8:
Cannot find an index in the referenced table where the
referenced columns appear as the first columns, or column types
in the table and the referenced table do not match for constraint.
Note that the internal storage type of ENUM and SET changed in
tables created with >= InnoDB-4.1.12, and such columns in old tables
cannot be referenced by such columns in new tables.
See http://dev.mysql.com/doc/refman/5.0/en/innodb-foreign-key-constraints.html
for correct foreign key definition.

howto_id是UserHowtoComment中的键(索引)。我不确定这是不是确切的问题..

4 个答案:

答案 0 :(得分:1)

制作3个表:一个用于HowTo,一个用于HowToStep,一个用于HowToStepImage。

为每个表格提供明确定义的密钥,例如数字或字符串。 然后让'child'表引用父表的键。 确保列也有明确的名称。

表格如何 COLUMNS HowToId(关键)

表HowToStep
COLUMNS HowToStepId(key),HowToId

表HowToStepImage
COLUMNS HowToStepImageId(key),HowToStepId

答案 1 :(得分:0)

您的查询非常混乱,例如step_id varchar(25)需要是一个int。

为什么你不只是使用gui程序或者可能是好的旧phpMyAdmin,所以你可以从他们正在创建的Querys中学习,phpMyAdmin也有一个高级功能调用“Designer”来创建约束。

答案 2 :(得分:0)

如果我读得正确,你的HowToComment id是HowtoStepImage的外键。每个评论都必须有图像吗?看起来像鸡和鸡蛋问题。从你的问题描述来看,似乎图像链接到评论,而不是相反。

答案 3 :(得分:0)

你正在成为MySQL中误导性术语的牺牲品。在关系模型中, key (必然)是不同的。在MySQL中,它只是一个索引。你需要 PRIMARY KEY UNIQUE KEY

编辑以明确添加上面隐含的内容:外键必须指向关系意义上的键。

相关问题