我很沮喪。我無法爲表Test_info添加外鍵約束:爲什麼不能添加這個外鍵約束?
FOREIGN KEY Test_info(score) REFERENCES Test(score)
它一直給我提供錯誤1215無法添加外鍵約束。我確定數據類型是一樣的,以及名稱等...任何人?
整個SQL表的創建是:
CREATE TABLE IF NOT EXISTS Employees (
ssn VARCHAR(20),
union_mem_no VARCHAR(20),
PRIMARY KEY (ssn)
) ENGINE=INNODB;
CREATE TABLE IF NOT EXISTS Traffic_Control (
ssn VARCHAR(20),
exam_date DATE,
PRIMARY KEY (ssn),
FOREIGN KEY (ssn) REFERENCES Employees(ssn) ON DELETE CASCADE
) ENGINE=INNODB;
CREATE TABLE IF NOT EXISTS Technician (
ssn VARCHAR(20),
T_name VARCHAR(20),
phone_num VARCHAR(20),
address VARCHAR(50),
Salary INT,
PRIMARY KEY (ssn),
FOREIGN KEY (ssn) REFERENCES Employees(ssn) ON DELETE CASCADE
) ENGINE=INNODB;
CREATE TABLE IF NOT EXISTS Model (
model_no VARCHAR(20),
Capacity INT,
Weight INT,
PRIMARY KEY (model_no)
) ENGINE=INNODB;
CREATE TABLE IF NOT EXISTS Expert (
ssn VARCHAR(20),
model_no VARCHAR(20),
FOREIGN KEY (ssn) REFERENCES Technician(ssn) ON DELETE CASCADE,
FOREIGN KEY (model_no) REFERENCES Model(model_no) ON DELETE CASCADE
) ENGINE=INNODB;
CREATE TABLE IF NOT EXISTS Plane (
reg_no VARCHAR(20),
PRIMARY KEY (reg_no)
) ENGINE=INNODB;
CREATE TABLE IF NOT EXISTS Type (
reg_no VARCHAR(20),
model_no VARCHAR(20),
FOREIGN KEY (reg_no) REFERENCES Plane(reg_no),
FOREIGN KEY (model_no) REFERENCES Model(model_no)
) ENGINE=INNODB;
CREATE TABLE IF NOT EXISTS Test (
FAA_no VARCHAR(20),
T_name VARCHAR(20),
score INT,
PRIMARY KEY (FAA_no)
) ENGINE=INNODB;
CREATE TABLE IF NOT EXISTS Test_info (
ssn VARCHAR(20),
FAA_no VARCHAR(20),
T_date DATE NOT NULL,
hours INT,
score INT,
PRIMARY KEY (ssn, FAA_no),
FOREIGN KEY (ssn) REFERENCES Employees(ssn) ON DELETE CASCADE,
FOREIGN KEY (FAA_no) REFERENCES Test(FAA_no),
FOREIGN KEY (score) REFERENCES Test(score)
) ENGINE=INNODB;
雖然不是「法律」,FK到PK是常態,無論它是什麼必須是唯一的。不知道你打算爲這個FK。這裏有什麼想法? – LoztInSpace
@LoztInSpace我的印象是外鍵簡單地連接了兩個表中的數據。我應該更清楚地知道,外鍵仍然是一個關鍵。我的錯。感謝您的洞察力。 – noobcoder