2010-06-25 56 views
2

从模糊棒棒糖收到非常好的修正后,我修改了我的代码,为每行数据创建插入语句。这是我输入的代码:试图正确插入数据

INSERT DEPARTMENTS 
(Department_Id,Department_Name,Manager_Id,Location_Id) 
VALUES 
('D0001','Think Tank',NULL,'L0001') 
GO 

INSERT DEPARTMENTS 
(Department_Id,Department_Name,Manager_Id,Location_Id) 
VALUES 
('D0002','Creators',NULL,'L0002') 
GO 

INSERT DEPARTMENTS 
(Department_Id,Department_Name,Manager_Id,Location_Id) 
VALUES 
('D0003','Marketers',NULL,'L0003') 
GO 

INSERT EMPLOYEES 
(Employee_Id,First_Name,Last_Name,Email,PhoneNumber,Hire_Date,Manager_ID,Department_Id) 
VALUES 
('E0001','Joe','Blow',NULL,NULL,2010/06/25,NULL,NULL) 
GO 

INSERT EMPLOYEES 
(Employee_Id,First_Name,Last_Name,Email,PhoneNumber,Hire_Date,Manager_ID,Department_Id) 
VALUES 
('E0002','John','Doe',NULL,NULL,2010/06/25,NULL,NULL) 
GO 

INSERT EMPLOYEES 
(Employee_Id,First_Name,Last_Name,Email,PhoneNumber,Hire_Date,Manager_ID,Department_Id) 
VALUES 
('E0003','Sue','Happy',NULL,NULL,2010/06/25,NULL,NULL) 
GO 

INSERT EMPLOYEES 
(Employee_Id,First_Name,Last_Name,Email,PhoneNumber,Hire_Date,Manager_ID,Department_Id) 
VALUES 
('E0004','Tina','Turner',NULL,NULL,2010/06/25,NULL,NULL) 
GO 

INSERT EMPLOYEES 
(Employee_Id,First_Name,Last_Name,Email,PhoneNumber,Hire_Date,Manager_ID,Department_Id) 
VALUES 
('E0005','Ike','Turner',NULL,NULL,2010/06/25,NULL,NULL) 
GO 

INSERT EMPLOYEES 
(Employee_Id,First_Name,Last_Name,Email,PhoneNumber,Hire_Date,Manager_ID,Department_Id) 
VALUES 
('E0006','Big','Bird',NULL,NULL,2010/06/25,NULL,NULL) 
GO 

INSERT EMPLOYEES 
(Employee_Id,First_Name,Last_Name,Email,PhoneNumber,Hire_Date,Manager_ID,Department_Id) 
VALUES 
('E0007','Speedy','Gonzales',NULL,NULL,2010/06/25,NULL,NULL) 
GO 

然而,这些是我收到错误消息:

Msg 547, Level 16, State 0, Line 1 
The INSERT statement conflicted with the FOREIGN KEY constraint "FK__DEPARTMEN__Locat__09DE7BCC". The conflict occurred in database "Final_Project", table "dbo.LOCATIONS", column 'Location_ID'. 
The statement has been terminated. 
Msg 547, Level 16, State 0, Line 2 
The INSERT statement conflicted with the FOREIGN KEY constraint "FK__DEPARTMEN__Locat__09DE7BCC". The conflict occurred in database "Final_Project", table "dbo.LOCATIONS", column 'Location_ID'. 
The statement has been terminated. 
Msg 547, Level 16, State 0, Line 2 
The INSERT statement conflicted with the FOREIGN KEY constraint "FK__DEPARTMEN__Locat__09DE7BCC". The conflict occurred in database "Final_Project", table "dbo.LOCATIONS", column 'Location_ID'. 
The statement has been terminated. 
Msg 206, Level 16, State 2, Line 2 
Operand type clash: int is incompatible with date 
Msg 206, Level 16, State 2, Line 2 
Operand type clash: int is incompatible with date 
Msg 206, Level 16, State 2, Line 2 
Operand type clash: int is incompatible with date 
Msg 206, Level 16, State 2, Line 2 
Operand type clash: int is incompatible with date 
Msg 206, Level 16, State 2, Line 2 
Operand type clash: int is incompatible with date 
Msg 206, Level 16, State 2, Line 2 
Operand type clash: int is incompatible with date 
Msg 206, Level 16, State 2, Line 2 
Operand type clash: int is incompatible with date 

我不会没有立即响应解决方案犯同样的错误。我不知道检查绿色复选标记意味着答案是令人满意的。

感谢您的帮助

回答

2

你有两种不同类型的错误。

首先是您违反了外键约束。有三种方法来解决这个问题:

  • 找出正确的密钥应该是什么(例如通过查询LOCATIONS表),并改变你的外键正确的值。
  • 在插入DEPARTMENTS之前,在LOCATIONS表中插入缺失的行。
  • 删除约束(这可能是一个坏主意)。

第二个错误更简单 - 您错误地格式化了日期。它应该是一个字符串。

'2010-06-25' 

完整的查询:

INSERT EMPLOYEES 
(Employee_Id,First_Name,Last_Name,Email,PhoneNumber,Hire_Date,Manager_ID,Department_Id) 
VALUES 
('E0001','Joe','Blow',NULL,NULL,'2010-06-25',NULL,NULL) 
+0

“•删除约束(这可能是一个坏主意)。” 是的,这是阻力最小的路径,几乎总是可以解决问题的最糟糕的事情。总是有一个非常严格的原因,将它们删除,因为它们对您不方便是为了保证数据完整性问题。 – HLGEM 2010-06-25 20:41:43

+0

我对不正确的日期进行了更改。但是,我正在尝试纠正约束错误。 – getbruce 2010-06-26 00:23:22

1

1)有与位置表中给出LocationIDs没有记录

2)你需要引用的日期值

+0

我做了一个改变不正确的日期。但是,我正在尝试纠正约束错误。 – getbruce 2010-06-26 00:19:24

+0

感谢您的帮助 – getbruce 2010-06-26 15:50:08

0
  • 是否有部门和位置之间的外键约束。如果是这样,那么插入一个部件需要一个现有的位置。地点连接到同系LOCATION_ID

  • 时间戳语法应为一个字符串'12/12/2010' 离不开“-signs

+0

我对不正确的日期进行了更改。但是,我正在尝试纠正约束错误。 – getbruce 2010-06-26 00:20:20