2011-09-14 22 views
1

私はMySql 5.5.8を使用しています。どのフィールドを外部キーのレフェレンスにする必要があるのか​​を知ることができません。たとえば、次のように外部キーを作成するための正しい構文

CREATE TABLE IF NOT EXISTS `answers` (
`id` int(11) NOT NULL AUTO_INCREMENT , 
`answer` text COLLATE utf8_polish_ci NOT NULL , 
`user` tinyint(4) DEFAULT NULL , 
`created` timestamp NULL DEFAULT CURRENT_TIMESTAMP , 
`html_template_id` int(11) NOT NULL , 
PRIMARY KEY (`id`) , 
CONSTRAINT html_template_id FOREIGN KEY `html_template_id` REFERENCES `html_templates` (`id`) 
) ENGINE = InnoDB DEFAULT CHARSET = utf8 COLLATE = utf8_polish_ci AUTO_INCREMENT =1; 

そして、私は、エラーメッセージが出ます:

#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'REFERENCES `html_templates`(`id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=' at line 10 

答えて

3

使用を

CONSTRAINT html_template_id FOREIGN KEY (`html_template_id`) REFERENCES `html_templates` (`id`) 

すなわち外部キーフィールドはカッコ

にする必要があります
関連する問題