【发布时间】:2014-03-14 18:04:05
【问题描述】:
CREATE TABLE IF NOT EXISTS `servergraph_server` (
`id` INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
`name` VARCHAR(64) NOT NULL,
`ip` VARCHAR(16) NOT NULL,
`port` SMALLINT UNSIGNED NOT NULL
)
CREATE TABLE IF NOT EXISTS `servergraph_data` (
`id` INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
`serverid` INT NOT NULL,
`time` TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL,
`fps` SMALLINT UNSIGNED NOT NULL,
`replay` TINYINT UNSIGNED NOT NULL,
`dropped_packets` INT UNSIGNED NOT NULL,
`online` TINYINT UNSIGNED NOT NULL,
`clients0` TINYINT UNSIGNED NOT NULL,
`clients1` TINYINT UNSIGNED NOT NULL,
`clients2` TINYINT UNSIGNED NOT NULL,
`clients3` TINYINT UNSIGNED NOT NULL,
FOREIGN KEY (`serverid`) REFERENCES `servergraph_server` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
)
这是我要创建的表。
但我总是在 FOREIGN KEY 行上遇到错误。
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 'FOREIG' at line 1
我正在使用这个版本的 MySQL:5.5.35-0+wheezy1
怎么了?
谢谢你,弗洛贝
解决方案:
ALTER TABLE `servergraph_data` ADD CONSTRAINT `fk_server` FOREIGN KEY (`serverid`) REFERENCES `servergraph_server` (`id`)
而不是 CREATE TABLE servergraph_data 中的 FOREIGN KEY 行
【问题讨论】:
-
您是否将其作为脚本执行?
-
不,我从插件内部执行它们,但它支持 MySQL
-
您的 DDL 有效:sqlfiddle.com/#!2/8702e 正在发生其他事情。
标签: mysql sql reference foreign-keys create-table