无法创建第二个外键

时间:2015-03-27 00:56:43

标签: mysql database foreign-keys composite-key

以下是我的代码:

if ($employee !== FALSE) {
    echo " ";
} else {
    mysql_query("CREATE TABLE employee (EID SMALLINT NOT NULL AUTO_INCREMENT PRIMARY KEY,
                    fname VARCHAR(20), lname VARCHAR(20), phone VARCHAR(15), email VARCHAR(30))") or die(mysql_error());
    mysql_query("ALTER TABLE employee AUTO_INCREMENT=001");
}
//ticket table creation
if ($ticket !== FALSE) {
    echo " ";
} else {
    mysql_query("CREATE TABLE ticket (TickID SMALLINT NOT NULL AUTO_INCREMENT PRIMARY KEY, EID SMALLINT NOT NULL,
                                    INDEX fk_empID(EID), FOREIGN KEY (EID) REFERENCES employee(EID), time_date VARCHAR(20),
                                    problem LONGTEXT, updates LONGTEXT, location VARCHAR(25), completed VARCHAR(5))ENGINE=INNODB") or die(mysql_error());
}
//ticket assignment intermediary table creation
if ($ticket_assign !== FALSE) {
    echo " ";
} else {
    mysql_query("CREATE TABLE assignment (TechID SMALLINT, TickID SMALLINT, PRIMARY KEY (TechID, TickID),
    INDEX fks_assgn(TickID, TechID), FOREIGN KEY(TechID) REFERENCES ticket(TechID))ENGINE=INNODB") or die(mysql_error());
    mysql_query("ALTER TABLE assignment (ADD CONSTRAINT fk_assgn FOREIGN KEY(TickID) REFERENCES ticket(TickID) ON UPDATE CASCADE ON DELETE CASCADE) ENGINE=INNODB") or die(mysql_error());
}
//technician table creation
if ($technician !== FALSE) {
    echo " ";   
} else {
mysql_query("CREATE TABLE technician (TechID SMALLINT NOT NULL AUTO_INCREMENT PRIMARY KEY,
                    fname VARCHAR(20), lname VARCHAR(20), phone VARCHAR(15), email VARCHAR(30))");
}

我正在尝试将两个外键添加到赋值表中,并且我一直收到一条错误,指出页面“无法添加外键约束”。

我错过了什么?我觉得这很简单,但我会给出任何建议。

提前致谢!

1 个答案:

答案 0 :(得分:0)

我解决了这个问题。事实证明,在制作表格之前,您无法将某些内容设置为外键。

将技术人员表移到分配表上方,错误就消失了。