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

即外键字段应该在括号