我正在尝试向表中添加一列并将其设为外键,但我不断收到此错误:
ERROR 1064 (42000): 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 'FOREIGN KEY(idplayer) REFERENCES players(playersid)' at line 1
下面是我在更改语句之前的代码:
CREATE TABLE transactions
(
transid INT UNSIGNED NOT NULL AUTO_INCREMENT,
type VARCHAR(20),
fromteam VARCHAR(30),
toteam VARCHAR(30),
idplayer INT UNSIGNED NOT NULL,
PRIMARY KEY(transid)
);
现在我尝试更改 idplayer 并将其设为外键:
ALTER TABLE transactions
MODIFY idplayer INT UNSIGNED NOT NULL
FOREIGN KEY(idplayer) REFERENCES players(playersid)
请提供帮助。
最佳答案
首先,您在NOT NULL
后面缺少一个逗号,其次您需要告诉mysql添加CONSTRAINT
。尝试一下:
ALTER TABLE `events`
MODIFY idplayer INT UNSIGNED NOT NULL,
ADD CONSTRAINT `FK_Name` FOREIGN KEY (idplayer) REFERENCES players(playersid);
关于mysql - 尝试修改 MySQL 中的表时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26763880/