外键约束 创建不了
为什么会有这个错误
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) );
8
收起
正在回答
1回答
同学你好,使用外键约束时,被关联的字段必须使用唯一约束,即使用“unique”或者“primary key”进行约束。
所以在“node_id”字段的约束中加入“unique”即可解决。
祝学习愉快~
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星