创建外键的正确语法
我使用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
I use MySql 5.5.8 and I can't find out how should I write which field need to be a foreign key refererece. For example:
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;
And I get an error message:
#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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用
ie 外键字段应该在括号中
use
i.e. the foreign key field should be in brackets