我对SQL Server 2008数据库表有一个唯一约束,约束包含4列。在输入一些测试数据时,我意外地收到了一个唯一的约束违规。对于四列,我只输入一个数字,插入值集,递增数字,然后重复(即(1,1,1,1)(2,2,2,2,)......等) 。当我到达10时发生违规。如果我现在输入任何数字,它将违反约束。然后我尝试输入字母'A',并试图插入以'A'开头的任何单词,这导致违规。我是否需要在约束中更改某些内容,或者这实际上应该如何工作?
ALTER TABLE [dbo].[Table] ADD CONSTRAINT [UC_Table_Column1_Column2_Column3_Column4]
UNIQUE NONCLUSTERED
(
[Column1] ASC,
[Column2] ASC,
[Column3] ASC,
[Column4] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF,
IGNORE_DUP_KEY = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON
[PRIMARY]
答案 0 :(得分:0)
这应该工作得很好。什么是实际的表定义?你的整理是什么?
-- Working Example
CREATE TABLE dbo.[Table](
Column1 VARCHAR(50)
, Column2 VARCHAR(50)
, Column3 INT
, Column4 VARCHAR(25)
);
ALTER TABLE [dbo].[Table] ADD CONSTRAINT [UC_Table_Column1_Column2_Column3_Column4]
UNIQUE NONCLUSTERED
(
[Column1] ASC,
[Column2] ASC,
[Column3] ASC,
[Column4] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF,
IGNORE_DUP_KEY = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON
[PRIMARY]
DECLARE @iteration INT = 0;
WHILE @iteration < 20
BEGIN
INSERT INTO [dbo].[Table]
SELECT @iteration, @iteration, @iteration, @iteration;
SET @iteration += 1;
END;
SELECT * FROM [dbo].[Table];