0

I read some answered questions but they don't help me! This is my table:

  CREATE TABLE [dbo].[t_status](
[student_id] [nvarchar](50) NOT NULL,
[les_id] [int] NOT NULL,
[year_id] [int] NOT NULL,
[status] [bit] NULL,
[Number_of_fail] [int] NULL,

I want prevent duplicate data if their student_id and les_id were equal. How can I do it, I don't have much info about triggers but I tried this one that didn't work!

        ALTER TRIGGER Trigger1
ON dbo.t_status
FOR INSERT
AS

declare @stu_id nvarchar(50)
declare @les_id int
Declare @Count int 

select @stu_id=student_id,@les_id = les_id From inserted


Select @Count=Count(student_id ) From t_status where les_id=@les_id and student_id=@stu_id
group by student_id

if @Count>1
delete From t_status where student_id =@stu_id
1
  • 4
    compound primary key on student_id and les_id?
    – dngfng
    Sep 26, 2012 at 12:38

2 Answers 2

1

i want prevent duplicate data if their student_id and les_id were equal. how can do it,

This is simple enough to do with UNIQUE KEY CONSTRAINT on these two columns than using a trigger.

ALTER TABLE t_status
ADD CONSTRAINT T_Status_Unique_StudentId_LesId UNIQUE (student_id, les_id)

Triggers are meant for enforcing more complex integrity constraints.

0

Make a composite/compound primary key, there are plenty of examples:

http://sqlzoo.net/howto/source/z.dir/tip241027/i02create.xml

google is your friend...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.