插入语句错误

时间:2014-04-29 05:25:31

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

我的表dbo.Students_Old包含列:

  

名称(varchar),DocNum(int),组(varchar),表单(varchar)。

它没有主键,我需要规范化这个表。所以我要制作三张桌子:

  

dbo.Students(StudentId(int primary key),Name(varchar),Group(int fk),   表格(int fk),

     

dbo.Forms(FormId(int pk),FName(varchar)),dbo.Groups(GroupId(int pk),   的gname(VARCHAR))。

此外,我还需要使用dbo.Students_Old中的数据填充所有表格。我的代码是:

CREATE TABLE dbo.Groups(GroupId int IDENTITY(1,1) PRIMARY KEY, GName nvarchar(10));

GO

INSERT INTO dbo.Groups(GName) 
  select Group
  from dbo.Students_Old
  group by Group

GO

CREATE TABLE dbo.Forms(FormId int IDENTITY(1,1) PRIMARY KEY, Form nvarchar(20));

INSERT INTO dbo.Forms(Form) 
select Form
from dbo.Students_Old
group by Form

GO

CREATE TABLE dbo.Students (StudentId int PRIMARY KEY, Name nvarchar(50),
Form int NOT NULL,
Group int NOT NULL,
 CONSTRAINT Form FOREIGN KEY(StudentId) REFERENCES dbo.Forms(FormId),
 CONSTRAINT Grup FOREIGN KEY(StudentId) REFERENCES dbo.Groups(GroupId));

GO

INSERT INTO dbo.Students(StudentId, Name, Form, Group) 
select DocNum, Name, f.FormId, g.GroupId 
from dbo.Students_Old s
join dbo.Forms f on s.Form=f.Form
join dbo.Groups g on s.Group=g.GName

Students_Old.DocNum也是独一无二的。

表正常创建,但在插入语句中我有一个错误:

The INSERT statement conflicted with the FOREIGN KEY constraint "Form". The conflict occurred in database "DBNames", table "dbo.Forms", column 'FormId'.

请帮帮我。

3 个答案:

答案 0 :(得分:1)

不确定是否是这种情况,因为您收到了FOREIGN KEY错误,但请尝试避免使用GROUP或其他保留字等列名。虽然您可以避免表创建步骤出错,但在修改/更新此类表时可能会遇到严重问题。

答案 1 :(得分:1)

在新数据库上执行以下查询

CREATE TABLE dbo.Groups(GroupId int IDENTITY(1,1) PRIMARY KEY, GName nvarchar(10));

GO

INSERT INTO dbo.Groups(GName) 
  select Group
  from dbo.Students_Old
  group by Group

GO

CREATE TABLE dbo.Forms(FormId int IDENTITY(1,1) PRIMARY KEY, Form nvarchar(20));

INSERT INTO dbo.Forms(Form) 
select Form
from dbo.Students_Old
group by Form

GO

CREATE TABLE dbo.Students (StudentId int PRIMARY KEY, Name nvarchar(50),
Form int NOT NULL,
[Group] int NOT NULL,
 CONSTRAINT Form FOREIGN KEY(Form) REFERENCES dbo.Forms(FormId),
 CONSTRAINT Grup FOREIGN KEY(Group) REFERENCES dbo.Groups(GroupId));

GO

INSERT INTO dbo.Students(StudentId, Name, Form, Group) 
select DocNum, Name, f.FormId, g.GroupId 
from dbo.Students_Old s
join dbo.Forms f on s.Form=f.Form
join dbo.Groups g on s.Group=g.GName

我已改为以下行

 CONSTRAINT Form FOREIGN KEY(Form) REFERENCES dbo.Forms(FormId),
 CONSTRAINT Grup FOREIGN KEY([Group]) REFERENCES dbo.Groups(GroupId));

在您的代码中,外键是在StudentID

上创建的

答案 2 :(得分:0)

执行以下查询。

CREATE TABLE dbo.Groups(GroupId int IDENTITY(1,1) PRIMARY KEY, GName nvarchar(10));

GO

INSERT INTO dbo.Groups(GName) 
  select [Group]
  from dbo.Students_Old
  group by [Group]

GO

CREATE TABLE dbo.Forms(FormId int IDENTITY(1,1) PRIMARY KEY, Form nvarchar(20));

INSERT INTO dbo.Forms(Form) 
select Form
from dbo.Students_Old
group by Form

GO

CREATE TABLE dbo.Students (StudentId int PRIMARY KEY, Name nvarchar(50),
Form int NOT NULL,
[Group] int NOT NULL,
 CONSTRAINT Form FOREIGN KEY(StudentId) REFERENCES dbo.Forms(FormId),
 CONSTRAINT Grup FOREIGN KEY(StudentId) REFERENCES dbo.Groups(GroupId));

GO

INSERT INTO dbo.Students(StudentId, Name, Form, [Group]) 
select DocNum, Name, f.FormId, g.GroupId 
from dbo.Students_Old s
join dbo.Forms f on s.Form=f.Form
join dbo.Groups g on s.[Group]=g.GName