0

我对 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]
4

1 回答 1

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];
于 2013-03-12T15:55:45.583 回答