违反UNIQUE KEY约束 [英] Violation of UNIQUE KEY constraint
问题描述
我设计了一个表如下:
I have designed a table as following :
CREATE TABLE [School](
[SchoolID] [int] IDENTITY(1,1) NOT NULL,
[StudentID] [int] NOT NULL,
[Section] [char](1) NOT NULL,
CONSTRAINT [PK_Student] PRIMARY KEY NONCLUSTERED
(
[SchoolID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY],
CONSTRAINT [CUK_Student] UNIQUE CLUSTERED
(
[StudentID] ASC,
[Section] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]</pre>
在插入值时,我给出了错误
On inserting values, i gives me error as
Violation of UNIQUE KEY constraint 'CUK_Student'. Cannot insert duplicate key in object 'School'. The statement has been terminated.
错误超出了我的理解。请帮助我如何解决这个问题,并解释这个错误背后的原因,以便我能理解其原因。
提前致谢,
Nutan Ranee
The error is beyond my understanding. Please help me how can I fix this and also explain me the reason behind this error so that i can understand its cause.
Thanks in advance,
Nutan Ranee
推荐答案
根据你的情况
Hi,
According to your situation
[StudentID],[Section]是复合主键,它应该是唯一的。
[SchoolID]也应该是唯一的。
[StudentID],[Section] is composite primary key and it should be unique.
[SchoolID] should also be unique.
所以你试图插入相同的值([StudentID], [部分]。
检查一下。这只是插入查询中的错误。
--Amit
So you are trying to insert values with same ([StudentID],[Section]).
Check this. This is only error in your insert query.
--Amit
您已创建列的唯一键[StudentID],[Section]。
例如,如果您在Studentid列中插入了1,则在section列中插入了section1。如果您尝试插入相同的值组合,则会出现该错误。
You have created unique key for columns [StudentID],[Section].
for example, if you have inserted "1" in Studentid column,"section1" in section column. If you try to insert the same combination of values you will get that error.
您好...
[SchoolID]是主键。所以它不允许重复值。
所以错误来了,。
Hi...
[SchoolID] is primary Key .so it is not allow duplicate value.
so the error came,.
这篇关于违反UNIQUE KEY约束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!