2009-12-17 89 views
1

我想設計一個數據庫,但我需要一些關係的幫助。我的桌子設計是否正確?數據庫設計問題

這裏是數據庫想法..

用戶將提交一份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 STATUS;顯示:

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中的關鍵(索引),雖然。我不確定這是否是確切的問題在這裏..

回答

0

您的查詢是非常混亂,例如step_id varchar(25)需要是一個int。

爲什麼不使用gui programm或者好的舊phpMyAdmin,所以你可以從他們正在創建的Querys中學習,phpMyAdmin還有一個先進的功能調用「設計器」來創建約束。

+0

每個HOWTO的步驟可以從0,1,2開始排序...但這在howtostep表中並不是唯一的。因此我正在考慮將step_id生成爲唯一的varchar,並且具有與其關聯的step_number。 – perlwle 2009-12-18 00:27:47

0

如果我正確地閱讀這篇文章,您的HowToComment id是HowtoStepImage的外鍵。每個評論都必須有圖片嗎?看起來像一隻雞和雞蛋的問題。從你的問題描述看來,圖像鏈接到評論,而不是相反。

+0

是的。這是一個錯誤。 HowtoStepImage中的image_id應該默認爲null。 現在想了一想,我應該有一個單獨的Howto表,並有UserHowtoComment的howto_id鏈接。 – perlwle 2009-12-18 00:32:25

0

你正在陷入MySQL中令人誤解的術語。在關係模型中,密鑰(必然)是不同的。在MySQL中說,它是只是一個索引。您需要主鍵唯一鍵

編輯明確添加上面所暗示的內容:外鍵必須指向關係意義上的鍵。

1

製作3個表格:一個用於HowTo,一個用於HowToStep,一個用於HowToStepImage。

爲每個表格提供明確定義的密鑰,例如,一個數字或一個字符串。 然後讓'child'表引用父表的鍵。 確保列的名稱也是清晰的。

TABLE方法文檔
COLUMNS HowToId(鍵)

TABLE HowToStep
COLUMNS HowToStepId(鍵),HowToId

TABLE HowToStepImage
COLUMNS HowToStepImageId(鍵),HowToStepId

+0

還請記住,外鍵是在「子」表中定義的,指的是「父」表中的索引字段。所以在這個例子中,HowToStep需要一個外鍵給HowTo(HowToId),並且HowToStepImage需要一個外鍵給HowToStep(HowTostepId)。 – 2010-01-27 14:03:24