SQL CREATE TABLE - 错误

时间:2013-04-29 06:24:44

标签: php mysql phpmyadmin

我目前正在使用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)(详情......)

2 个答案:

答案 0 :(得分: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;

答案 1 :(得分: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 )
);