Click here to Skip to main content
15,921,793 members
Please Sign up or sign in to vote.
1.22/5 (2 votes)
See more:
I have designed a table as following :
SQL
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.

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
Posted
Updated 29-Dec-22 21:01pm
v2
Comments
Sergey Alexandrovich Kryukov 25-Jul-12 3:36am    
This is pretty obvious: you tried to add the same student to the same school, which is prevented due to your UNIQUE constraint. I would understand your surprise if you showed your query which seemingly does not do it, but this is a normal behavior. Just look at your query or add its code to your question.

Still not clear?
--SA

Hi,

According to your situation
Violation of UNIQUE KEY constraint:

[StudentID],[Section] is composite primary key and it should be unique.
[SchoolID] should also be unique.


So you are trying to insert values with same ([StudentID],[Section]).

Check this. This is only error in your insert query.


--Amit
 
Share this answer
 
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.
 
Share this answer
 
Hi...


[SchoolID] is primary Key .so it is not allow duplicate value.

so the error came,.
 
Share this answer
 
Violation of UNIQUE KEY constraint 'UQ__TBL_Farm__91785593E0D120B6'. Cannot insert duplicate key in object 'dbo.TBL_Farmer_Details'. The duplicate key value is (232018411312)
 
Share this answer
 
Comments
Graeme_Grant 30-Dec-22 2:11am    
This was already answered and accepted back in July, 2012 - 10 years ago!

Please stick to current questions where help is needed.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900