尝试正确插入数据

时间:2010-06-25 20:21:39

标签: sql sql-server sql-server-2008

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

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

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

感谢您的帮助

3 个答案:

答案 0 :(得分:2)

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

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

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

第二个错误更简单 - 您的日期格式不正确。它应该是一个字符串。

'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)

答案 1 :(得分:1)

1)Location表中没有给定LocationID的记录

2)您需要引用日期值

答案 2 :(得分:0)

  • 部门和位置之间是否存在FOREIGN KEY约束。如果是这样,那么插入一个deparment需要一个现有的Location。位置通过Location_ID链接到部门

  • 时间戳语法应为字符串'12 / 12/2010',不能没有'-signs