I am trying to either write a trigger or a check constraint to prevent
duplicates in my table. There are two columns I need to look at for the
duplicates and only one combo value for both columns is allowed in the
table. For e.g.
Column Serial can have only one '123456' value with testresult value as
'PASS'. This serial can be in the table many times with any other combo
so for e.g.
The table could contain 100 entries for serial column value '123456'
with testresult value 'FAIL', 'PENDING' etc.
** TESTED SCHEMA BELOW **
-- create table
CREATE TABLE bstresult
(ID int IDENTITY (1, 1) NOT NULL ,
serial char (10) NULL ,
testresult char (10) NULL
)
-- Insert valid values
insert into bstresult values ('123456','PASS')
insert into bstresult values ('123456','FAIL')
insert into bstresult values ('123456','FAIL')
insert into bstresult values ('123456','PENDING')
-- insert invalid value this should fail
insert into bstresult values ('123456','PASS')
If I simply create a unique constraint on both columns it will not
allow the FAIL combo or PENDING combo with the same serial which I need
to allow.
Appreciate your help.