在MariaDB服务器中添加外键
问题描述:
使用expressJS mysql模块,我能够创建简单的数据库表。但是,当我尝试使用添加约束外键的alter table时,出现语法错误。在MariaDB服务器中添加外键
的script.js
connection.query('\
ALTER TABLE `' + dbconfig.database + '`.`' + dbconfig.menu_table + '` (\
ADD CONSTRAINT `caterer_fk` FOREIGN KEY (`item_id`) REFERENCES `mydatabase.item` (`id`) \
)');
错误
Error: ER_PARSE_ERROR: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '( ADD CONSTRAINT caterer_fk FOREIGN KEY (
item_id
) REFERENCESmydatabase.item
' at line 1
答
这是
ALTER TABLE db.table ADD CONSTRAINT whatever
不
ALTER TABLE db.table (ADD CONSTRAINT whatever) /* wrong! */
MySQL和MariaDB语法错误消息显示从解析器不理解的第一个字符开始的语句。