2
我很奇怪,爲什麼我不能添加該外國keys.This可以是我的架構多個外鍵不能被添加
CREATE TABLE members(
member_id INT NOT NULL AUTO_INCREMENT,
name VARCHAR(50) NOT NULL,
num_1 int,
num_2 int,
password VARCHAR(50) NOT NULL,
PRIMARY KEY (member_id)
)ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE contacts(
contact_id INT NOT NULL AUTO_INCREMENT,
s1 int,
phone_number VARCHAR(10) NOT NULL,
s2 int,
s3 int,
PRIMARY KEY (contact_id),
FOREIGN KEY (s1) REFERENCES members(num_1) ON DELETE CASCADE ON UPDATE CASCADE,
FOREIGN KEY (s2) REFERENCES members(num_2) ON DELETE CASCADE ON UPDATE CASCADE,
FOREIGN KEY (s3) REFERENCES members(member_id) ON DELETE CASCADE ON UPDATE CASCADE
)ENGINE=InnoDB DEFAULT CHARSET=utf8
我得到了MySQL終端上這個錯誤 ERROR 1215 (HY000): Cannot add foreign key constraint
有沒有我的模式有問題嗎?
你可能沒有'num_1'和'num_2'上的索引。可能是原因 –
通常外鍵需要引用表中的主鍵 –
@JanneMatikainen我認爲唯一鍵 – chetan