MySQL外键错误。约束密钥错误
我无法在现有表中添加外键。错误如下
Executing:
ALTER TABLE `sms`.`student_account`
ADD INDEX `student_id_idx` (`student_id` ASC) VISIBLE;
;
ALTER TABLE `sms`.`student_account`
ADD CONSTRAINT `student_id`
FOREIGN KEY (`student_id`)
REFERENCES `sms`.`student` (`student_id`)
ON DELETE RESTRICT
ON UPDATE CASCADE;
ERROR 1064: 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 '' at line 2
SQL Statement:
ALTER TABLE `sms`.`student_account`
ADD INDEX `student_id_idx` (`student_id` ASC) VISIBLE
I can't add a foreign key to an existing table. The error is as follows
Executing:
ALTER TABLE `sms`.`student_account`
ADD INDEX `student_id_idx` (`student_id` ASC) VISIBLE;
;
ALTER TABLE `sms`.`student_account`
ADD CONSTRAINT `student_id`
FOREIGN KEY (`student_id`)
REFERENCES `sms`.`student` (`student_id`)
ON DELETE RESTRICT
ON UPDATE CASCADE;
ERROR 1064: 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 '' at line 2
SQL Statement:
ALTER TABLE `sms`.`student_account`
ADD INDEX `student_id_idx` (`student_id` ASC) VISIBLE
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我已经写了下面的模式。我建议不要使用相同的名称
student_id
作为已用于列的约束。我希望fk_student_id
避免在某个时候遇到歧义或冲突的风险。db<>>
I've written the schema below which is working. I advise against using the same name
student_id
for the constraint as already used for the column. I would preferfk_student_id
to avoid risk of an ambiguity or conflict at some point.db<>fiddle here