I'd like to have a constraint on my table Visitor that permits the insertion of duplicate data. However, this should only be permitted if a boolean (IsUnique) is set to true.
E.g. (expected results for each insert after the -->):
INSERT INTO Visitor (FirstName, LastName, IsUnique) VALUES ('John', 'Doe', 'True') --> ok
INSERT INTO Visitor (FirstName, LastName, IsUnique) VALUES ('John', 'Doe', 'True') --> not ok. already exists
INSERT INTO Visitor (FirstName, LastName, IsUnique) VALUES ('John', 'Doe', 'False') --> ok. IsUnique is set to false
INSERT INTO Visitor (FirstName, LastName, IsUnique) VALUES ('John', 'Doe', 'False') --> also ok because IsUnique is set to false
This is what I came up with so far:
ALTER TABLE Visitor ADD CONSTRAINT CT_UniqueVisitor UNIQUE NONCLUSTERED (Title, Firstname, Lastname, Company, IsUnique)
This doesn't allow insertion of duplicate rows at all though. Please help.