2011-09-14 5 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`) 

즉 외래 키 필드 괄호

에 있어야합니다
관련 문제