外键约束 创建不了

外键约束 创建不了

为什么会有这个错误

1822 - Failed to add the foreign key constraint. Missing index for constraint 'power_ibfk_1' in the referenced table 'staff'

CREATE TABLE staff(
	employee_id	INT PRIMARY KEY,
	username VARCHAR(20) NOT NULL,
	password VARCHAR(100) NOT NULL,
	salt INT NOT NULL,
	title VARCHAR(50) NOT NULL,
	node_id INT UNSIGNED NOT NULL
);
CREATE TABLE power(
node_id INT UNSIGNED NOT NULL,
node_type INT NOT NULL,
node_name VARCHAR(50) NOT NULL,
url VARCHAR(50),
FOREIGN KEY(node_id) REFERENCES staff(node_id) 
);

https://img1.sycdn.imooc.com//climg/63318fc909a68df217301350.jpg

正在回答

登陆购买课程后可参与讨论,去登陆

1回答

同学你好,使用外键约束时,被关联的字段必须使用唯一约束,即使用“unique”或者“primary key”进行约束。

所以在“node_id”字段的约束中加入“unique”即可解决。

祝学习愉快~

问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
请稍等 ...
意见反馈 帮助中心 APP下载
官方微信

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师