SQL - 只能使用INSERT添加空值

时间:2013-04-29 07:25:20

标签: php mysql sql database

我目前正在寻找将虚拟信息加载到我创建的数据库中。

这是表格之一:

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,
Email               VARCHAR(30) NOT NULL,
Telephone           INTEGER(15) NOT NULL,
Date_employed           DATE NOT NULL,
Salary              DECIMAL NOT NULL,
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;

这是我提交数据的唯一方法:

INSERT INTO Staff VALUES 
(null, null, 'Sean', 'D', 'UP62@mac.uk', '07411557877', '2012-10-08', 80000.00,null,null,null);

如果我尝试将int值改为:我得到:

INSERT INTO Staff VALUES 
(null,1, 'Luke', 'Clifford', 'UP624715@myport.ac.uk', '07411557077', '2012-10-08', 80000.00,1,1,1);

错误:

 #1452 - Cannot add or update a child row: a foreign key constraint fails (`happy_homes`.`staff`, CONSTRAINT `staff_ibfk_1` FOREIGN KEY
(`Local_ID`) REFERENCES `localoffice` (`Local_ID`))

任何人都可以提供帮助

1 个答案:

答案 0 :(得分:1)

LocalOffice表中插入第一个数据之后,查询将起作用。在插入LocalOffice表之前,整数值应该出现在Staff表中。

它清楚地说它是外键

FOREIGN KEY (Local_ID)      REFERENCES LocalOffice(Local_ID)