2014-05-07 39 views
0

我要讓3個表所示:什麼是可能使用一個參考場爲多個外鍵約束

wc_groups表

CREATE TABLE IF NOT EXISTS `wc_groups` (
    `id` int(2) unsigned NOT NULL AUTO_INCREMENT, 
    `idgroup` int(7) NOT NULL, 
    `title` varchar(10) NOT NULL, 
    `content` text, 
    `status` smallint(1) NOT NULL DEFAULT '1', 
    PRIMARY KEY (`id`), 
    UNIQUE KEY `idgroup` (`idgroup`), 
    KEY `status` (`status`) 
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ; 

wc_matches表

CREATE TABLE IF NOT EXISTS `wc_matches` (
    `id` int(4) unsigned NOT NULL AUTO_INCREMENT, 
    `time` date NOT NULL, 
    `group_id` int(2) unsigned NOT NULL, 
    `place` varchar(100) COLLATE utf8_unicode_ci NOT NULL, 
    `team_id_1` int(10) unsigned NOT NULL, 
    `team_id_2` int(10) unsigned NOT NULL, 
    `edituser` varchar(50) COLLATE utf8_unicode_ci NOT NULL, 
    PRIMARY KEY (`id`), 
    KEY `group_id_foreign` (`group_id`) 
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=1 ; 

wc_teams表

CREATE TABLE IF NOT EXISTS `wc_teams` (
    `id` int(10) unsigned NOT NULL AUTO_INCREMENT, 
    `title` varchar(255) COLLATE utf8_unicode_ci NOT NULL, 
    `group_id` int(2) unsigned NOT NULL, 
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=1 ; 

ALTER TABLE `wc_teams` 
    ADD CONSTRAINT `group_id_foreign` FOREIGN KEY (`group_id`) REFERENCES `wc_groups` (`id`) ON DELETE CASCADE ON UPDATE CASCADE; 

爲什麼我在執行代碼時遇到錯誤代碼創建表wc_team

什麼是可能的使用什麼是可能的使用一個參考字段(wc_groupsid))對多個外鍵約束?

+0

我想美這樣 結構** wc_matches.group_id-> wc_groups.id wc_teams.group_id-> wc_groups.id ** – user3395037

回答

1

我不明白你的意思與多個外鍵。

但在你wc_teams問題創建查詢是你有失落,背後

`group_id` int(2) unsigned NOT NULL, 

而且還錯失了主鍵,所以我想你需要這個

CREATE TABLE IF NOT EXISTS `wc_teams` (
    `id` int(10) unsigned NOT NULL AUTO_INCREMENT, 
    `title` varchar(255) COLLATE utf8_unicode_ci NOT NULL, 
    `group_id` int(2) unsigned NOT NULL, 
    PRIMARY KEY (`id`) 
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=1 ; 
+0

我想美這樣 01的結構** wc_matches.group_id-> wc_groups.id wc_teams.group_id-> wc_groups.id ** – user3395037

+0

@ user3395037我看到你接受了答案,所以我想現在一切正常?因爲問題不是2個外鍵,而是你的wc_teams表從未被創建過 – melvin