交叉表的插入问题?
表结构:
交叉表:
FactoryOfGameID Integer(自动增长列)
PK FactoryID Integer
PK GameID Integer
CreateBy Integer
CreateDate Datetime
表二:
PK GameID Integer YES
GameName Varchar (100)
CreateDate Datetime
表三:
PK FactoryID Integer (自动增长)
FactoryName Varchar (100)
CreateDate Datetime
直接用insert 在交叉表中插入数据时,老提主键重复,插不进去?
插入测试内容如下:
insert into T values(1,3,2333, '2005/11/20 ')
insert into T values(1,2,1133, '2003/7/8 ')
insert into T values(2,3,10089, '2002/6/21 ')
insert into T values(3,3,10089, '2002/6/21 ')
------解决方案--------------------mark
------解决方案--------------------PK FactoryID Integer
PK GameID Integer
insert into T values(1,3,2333, '2005/11/20 ')
insert into T values(1,2,1133, '2003/7/8 ')
FactoryID是PK,你两次都插入“1”,可以么?
------解决方案--------------------FactoryOfGameID Integer(自动增长列)
PK FactoryID Integer (自动增长)
这两个自动增长列是不能有重复的,你的测试数据本身就不对了,存在重复数据
------解决方案--------------------你的主键设置有问题
CREATE TABLE [NetFactoryOfGameDict](
[FactoryOfGameID] [int] IDENTITY(1,1) NOT NULL,
[FactoryID] [int] NOT NULL,
[GameID] [int] NOT NULL,
[CreateBy] [int] NOT NULL,
[CreateDate] [datetime] NOT NULL DEFAULT (getdate()),
PRIMARY KEY CLUSTERED
(
[FactoryID] ASC,
[GameID] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY],
UNIQUE NONCLUSTERED
(
[GameID] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY],
UNIQUE NONCLUSTERED
(
[FactoryID] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
从建表代码可以看到,这个表有一个主键和两个约束,一个是FactoryID和GameID的联合主键,另外两个分别是GameID和FactoryID的唯一约束,所以FactoryID和GameID都不能有重复的.
去掉下面两个约束就好了.