gpt4 book ai didi

mysql - 添加外键时在 "REFERENCES"附近出现错误 #1064

转载 作者:行者123 更新时间:2023-11-29 04:08:19 28 4
gpt4 key购买 nike

我在尝试创建外键时不断收到错误 1064:

ALTER TABLE `zajimavost`
ADD CONSTRAINT FK_zaj_bod
FOREIGN KEY def_bod
REFERENCES `bod`(`gid`)
ON UPDATE CASCADE ON DELETE CASCADE;

它提示:

#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 `bod`(`gid`)
ON UPDATE CASCADE ON DELETE CASCADE' at line 4

不管有没有反引号我都试过了,问题总是一样的。

我的表:

CREATE TABLE `zajimavost` (
`zaj_id` int(11) NOT NULL,
`lok_id` int(11) NOT NULL,
`nazev` varchar(255) COLLATE utf8_czech_ci NOT NULL,
`kategorie` int(11) NOT NULL DEFAULT '1',
`datace_od` int(11) NOT NULL DEFAULT '31',
`datace_do` int(11) NOT NULL DEFAULT '60',
`def_bod` int(11) DEFAULT NULL,
PRIMARY KEY (`zaj_id`),
KEY `ck_zaj_lok` (`lok_id`),
KEY `zaj_id` (`zaj_id`),
KEY `FK_zaj_kat` (`kategorie`),
KEY `FK_zaj_bod` (`def_bod`),
CONSTRAINT `FK_zaj_kat` FOREIGN KEY (`kategorie`) REFERENCES `kategorie` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `fk_zaj_lok` FOREIGN KEY (`lok_id`) REFERENCES `lokalita` (`lok_id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_czech_ci

CREATE TABLE `bod` (
`gid` int(11) NOT NULL AUTO_INCREMENT,
`geom` point DEFAULT NULL,
`radius` int(4) NOT NULL DEFAULT '50',
PRIMARY KEY (`gid`)
) ENGINE=InnoDB AUTO_INCREMENT=30 DEFAULT CHARSET=utf8 COLLATE=utf8_czech_ci

表中有数据。

那么怎么解决呢?

最佳答案

添加外键时需要在子表列名中使用(),试试这个

ALTER TABLE zajimavost 
ADD CONSTRAINT FK_zaj_bod
FOREIGN KEY (def_bod) REFERENCES `bod`(`gid`)
ON UPDATE CASCADE ON DELETE CASCADE;

关于mysql - 添加外键时在 "REFERENCES"附近出现错误 #1064,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21552503/

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