0

我目前正在使用 phpmyadmin 创建许多不同的表,但这似乎引起了问题。这可能是这部分代码的错误,或者可能是在我的代码中进一步引用了 Staff 的错误?

SQL query:

CREATE TABLE Staff(

Staff_ID INTEGER( 5 ) PRIMARY KEY AUTO_INCREMENT ,
Local_ID INTEGER( 5 ) ,
First_name VARCHAR( 20 ) NOT NULL ,
Last_name VARCHAR( 20 ) NOT NULL ,
Address_line_1 VARCHAR( 30 ) NOT NULL ,
Address_line_2 VARCHAR( 30 ) NOT NULL ,
City VARCHAR( 20 ) NOT NULL ,
Post_Code VARCHAR( 8 ) NOT NULL ,
Email VARCHAR( 30 ) NOT NULL ,
Telephone INTEGER NOT NULL ,
Date_employed DATE,
Salary DECIMAL,
Sales_ID INTEGER( 5 ) ,
Manager_ID INTEGER( 5 ) ,
Development_ID INTEGER( 5 ) ,
FOREIGN KEY ( Local_ID ) REFERENCES LocalOffice( Local_ID ) ,
FOREIGN KEY ( Sales_ID ) REFERENCES Sales_Advisors( Sales_ID ) ,
FOREIGN KEY ( Manager_ID ) REFERENCES Site_Manager( Manager_ID ) ,
FOREIGN KEY ( Development_ID ) REFERENCES Development( Development_ID )
);

MySQL 说:

1005 - 无法创建表“h_h.staff”(错误号:150)(详细信息...)

4

2 回答 2

2

从错误来看,很明显这个错误是由于外键约束造成的。您可以尝试禁用外键约束,然后创建您的表并最终启用它吗?

SET foreign_key_checks = 0;

CREATE TABLE Staff(

Staff_ID INTEGER( 5 ) PRIMARY KEY AUTO_INCREMENT ,
Local_ID INTEGER( 5 ) ,
First_name VARCHAR( 20 ) NOT NULL ,
Last_name VARCHAR( 20 ) NOT NULL ,
Address_line_1 VARCHAR( 30 ) NOT NULL ,
Address_line_2 VARCHAR( 30 ) NOT NULL ,
City VARCHAR( 20 ) NOT NULL ,
Post_Code VARCHAR( 8 ) NOT NULL ,
Email VARCHAR( 30 ) NOT NULL ,
Telephone INTEGER NOT NULL ,
Date_employed DATE,
Salary DECIMAL,
Sales_ID INTEGER( 5 ) ,
Manager_ID INTEGER( 5 ) ,
Development_ID INTEGER( 5 ) ,
FOREIGN KEY ( Local_ID ) REFERENCES LocalOffice( Local_ID ) ,
FOREIGN KEY ( Sales_ID ) REFERENCES Sales_Advisors( Sales_ID ) ,
FOREIGN KEY ( Manager_ID ) REFERENCES Site_Manager( Manager_ID ) ,
FOREIGN KEY ( Development_ID ) REFERENCES Development( Development_ID )
);

SET foreign_key_checks = 1;
于 2013-04-29T06:47:31.780 回答
0

我认为你应该像这样在外键上创建索引

...

Development_ID INTEGER( 5 ) ,
INDEX Local_ID,
INDEX Sales_ID,
INDEX Manager_ID,
INDEX DEvelopment_ID,
FOREIGN KEY ( Local_ID ) REFERENCES LocalOffice( Local_ID ) ,
FOREIGN KEY ( Sales_ID ) REFERENCES Sales_Advisors( Sales_ID ) ,
FOREIGN KEY ( Manager_ID ) REFERENCES Site_Manager( Manager_ID ) ,
FOREIGN KEY ( Development_ID ) REFERENCES Development( Development_ID )
);
于 2013-04-29T06:42:08.933 回答