gpt4 book ai didi

mysql - 创建 FOREIGN KEY 时出错

转载 作者:行者123 更新时间:2023-11-30 00:32:25 25 4
gpt4 key购买 nike

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 行

最佳答案

在创建表语句后放置分号:

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
);

关于mysql - 创建 FOREIGN KEY 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22412580/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com